Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1491 Accepted Submission(s): 555
2 3 51 52 51 4 51 52 52 51
3 4
题意:求最长回文子串,而外要求:从回文串最中间向两边满足非递增。
分析:在manacher函数中加一个判断,跳过原来的加入的值,以及加一个判断控制最中间向两边满足非递增;
转载请注明出处:寻找&星空の孩子
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4513
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define maxn 100010*2 int P[maxn]; //(p.s. 可以看出,P[i]-1正好是原字符串中回文串的总长度) int s1[maxn]; int s2[maxn]; int n; void manacher(int* s) { int i,id=0,mx=0; P[0]=0; for(i=1;i<=2*n+1;i++) { if(mx > i) P[i] = min(P[2*id-i],mx-i); else P[i] = 1; while(s[i+P[i]]==s[i-P[i]] ) { if(s[i+P[i]]!=-2) { if(s[i+P[i]]<=s[i+P[i]-2]) P[i]++; else break; } P[i]++; } if(mx < P[i] + i) { mx = P[i] + i; id = i; } } } void init() { int i, j = 2; s2[0] =-1, s2[1] = -2; for(i=0;s1[i];i++) { s2[j++] = s1[i]; s2[j++] = -2; } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&s1[i]); init(); manacher(s2); int ans=0; for(int i=1;i<=2*n+1;i++) { ans=max(ans,P[i]); } printf("%d\n",ans-1); } return 0; }