人生亦梦


私信TA

用户名:uq_55161405400

访问量:8176

签 名:

追寻强大且简洁的算法解疑,不会有难题,因而我为此痴迷

等  级
排  名 3034
经  验 1978
参赛次数 1
文章发表 25
年  龄 0
在职情况 学生
学  校
专  业 网络空间安全

  自我简介:

菜,并痴迷着; 爱,并奋斗着

解题思路:

注意事项:

参考代码:

//思路:从最长串开始看,符合即为最大回文串长度,否则减1,继续查找

            具体见代码(建议粘贴于编译器上看,结构风格更好,易懂)



#include <stdio.h>

#include <string.h>


int cheak(char *p,int lon);         //判断回文

static int len;                             //全局变量

/*************************************/

int main ()

{

char ch[100];

int ture;

while(scanf("%s",ch)!=EOF)

{

    len=strlen(ch);

    for(int i=len;len>=1;i--)        //从最长串开始看,符合即为最大回文串长度,否则减1

    {

    ture=cheak(ch,i);     

    if(ture)

    {

    printf("%d\n",ture);              //找到最长串,开始下一组数

    break;

        }

}

    }

return 0;

}

/*************************************/

int cheak(char *p,int lon)        //长度为y的回文串检索

{

int a,b;

int y=lon-1;                            //数组下标从0开始,所以长度减1

for (int i=0;y<len;i++,y++)    //每检查lon长度后,右移继续检查回文串

{

  for(a=i,b=y;a<b;a++,b--)     //对比首尾

  {

  if(p[a]!=p[b])

  break;

  }

  if(a<b)                                  //说明break过,不满足回文,右移

  continue;

  else

  return lon;

}

return 0;                                 //都不满足,说明该长度无回文串

}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区