HDU 4513 吉哥系列故事 完美队形II (manacher)
2015-11-03 22:16
453 查看
题意:
N<=105的串,求最长回文串,并且左边的部分是不降的
分析:
计算p数组的时候判断一下就好了,−−注意′#′
代码:
N<=105的串,求最长回文串,并且左边的部分是不降的
分析:
计算p数组的时候判断一下就好了,−−注意′#′
代码:
// // Created by TaoSama on 2015-11-03 // Copyright (c) 2015 TaoSama. All rights reserved. // //#pragma comment(linker, "/STACK:1024000000,1024000000") #include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <string> #include <set> #include <vector> using namespace std; #define pr(x) cout << #x << " = " << x << " " #define prln(x) cout << #x << " = " << x << endl const int N = 2e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7; int n, k, p ; int a , s ; int manacher() { s[0] = '@'; s[1] = '#'; n = 2; for(int i = 0; i < k; ++i) s[n++] = a[i], s[n++] = '#'; s = 0; int mx = 0, id, ret = 0; for(int i = 1; i < n; ++i) { p[i] = mx > i ? min(mx - i, p[2 * id - i]) : 1; while(s[i - p[i]] == s[i + p[i]]) { if(p[i] == 1 || s[i + p[i]] == '#') {} else { if(s[i + p[i]] > s[i + p[i] - 2]) break; } ++p[i]; } if(mx < i + p[i]) mx = i + p[i], id = i; ret = max(ret, p[i] - 1); } return ret; } int main() { #ifdef LOCAL freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin); // freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout); #endif ios_base::sync_with_stdio(0); int t; scanf("%d", &t); while(t--) { scanf("%d", &k); for(int i = 0; i < k; ++i) scanf("%d", a + i); printf("%d\n", manacher()); } return 0; }
相关文章推荐
- UVA 12378 Ball Blasting Game
- 最长回文子串
- manacher算法
- hiho第一周——最长回文子串
- Longest Palindromic Substring
- 最长回文子串,Manacher算法
- Manacher算法-求字符串中最长回文串
- Manacher算法之个人愚见
- 最长回文串的解法---Manacher算法
- POJ 3974 Palindrome
- HDU 3068 最长回文
- KT学算法(三)——最长回文子串与Manacher算法
- 最长回文子串
- Manacher算法--O(n)回文子串算法
- Longest Palindromic Substring
- HDU 3068 最长回文子串
- Manacher求最长回文
- URAL 1297 Palindrome
- HDU 3613 Best Reward (manacher)
- 最长回文子串的manacher算法