hihocoder1032(最长回文子串manacher算法)
2015-03-31 16:50
471 查看
题目连接:点击打开链接
解题思路:
manacher算法的模板题。
完整代码:
解题思路:
manacher算法的模板题。
完整代码:
#include <algorithm> #include <iostream> #include <cstring> #include <complex> #include <cstdio> #include <string> #include <cmath> #include <queue> using namespace std; typedef unsigned long long LL; const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const double EPS = 1e-9; const double PI = acos(-1.0); //M_PI; const int maxn = 2000001; int n; char s[maxn] , t[maxn]; int len[maxn]; void manacher(char t[]) { int t_len = strlen(t); int mx = 0 , id; for(int i = 1 ; i < t_len ; i ++) { if(mx > i) len[i] = min(len[id * 2 - i] , mx - i); else len[i] = 1; while(t[i + len[i]] == t[i - len[i]]) len[i] ++; if(len[i] + i > mx) { mx = len[i] + i; id = i; } } } void solve(char s[]) { int s_len = strlen(s); int t_len = s_len * 2 + 2; t[0] = '+'; for(int i = 0 ; i < t_len ; i ++) { t[i * 2 + 1] = '#'; t[i * 2 + 2] = s[i]; } manacher(t); int ans = -INF; for(int i = 1 ; i < t_len ; i ++) ans = max(ans , len[i]); printf("%d\n" , ans - 1); } int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif while(~scanf("%d",&n)) { for(int i = 0 ; i < n ; i ++) { scanf("%s" , s); solve(s); } } return 0; }
相关文章推荐
- hihoCoder 1032 最长回文子串 (manacher算法)
- hihoCoder 1032 最长回文子串(Manacher算法)
- hihocoder 1032 最长回文子串 (Manacher算法 详解+模板)
- hihocoder 1032, 最长回文子串, manacher算法
- hihocoder 1032 最长回文子串(Manacher算法)
- hihoCoder#1032 : 最长回文子串(manacher算法)
- hihocoder1032 最长回文子串
- [hihoCoder] #1032 : 最长回文子串
- [hihoCoder] #1032 : 最长回文子串
- hihocoder 1032 最长回文子串(Manacher)
- Hihocoder 1032 最长回文子串 Manacher && 亲测暴力可过
- hihoCoder - 1032 - 最长回文子串 (字符串~)
- hihocoder 1032 最长回文子串(Manachar算法)
- Hihocoder #1032 : 最长回文子串 (Manacher算法)
- 【hihoCoder】【1032】hiho一下第一周/A:最长回文子串
- hihocoder 1032 最长回文子串
- hiho#1032 : 最长回文子串 (manacher算法O(n)时间求字符串的最长回文子串 )
- hihoCoder hiho一下 第一周 #1032 : 最长回文子串 (Manacher)
- hihocoder-1032 最长回文子串(Manacher)
- hihoCoder#1032 最长回文子串——Manacher算法