1. 程式人生 > 實用技巧 >7-17 最長對稱子串 (25分)

7-17 最長對稱子串 (25分)

7-17 最長對稱子串 (25分)

對給定的字串,本題要求你輸出最長對稱子串的長度。例如,給定Is PAT&TAP symmetric?,最長對稱子串為s PAT&TAP s,於是你應該輸出11。

輸入格式:

輸入在一行中給出長度不超過1000的非空字串。

輸出格式:

在一行中輸出最長對稱子串的長度。

輸入樣例:

Is PAT&TAP symmetric?

輸出樣例:

11




#include<stdio.h>
#include<string.h>
int judge(char *a,int n)
{
int i;
for(i=0;i<n/2;i++)
{
if(a[i]!=a[n-i-1])
return 0;
}
return 1;
}
int main()
{
char a[1001];
gets(a);
int i;
int len=1;
int high;
for(i=0;i<strlen(a)-1;i++)
{

high=strlen(a)-1;
while(high>i)
{
if(a[high]==a[i]&&judge(a+i,high-i+1))
break;
high--;
}
if(high!=i&&len<high-i+1)
{
len=high-i+1;
}



}
printf("%d\n",len);
return 0;
}