最长回文串前缀

来源:互联网 发布:python多进程和多线程 编辑:程序博客网 时间:2024/06/16 18:06

主要考虑了有两个以上回文串的情况,取末尾的回文串,然后我就从末尾开始判断,测了一些情况都复合,有错的地方请指正。

#include<stdio.h>#include<string.h>int huiwen(char *head,char *rear){    while(head<rear){        if(*head==*rear)        {          head++;          rear--;        }        else            return 0;    }    return 1;}char a[1000000];int main(){    while(scanf("%s",a)){      int len=strlen(a);      int count=0,i=0;      char *rear=a,*head=&a[len-1];      while(head>a){                   while(*rear!=*head&&rear<=head)              rear++;          if(head!=rear){              if(huiwen(rear,head)){                count=rear-a;                break;              }          }          head--;          rear=a;      }      if(head==a){         count=len;      }      printf("%d\n",count);    }    return 0;}
0 0
原创粉丝点击