USACO1.24Calf Flac

Calf Flac

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


交了四次 长度算错了 看到没过的那组数据就知道自己哪错了 改了一下就交了 连样例都没过 仔细一看改错地方了。。
循环走一遍 走到位置向左右按回文串延伸 随时更新最大的延伸
View Code
  1 /*

  2 ID: your_id_here

  3 PROG: calfflac

  4 LANG: C++

  5 */

  6 #include <iostream>

  7 #include<cstdio>

  8 #include<string.h>

  9 #include<cmath>

 10 using namespace std;

 11 char str,c[20011];

 12 int judge(char c)

 13 {

 14     if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))

 15     return 0;

 16     else

 17     return 1;

 18 }

 19 int pan(char a,char b)

 20 {

 21     if(fabs(a-b)==32||a==b)

 22     return 1;

 23     else

 24     return 0;

 25 }

 26 int main()

 27 {

 28     freopen("calfflac.in","r",stdin);

 29     freopen("calfflac.out","w",stdout);

 30     int i,j,k,n,m,g=0,s,t,l,r;

 31     while(scanf("%c",&str)!=EOF)

 32     {

 33         g++;

 34         c[g] = str;

 35     }

 36     s = 1;

 37     t = 1;

 38     int max = 0;

 39     for(i = 1; i < g; i++)

 40     {

 41         l = i-1;

 42         r = i+1;

 43         int tmax = 0;

 44         if(!judge(c[i]))

 45         tmax = 1;

 46         else

 47         tmax = 0;

 48         while(l>=1&&r<=g)

 49         {

 50             while(l>=1&&judge(c[l]))

 51             l--;

 52             if(l<1)

 53             break;

 54             while(r<=g&&judge(c[r]))

 55             r++;

 56             if(r>g)

 57             break;

 58             if(pan(c[l],c[r]))

 59             {

 60                 l--;

 61                 r++;

 62                 tmax++;

 63                 tmax++;

 64                 if(tmax>max)

 65                 {

 66                     s = l+1;

 67                     t = r-1;

 68                     max = tmax ;

 69                 }

 70             }

 71             else

 72             break;

 73         }

 74         l = i;

 75         r = i+1;

 76         tmax = 0;

 77         while(l>=1&&r<=g)

 78         {

 79             while(l>=1&&judge(c[l]))

 80             l--;

 81             if(l<1)

 82             break;

 83             while(r<=g&&judge(c[r]))

 84             r++;

 85             if(r>g)

 86             break;

 87             if(pan(c[l],c[r]))

 88             {

 89                 l--;

 90                 r++;

 91                 tmax++;

 92                 if(2*tmax>max)

 93                 {

 94                     s = l+1;

 95                     t = r-1;

 96                     max = 2*tmax;

 97                 }

 98             }

 99             else

100             break;

101         }

102     }

103     printf("%d\n",max);

104     while(s<=g&&judge(c[s]))

105     s++;

106     while(t>=1&&judge(c[t]))

107     t--;

108     for(i = s ; i <= t ; i++)

109     printf("%c",c[i]);

110     puts("");

111     fclose(stdin);

112     fclose(stdout);

113     return 0;

114 }

 

你可能感兴趣的:(USACO)