b babd a abcd
0 2 aza No solution!
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; int const MAX = 200005; char s[MAX << 1], save[MAX << 1]; int p[MAX << 1], l, r; int Manacher() { int len = strlen(s), maxp = 0, maxl = 0; for(int i = len; i >= 0; i--) { s[i * 2 + 2] = s[i]; s[i * 2 + 1] = '#'; } s[0] = '*'; for(int i = 2; i < 2 * len + 1; i++) { if(p[maxp] + maxp > i) p[i] = min(p[2 * maxp - i], p[maxp] + maxp - i); else p[i] = 1; while(s[i - p[i]] == s[i + p[i]]) p[i]++; if(p[maxp] + maxp < i + p[i]) maxp = i; if(maxl < p[i]) { l = (i - p[i]) / 2; r = (i + p[i]) / 2 - 2; maxl = p[i]; } } return maxl - 1; } int main() { int ans; char ch[2]; while(scanf("%s %s",ch, s) != EOF) { int len = strlen(s); l = r = 0; for(int i = 0; i < len; i++) { int tmp = s[i] - ch[0]; if(tmp < 0) tmp = 26 + tmp; s[i] = 'a' + tmp; } strcpy(save, s); int ans = Manacher(); if(ans == 1) printf("No solution!\n"); else { printf("%d %d\n", l, r); for(int i = l; i <= r; i++) printf("%c", save[i]); printf("\n"); } } }
HDU 3294 Girls' research (Manacher算法 + 记录区间)
原文地址:http://blog.csdn.net/tc_to_top/article/details/43806819