It 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.
PROGRAM NAME: calfflac
INPUT FORMAT
A file with no more than 20,000 characters. The file has one or more lines which, when taken together, represent one long string. No line is longer than 80 characters (not counting the newline at the end).
SAMPLE INPUT (file calfflac.in)
Confucius say: Madam, I'm Adam.
OUTPUT FORMAT
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.
SAMPLE OUTPUT (file calfflac.out)
11 Madam, I'm Adam
/*
ID: jszhais1
PROG: calfflac
LANG: C++
*/
//#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ( "calfflac.in" ) ;
ofstream fout ( "calfflac.out" ) ;
int main(int argc, const char * argv[])
{
char transChar(char &ch_a);
int maxPalindromes(char *ch,int &start,int &end,const int &chLength);
char ch_a[20010];
int ch_length=0;
for(;!fin.eof();ch_length++)
ch_a[ch_length]=fin.get();
//for(;(ch_a[ch_length]=getchar())!='$';ch_length++);
ch_a[ch_length]='\0';
int start=0,end=0;
int length = maxPalindromes(ch_a,start,end,ch_length-1);
// cout<<"start: "<<start<<" end: "<<end<<endl;
fout<<length<<endl;
for(int i=start;i<=end;i++)
fout<<ch_a[i];
fout<<endl;
return 0;
}
int maxPalindromes(char *ch,int &start,int &end,const int &chLength)
{
char transChar(char &ch_a);
int MAX=0;
for(int i=1;i<=chLength;i++)
{
int left = 0,right = 1,max=0;
for(left=i,right=i;left>=0&&right<chLength;left--,right++)
{
//cout<<"singleRight"<<endl;
while(left>=0&&transChar(ch[left])==-1)
left--;
while(right<chLength&&transChar(ch[right])==-1)
right++;
//max++;
if(!(left>=0&&right<chLength)||transChar(ch[left])!=transChar(ch[right]))
{
//if(transChar(ch[right-1])==-1)
right--;
//if(transChar(ch[left+1])==-1)
left++;
break;
}
}//for
for(int j = left;j<=right;j++)
if(transChar(ch[j])!=-1)
max++;
if(max>MAX)
{
//cout<<"max1: "<<max<<"left: "<<left<<"right: "<<right<<endl;
MAX = max;
start = left;
end = right;
}//if
if(transChar(ch[i])==transChar(ch[i+1]))
{
left = 0,right = 0,max=0;
for(left=i,right=i+1;left>=0&&right<=chLength;left--,right++)
{
//cout<<"Hi: left: "<<left<<" "<<"right: "<<right<<endl;
while(left>=0&&transChar(ch[left])==-1)
left--;
while(right<chLength&&transChar(ch[right])==-1)
right++;
// max++;
if(!(left>=0&&right<chLength)||transChar(ch[left])!=transChar(ch[right]))
{
//if(transChar(ch[right-1])==-1)
right--;
//if(transChar(ch[left+1])==-1)
left++;
break;
}
}//for
for(int j = left;j<=right;j++)
if(transChar(ch[j])!=-1)
max++;
//max = right-left;
if(max>MAX)
{
//cout<<"max2: "<<max<<"left: "<<left<<"right: "<<right<<endl;
MAX = max;
start = left;
end = right;
}//if
}//if
}//for
//cout<<"["<<ch[end]<<"]"<<endl;
while (transChar(ch[start])==-1) {
start++;
}
while(transChar(ch[end])==-1)
end--;
int length=0;
for(int i=start;i<=end;i++)
{
if(transChar(ch[i])!=-1)
length++;
}
return length;
}
char transChar(char &ch_a)
{
char ch_b;
if(ch_a>='a'&&ch_a<='z')
ch_b = ch_a;
else if(ch_a>='A'&&ch_a<='Z')
ch_b = ch_a+32;
else
ch_b=-1;
return ch_b;
}
结果:
USER: jim zhai [jszhais1] TASK: calfflac LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 3360 KB] Test 2: TEST OK [0.000 secs, 3360 KB] Test 3: TEST OK [0.000 secs, 3360 KB] Test 4: TEST OK [0.000 secs, 3360 KB] Test 5: TEST OK [0.000 secs, 3360 KB] Test 6: TEST OK [0.000 secs, 3360 KB] Test 7: TEST OK [0.000 secs, 3360 KB] Test 8: TEST OK [0.313 secs, 3360 KB] All tests OK.
Russ Cox
To make the programming easier, we keep two copies of the text: the original, and one with the punctuation stripped out. We find the biggest palindrome in the latter copy, and then figure out which part of the original it corresponds to.
To find the biggest palindrome in the alphabetic text, we look, for each letter in the text, at the biggest palindrome centered on that letter (in the case of an odd length palindrome) or just to the right of that letter (in the case of an even length palindrome).
There are 20,000 letters, and we are assured that no palindrome is more than 2,000 letters long, and we search for both even and odd palindromes, for a total of about 20,000*2,000*2 = 80,000,000 operations, which is perfectly reasonable within the time limit.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
char fulltext[21000];
char text[21000];
char *pal;
int pallen;
void
findpal(void)
{
char *p, *fwd, *bkwd, *etext;
int len;
etext = text+strlen(text);
for(p=text; *p; p++) {
/* try palindrome with *p as center character */
for(fwd=bkwd=p; bkwd >= text && fwd < etext && *fwd == *bkwd;
fwd++, bkwd--)
;
bkwd++;
len = fwd - bkwd;
if(len > pallen) {
pal = bkwd;
pallen = len;
}
/* try palindrome with *p as left middle character */
for(bkwd=p, fwd=p+1;
bkwd >= text && fwd < etext && *fwd == *bkwd; fwd++, bkwd--)
;
bkwd++;
len = fwd - bkwd;
if(len > pallen) {
pal = bkwd;
pallen = len;
}
}
}
void
main(void)
{
FILE *fin, *fout;
char *p, *q;
int c, i, n;
fin = fopen("calfflac.in", "r");
fout = fopen("calfflac.out", "w");
assert(fin != NULL && fout != NULL);
/* fill fulltext with input, text with just the letters */
p=fulltext;
q=text;
while((c = getc(fin)) != EOF) {
if(isalpha(c))
*q++ = tolower(c);
*p++ = c;
}
*p = '\0';
*q = '\0';
findpal();
fprintf(fout, "%d\n", pallen);
/* find the string we found in the original text
by finding the nth character */
n = pal - text;
for(i=0, p=fulltext; *p; p++)
if(isalpha(*p))
if(i++ == n)
break;
assert(*p != '\0');
/* print out the next pallen characters */
for(i=0; i<pallen && *p; p++) {
fputc(*p, fout);
if(isalpha(*p))
i++;
}
fprintf(fout, "\n");
exit(0);
}