对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。
输入格式:
输入在一行中给出长度不超过1000的非空字符串。
输出格式:
在一行中输出最长对称子串的长度。
输入样例:
Is PAT&TAP symmetric?
输出样例:
11
这是我的代码
#include<stdio.h>
#include<string.h>
int issyms(char *a,char *b);
int forlen(char *a,char *b);
int main()
{
int length,max=1;
char syms[1001];
char *j=0;
char *i=syms;
gets(syms);
length=strlen(syms);
for(i=syms;i<=&syms[length-1];i++) //从字符串第一项开始
{
for(j=i+1;j<=&syms[length-1];j++) //遍历所有子串
{
if(issyms(i,j))
{
if(forlen(i,j)>max)
{
max=forlen(i,j); //找最大值
}
}
}
}
printf("%d",max);
return 0;
}
int issyms(char *a,char *b) //判断是否为对称字符串
{
while(a<b)
{
if(*a==*b)
{
a++;
b--;
}
else
{
return 0;
}
}
return 1;
}
int forlen(char *a,char *b) //计算字符串长度
{
char *i=a;
int cnt=0;
for(i=a;i<b+1;i++)
{
cnt++;
}
return cnt;
}
是我太愚钝了,真的只能用一种不是很好的方法去完成它,时间复杂度不太可观。