标签:
题目链接:http://poj.org/problem?id=3974
Description
Input
Output
Sample Input
abcbabcbabcba abacacbaaaab END
Sample Output
Case 1: 13 Case 2: 6
Source
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <algorithm> 5 #include <iostream> 6 #include <cmath> 7 #include <queue> 8 #include <map> 9 #include <stack> 10 #include <list> 11 #include <vector> 12 13 using namespace std; 14 15 const int maxn = 2222222; 16 int pre[maxn]; 17 char str[maxn]; 18 char tmp[maxn]; 19 20 inline int min(int x, int y) { 21 return x < y ? x : y; 22 } 23 24 int init(char *tmp, char *str) { 25 int len = strlen(str); 26 tmp[0] = ‘$‘; 27 for(int i = 0; i <= len; i++) { 28 tmp[2*i+1] = ‘#‘; 29 tmp[2*i+2] = str[i]; 30 } 31 len = 2 * len + 2; 32 tmp[len] = 0; 33 return len; 34 } 35 36 void manacher(int *pre, char *tmp, int len) { 37 int id = 0; 38 int mx = 0; 39 for(int i = 1; i < len; i++) { 40 pre[i] = mx > i ? min(pre[2*id-i], mx-i) : 1; 41 while(tmp[i+pre[i]] == tmp[i-pre[i]]) { 42 pre[i]++; 43 } 44 if(pre[i] + i > mx) { 45 id = i; 46 mx = pre[i] + id; 47 } 48 } 49 } 50 51 int main() { 52 int kase = 1; 53 // freopen("in", "r", stdin); 54 while(~scanf("%s", str) && strcmp(str, "END") != 0) { 55 memset(pre, 0, sizeof(pre)); 56 memset(tmp, 0, sizeof(tmp)); 57 int len = init(tmp, str); 58 manacher(pre, tmp, len); 59 int ans = 1; 60 for(int i = 0; i < len; i++) { 61 ans = max(ans, pre[i]); 62 } 63 printf("Case %d: %d\n", kase++, ans - 1); 64 } 65 }
标签:
原文地址:http://www.cnblogs.com/vincentX/p/4769191.html