51 nod 1088 最长回文子串

/*
1088 最长回文子串
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注
回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。
输入一个字符串Str,输出Str里最长回文子串的长度。
Input
输入Str(Str的长度 <= 1000)
Output
输出最长回文子串的长度L。
Input示例
daabaac
Output示例
5
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
char c[1010];
int main()
{
cin>>c;
int len=strlen(c);
int count1=0,ans=0,count2=0,flag=0;
for(int i=0;i<len;i++)
{
int j=i-1;
while((2*i-j)<len&&j>=0&&c[j]==c[2*i-j]) {
count1++;
j--;
}
j=i-1;
while((2*i-j-1)<len&&j>=0&&c[j]==c[2*i-j-1]) {
count2++;
j--;
}

/*if((2*i-j)<len&&c[j]==c[2*i-j])
{
count1++;
flag=1;
} //若不满足第一个条件则继续往下运行,满足了下面的条件的话继续执行循环,
//这样导致不满足一条件的情况没有排除掉
if((2*i-j-1)<len&&c[j]==c[2*i-j-1])
{
count2++;
}
else
{
if(flag==0)
break;
}
*/

count1=count1*2+1;
count2=count2*2;
ans=max(ans,max(count1,count2));
count1=0;
count2=0;

}
cout<<ans<<endl;
return 0;
}

猜你喜欢

转载自www.cnblogs.com/kongblog/p/9594113.html
今日推荐