最长回文
题目描述
给出一个只由小写英文字符a,b,c…y,z组成的字符串S,求S中最长回文串的长度.
回文就是正反读都是一样的字符串,如aba, abba等
输入
输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c…y,z组成的字符串S
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000
输出
每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.
样例
Sample Input
aaaa
abab
Sample Output
4
3
题意
练习模板。。。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))
#define lson st<<1
#define rson st<<1|1
const int MAXN = 1e6+10;
int p[MAXN];
int malache(string s) {
string t = "$#";
for(int i = 0; i < s.size(); i++) {
t += s[i];
t += "#";
}
int mx = 0, id = 0, resl = 0, resc = 0;
for(int i = 1; i < t.size(); i++) {
p[i] = mx>i ? min(p[id*2-i],mx-i):1;
while(t[i+p[i]] == t[i-p[i]])
p[i]++;
if(mx < p[i]+i) {
mx = p[i]+i;
id = i;
}
if(resl < p[i]) {
resl = p[i];
resc = i;
}
}
return resl-1;
}
int main() {
ios::sync_with_stdio(false);
string s;
while(cin >> s) {
cout << malache(s) << endl;
s.clear();
}
}