1040 Longest Symmetric String (25 分)
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?
, the longest symmetric sub-string is s PAT&TAP s
, hence you must output 11
.
Input Specification:
Each input file contains one test case which gives a non-empty string of length no more than 1000.
Output Specification:
For each test case, simply print the maximum length in a line.
Sample Input:
Is PAT&TAP symmetric?
Sample Output:
11
马拉车模板题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1010;
char beginn[maxn],endd[maxn];
int len[maxn];
int tralation()
{
int gg=strlen(beginn);
int noww=0;
endd[0]='&';
for(int i=0;i<gg;i++)
{
endd[++noww]='#';
endd[++noww]=beginn[i];
}
endd[++noww]='#';
return noww;
}
int Manacher(int total)
{
int maxx = 0;
int mx = 0,id = 0; //id表示的最大回文的中心点是哪一个 而mx表示的是最大回文的中心点的最远的边界是哪一个
for(int i =1;i<=total;i++)
{
if(i<mx) //如果此刻 i的点比mx还要小的话说明 在mx-i处这边是回文 然后在比较一下
len[i] = min(mx - i,len[2*id-i]); //因为2*id - i 和 i 他们是相对于id对称的 所以说吧 就是要比较mx-i和len[2*id-i] ;
else len[i] = 1; //如果此刻i的点比边界还要大的话 那就需要从一开始加了
while(endd[i+len[i]]==endd[i-len[i]])
len[i]++;
if(i+len[i]>mx)
{
mx = i+len[i];
id = i;
}
maxx = max(maxx,len[i]);
}
return maxx-1;
}
int main()
{
scanf("%[^\n]",beginn);
int total = tralation();
int ans = Manacher(total);
printf("%d\n",ans);
return 0;
}