没看清输入输出方式,一个简单的manacher都错了好几次。。。。
Calf FlacIt is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes. Your job will be to detect these bovine beauties.
Ignore punctuation, whitespace, numbers, and case when testing for palindromes, but keep these extra characters around so that you can print them out as the answer; just consider the letters `A-Z' and `a-z'.
Find the largest palindrome in a string no more than 20,000 characters long. The largest palindrome is guaranteed to be at most 2,000 characters long before whitespace and punctuation are removed.
Confucius say: Madam, I'm Adam.
The first line of the output should be the length of the longest palindrome found. The next line or lines should be the actual text of the palindrome (without any surrounding white space or punctuation but with all other characters) printed on a line (or more than one line if newlines are included in the palindromic text). If there are multiple palindromes of longest length, output the one that appears first.
11 Madam, I'm Adam
/* ID:qhn9992 PROG:calfflac LANG:C++11 */ #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> using namespace std; char str[220000]; char s[440000]; int p[440000],id[440000],pos,how; void manacher() { pos=-1,how=0; int len=strlen(s); int mid=-1,mx=-1; for(int i=0;i<len;i++) { int j=-1; if(i<mx) { j=2*mid-1; p[i]=min(p[j],mx-i); } else p[i]=1; while(i+p[i]<len&&s[i+p[i]]==s[i-p[i]]) p[i]++; if(p[i]+i>mx) { mx=p[i]+i; mid=i; } if(p[i]>how) { how=p[i]; pos=i; } } how--; } int main() { freopen("calfflac.in","r",stdin); freopen("calfflac.out","w",stdout); char ch; int ct=0; while(scanf("%c",&ch)!=EOF) { str[ct++]=ch; } memset(id,-1,sizeof(id)); int n=ct,cnt=2; s[0]='$',s[1]='#'; for(int i=0;i<n;i++) { if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')) { char c=str[i]; if(c>='A'&&c<='Z') c=c-'A'+'a'; s[cnt]=c; id[cnt++]=i; s[cnt++]='#'; } } manacher(); int st=pos-how,ed=pos+how; while(id[st]==-1) st++; while(id[ed]==-1) ed--; printf("%d\n",how); for(int i=id[st];i<=id[ed];i++) putchar(str[i]); putchar(10); return 0; }