usaco 1.3.3 Calf Flac

来源:互联网 发布:centos 安装chrome 编辑:程序博客网 时间:2024/05/01 08:05
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


不要尝试在头脑混乱的时候码代码,你永远无法预料自己会犯什么错。

(虽然头脑貌似清醒时也会犯各种奇葩错。)

不要忘了提交前修改一些测试方便的地方。如cout,pause,还有输入时的EOF。

要相信自己的算法是对的。如果自己都不相信自己,它就什么也不是了。

要敢于推测自己是错的……- -

写完if什么的先写大括号!已经不知道犯过多少次这样的脑残错了。

过了sample可以试些小test。会有“惊喜”。

用尽一切办法先搞出正确答案来再说。。。过程不是重点(误)


/*ID: wtff0411PROG: calfflacLANG: C++*/#include <iostream>#include <fstream>#include <string>#include <cstring>#include <vector>using namespace std;char input[20020];char in[20020];int save[20020];int main(){    freopen("calfflac.in","r",stdin);    freopen("calfflac.out","w",stdout);    //cin>>input;    char c;    int i=0;    int maxx=0,maxi;    int j=0;    while((c=getchar())!=EOF)    {        input[i++]=c;        if(isalpha(c))        {            in[j]=tolower(c);            j++;        }    }    input[i]='\0';    in[j]='\0';        int length=strlen(input);    int savej=j;    for(i=0;i<savej;i++)    save[i]=1;              //cout<<in<<endl;    for(i=1;i<savej;i++)//think about the condition more carefully    {        if(i-save[i-1]-1>=0&&in[i]==in[i-save[i-1]-1])        {            save[i]=save[i-1]+2;            continue;        }        else if(in[i]==in[i-1])        {            save[i]=save[i-1]+1;        }    }        for(i=1;i<savej;i++)    {        if(maxx<save[i])        {            maxx=save[i];            maxi=i;        }        //cout<<save[i]<<endl;    }        //cout<<maxi;    cout<<maxx<<endl;    int num=maxi-maxx+1;    j=0;    for(i=0;i<num;j++)    {        if(isalpha(input[j]))        {                        i++;        }    }        while(!isalpha(input[j]))    j++;        for(i=0;i<maxx&&j<length;j++)    {        putchar(input[j]);        if(isalpha(input[j]))        {            i++;        }    }        cout<<endl;    //system("pause");    return 0;}                    


妹子今天好认真。开学第一天就去自习要不要这么夸张。亚历山大啊。

好困好困好困好困。

原创粉丝点击