标签:
题目大意:
求字符串的最长回文子串的长度。
思路:
Manacher板题,Hash可能会T。要学习Manacher,可参考https://www.felix021.com/blog/read.php?2040
代码:
#include<cstdio> #include<cstring> #include<iostream> #define M 350000 using namespace std; int r[M]; char s[M],str[M]; int main() { while (scanf("%s",s)!=EOF) { int i,k,l=strlen(s),ans=0,maxr=0; for (str[0]=‘$‘,i=l;i>=0;i--) str[i+i+2]=s[i],str[i+i+1]=‘#‘; for (i=2;i<=l+l;i++) { r[i]=maxr>i?min(r[k+k-i],maxr-i):1; for (;str[i-r[i]]==str[i+r[i]];r[i]++); if (i+r[i]>maxr) maxr=i+r[i],k=i; ans=max(r[i],ans); } printf("%d\n",ans-1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/HHshy/p/5734126.html