标签:iostream sdn math net tac names ace int ios
居然能够做到O(n)的复杂度求最长回文。,也是给跪了。
以下这个人把manacher讲的很好,,能够看看
http://blog.csdn.net/xingyeyongheng/article/details/9310555
我就照着他的代码敲了一遍贴了个模板。。
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<string> #include<vector> #include<cstring> #include<iostream> #include<algorithm> #include<functional> using namespace std; const int MX = 1e6 + 5; char s[MX * 2];//记得要开两倍 int p[MX * 2]; int manacher(char *s){ int len = strlen(s), id = 0, ans = 0; for(int i = len; i >= 0; i--) { s[i + i + 2] = s[i]; s[i + i + 1] = ‘#‘; } s[0] = ‘*‘;//防越界,非常重要!! for(int i = 2; i < 2 * len + 1; ++i) { if(p[id] + id > i) p[i] = min(p[2 * id - i], p[id] + id - i); else p[i] = 1; while(s[i - p[i]] == s[i + p[i]]) p[i]++; if(id + p[id] < i + p[i]) id = i; ans = max(ans, p[i] - 1); } return ans; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", s); printf("%d\n", manacher(s)); } return 0; }
标签:iostream sdn math net tac names ace int ios
原文地址:http://www.cnblogs.com/lxjshuju/p/6915897.html