usaco pro 52 Calf Flac

来源:互联网 发布:最新编程语言 编辑:程序博客网 时间:2024/05/18 01:34
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)

11Madam, I'm Adam



回文串的题目。。
一看见回文串就想到DP。。。
结果发现,这道题可以直接暴力出来~枚举每一个字符的,向两端走,直到不相等为止。
只是要注意一下回文串中元素个数为奇数或者偶数的区别。。。
首先先将输入串的字符部分取出,放入另外一个数组,以便判断,但是要记录每个字符元素在原来字符串的位置。
求出最长的回文串,然后将其用原来在字符串中的位置输出。。


代码:

View Code
  1 /*  2 ID: jings_h1  3 PROG: calfflac  4 LANG: C++  5 */  6   7 #include<stdio.h>  8 #include<iostream>  9 #include<string.h> 10 using namespace std; 11 char a[20005]; 12 struct node{ 13        char v; 14        int p; 15 }; 16 node b[20005]; 17 int com(int length,int start){ 18     for(int j=1;j<=length;j++){ 19             if(b[start+j].v!=b[start-j].v) 20                   return (j-1); 21                   }        22    return length; 23 }           24 int com2(int length,int start){ 25     for(int g=0;g<=length;g++){ 26             if(b[start+g].v!=b[start-g-1].v){ 27                   return (g); 28                   } 29                   } 30     return length+1; 31 }  32 int main(){ 33     freopen("calfflac.in","r",stdin); 34     freopen("calfflac.out","w",stdout); 35     char temp[20005]; 36     memset(a,0,sizeof(a)); 37     memset(b,0,sizeof(b)); 38     int len=0; 39   //  int tem=2; 40     while(gets(temp)){ 41            // tem--; 42             strcpy(a+len,temp); 43             len+=strlen(temp); 44             a[len++]='\n'; 45             } 46     int times=0; 47     for(int j=0;j<len;j++){ 48             if(a[j]>='a'&&a[j]<='z'){ 49                    b[times].v=a[j]; 50                    b[times].p=j; 51                    times++; 52                    } 53             else if(a[j]>='A'&&a[j]<='Z'){ 54                    b[times].v=a[j]+'a'-'A'; 55                    b[times++].p=j; 56                    } 57     } 58     len=times; 59     //cout<<b<<endl; 60     int maxn1=0; 61     int pointa1=0; 62     int pointa2=0; 63     for(int i=0;i<len;i++){ 64             int temp1=i-0; 65             int temp2=len-1-i; 66             int res=temp1<temp2?temp1:temp2; 67             int result=com(res,i); 68             if(maxn1<result){ 69                  maxn1=result; 70                  pointa1=b[i-maxn1].p; 71                  pointa2=b[i+maxn1].p; 72                  } 73     } 74  //    cout<<"pooint1 "<<point1<<" "<<maxn1<<endl; 75  //   pointa1=point1-maxn1; 76    // maxn1=maxn1+maxn1+1; 77     int maxn2=0; 78     int pointb1=0; 79     int pointb2=0; 80   //  cout<<"len "<<len<<endl; 81     for(int k=1;k<len;k++){ 82             int temp1=k-1-0; 83             int temp2=len-1-k; 84             int res=temp1<temp2?temp1:temp2; 85             int result=com2(res,k); 86             if(maxn2<result){ 87                  maxn2=result; 88                  pointb1=b[k-maxn2].p; 89                  pointb2=b[k+maxn2-1].p; 90                  } 91                  }  92     if(((pointa2-pointa1)>(pointb2-pointb1))||(((pointa2-pointa1)==(pointb2-pointb1))&&(pointa1<=pointb1))){ 93               // printf("%d\n",maxn1*2+1); 94                cout<<maxn1*2+1<<endl; 95                for(int g=pointa1;g<=pointa2;g++){ 96                        //printf("%c",a[g]); 97                        cout<<a[g]; 98                        } 99               // printf("\n");100               cout<<endl;101                }102     else {103               // printf("%d\n",maxn2*2);104                cout<<maxn2*2<<endl;105                for(int k=pointb1;k<=pointb2;k++){106                       // printf("%c",a[k]);107                        cout<<a[k];108                        }109                //printf("\n");110                cout<<endl;111                }112     return 0;113 }114     

 

原创粉丝点击