标签:manacher
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 aba 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 acacac
1 6
Statistic | Submit | Discuss | Note
这题我是用manacher做的,不过很多人好像是EKMP搞的
先求出p数组,然后遍历一遍,找到回文到字符串端点的回文串,然后求最大值即可
/************************************************************************* > File Name: hdu3613.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年02月02日 星期一 11时59分27秒 ************************************************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const double pi = acos(-1); const int inf = 0x3f3f3f3f; const double eps = 1e-15; typedef long long LL; typedef pair <int, int> PLL; const int N = 1100000; char str[N]; char mat[N]; int p[N]; int val[30]; int sum[N]; void manacher (int cnt) { memset (p, 0, sizeof(p)); int MaxId = 0, id; for (int i = 1; i < cnt; ++i) { if (MaxId > i) { p[i] = min (p[2 * id - i], MaxId - i); } else { p[i] = 1; } while (str[i + p[i]] == str[i - p[i]]) { ++p[i]; } if (p[i] + i > MaxId) { MaxId = p[i] + i; id = i; } } } int main () { int t; scanf("%d", &t); while (t--) { for (int i = 0; i < 26; ++i) { scanf("%d", &val[i]); } scanf("%s", mat); int cnt = 2; int len = strlen (mat); for (int i = 0; i < len; ++i) { str[cnt++] = mat[i]; str[cnt++] = '#'; } str[0] = '?'; str[1] = '#'; str[cnt] = '\0'; sum[0] = 0; manacher (cnt); for (int i = 1; i < cnt; ++i) { sum[i] = sum[i - 1]; if (str[i] != '#') { sum[i] += val[str[i] - 'a']; } } int ans = 0; for (int i = 2; i < cnt - 1; ++i) { if (p[i] - 1 == len) { continue; } if (i - p[i] + 1 == 1) { int tmp = sum[i + p[i] - 1]; int id = (cnt - 1 + i + p[i] - 1) / 2; if (p[id] == cnt - 1 - id + 1) { tmp += sum[cnt - 1] - sum[i + p[i] - 2]; } ans = max(ans, tmp); } else if (i + p[i] - 1 == cnt - 1) { int tmp = sum[i + p[i] - 1] - sum[i - p[i]]; int id = (1 + i - p[i] + 1) / 2; if (p[id] == id) { tmp += sum[i - p[i] + 1]; } ans = max(ans, tmp); } } printf("%d\n", ans); } return 0; }
标签:manacher
原文地址:http://blog.csdn.net/guard_mine/article/details/43406937