usaco 1.3.3 Calf Flac

本文介绍了一个算法,用于检测由大量笔记本电脑和奶牛产生的回文。通过忽略标点符号、空格、数字和大小写,算法能够找到字符串中最大的回文,且长度不超过20,000字符。该算法适用于文件输入,并能输出回文的长度及文本。
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


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

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

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

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

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

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

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

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


/*
ID: wtff0411
PROG: calfflac
LANG: 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;
}
        
        
    


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

好困好困好困好困。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值