您的位置:首页 > 其它

PAT (Advanced Level) Practise 1040 Longest Symmetric String (25)

2017-07-19 11:37 330 查看


1040. Longest Symmetric String (25)

时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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


题意:给你一个字符串,输出它的回文串最大长度

解题思路:Manacher算法

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

#define ll long long

char s1[110009],s2[220009];
int p[220009];

int main()
{
while(gets(s1))
{
int i=0,len=1;
s2[0]='$';
while(s1[i]!='\0')
{
s2[len++]='#';
s2[len++]=s1[i];
i++;
}
s2[len++]='#';
s2[len]='\0';
memset(p,0,sizeof p);
int id=0,ma=0;
for(int i=0;i<len;i++)
{
p[i]=ma>i?min(p[id*2-i],ma-i):1;
while(s2[i+p[i]]==s2[i-p[i]]) p[i]++;
if(i+p[i]>ma)
{
ma=i+p[i];
id=i;
}
}
int ans=0;
for(int i=0;i<len;i++)
ans=max(ans,p[i]-1);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: