HDU3068 longest reply
Maximum retrieval Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission (s): 13605 Accepted Submission (s): 4947
The Problem Description is a string consisting of lowercase English characters a, B, c... y, and z. Evaluate the length of the longest return string in S.
The same string is used for both positive and negative reads, such as aba and abba.
There are multiple Input cases in each group. Each Input is a string consisting of lowercase letters a, B, c... y, and z.
The two cases are separated by empty rows (this empty row does not need to be processed)
String Length len <= 110000
An integer x in each line of Output corresponds to a group of cases, which indicates the maximum length of the input string in the group of cases.
Sample Input
Aaaa abab
Sample Output
4 3
Source 2009 Multi-University Training Contest 16-Host by NIT
Analysis: The template Question of the manacher algorithm.
<span style="font-size:18px;">#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define MAXN 110010char s[MAXN],str[MAXN*2];int p[MAXN*2];int n;void init(){ int i; str[0] = '@'; str[1] = '#'; for(i=0,n=2; s[i]; i++,n+=2) { str[n] = s[i]; str[n+1] = '#'; } str[n] = 0;}int solve(){ int id; int mx=0; int ans = 0; for(int i=1; str[i]; i++) { if(mx > i) p[i]=p[2*id-i]>(mx-i)?(mx-i):p[2*id-i]; else p[i] = 1; while(str[i-p[i]] == str[i+p[i]]) p[i]++; if(p[i]+i > mx) { mx = p[i] + i; id = i; } if(ans < p[i]) ans = p[i]; } return ans-1;}int main(){ while(scanf("%s",s)!=-1) { init(); printf("%d\n",solve()); }}</cstring></cstdio></iostream></span>