附上题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 这个题的意思是给你一个串, 求这个串中的最长的回文串的长度, 直接裸manacher即可。 代码如下:
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int maxn = 110000*2+100; char input[maxn]; char a[maxn]; int p[maxn]; //manacher void manacher(char *s){ int len = strlen(s+1); int m = 2*len+1; for(int i=1; i<=len; i++){ a[i<<1] = s[i]; a[i<<1|1] = ‘#‘; } a[0] = ‘+‘; a[1] = ‘#‘; a[m+1] = ‘-‘; // cout<<a<<endl; int mx=0, idx; int res = 0; for(int i=1; i<=m; i++) { if(mx > i) p[i] = min(p[2*idx-i], mx-i); else p[i] = 1; for(; a[i-p[i]]==a[i+p[i]]; p[i]++); if(p[i]+i > mx) mx = p[i]+i, idx=i; res = max(res, p[i]); } cout<<res-1<<endl; // for(int i=1; i<=m; i++) // cout<<p[i]<<‘ ‘; // cout<<endl; } int main() { while(scanf("%s", input+1) != EOF){ manacher(input); } return 0; }
时间: 2024-10-10 07:24:30