标签:
http://acm.hdu.edu.cn/showproblem.php?pid=3294
Description
Input
Output
Sample Input
Sample Output
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f #define N 1000007 char s[N], S[N], str[N]; int p[N], Mid; int Manacher() { int MaxLen = 0, index = 0, ans = 1; int len = strlen(s); Mid = 0; for(int i=2; i<len; i++) { if(MaxLen>i) p[i] = min(p[index*2-i], MaxLen-i); else p[i] = 1; while(s[i-p[i]]==s[i+p[i]] ) p[i]++; if(p[i]+i>MaxLen) { MaxLen = p[i] + i; index = i; } if(p[i]>ans) { ans = p[i]; Mid = i; } } return ans-1; } int main() { char ch[10]; while(scanf("%s%s", ch, S)!=EOF) { int i, len = strlen(S), number; char c; number = ch[0]-‘a‘; s[0] = ‘$‘; for(i=0; i<len; i++) { c = S[i] - number; if(c<‘a‘) c += 26; S[i] = c; s[i*2+1] = ‘#‘; s[i*2+2] = S[i]; } s[i*2+1] = ‘#‘; s[i*2+2] = 0; int ans = Manacher(); if(ans<2) printf("No solution!\n"); else { int L = Mid/2-(ans+1)/2; printf("%d %d\n", L, L+ans-1); memset(str, 0, sizeof(str)); strncpy(str, S+L, ans); printf("%s\n", str); } } return 0; }
(回文串 Manacher )Girls' research -- hdu -- 3294
标签:
原文地址:http://www.cnblogs.com/YY56/p/4853644.html