Calf 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


哭陷阱忒多了。。

/*
ID: des_jas1
PROG: calfflac
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <algorithm>
//#define fin cin
//#define fout cout
using namespace std;

const int MAXN=20000+10; //要大
string origin,after;
int p[MAXN];

int main() 
{
    ofstream fout ("calfflac.out");
    ifstream fin ("calfflac.in");
	string tp;
	int i,j,m=0,max=0;
	string::size_type n;
    while(!fin.eof())
	{
       getline(fin,tp);
       origin+=tp+"\n"; //不知道为什么它自动踢掉了\n,好吧自己动手。
	}
	n=origin.size();
	after=origin;//没有这个会死
	for(i=0;i<n;i++)
		if(isalpha(origin[i]))
		{
			p[m]=i;
			after[m++]=toupper(origin[i]);
		}
    after.resize(m+1); //没有这个好像不会死。。
	int len,x,y;
    for(i=0;i<m;i++)
	{
		for(j=0;i-j>=0 && i+j<m;j++)
		{
			if(after[i-j]!=after[i+j]) break;
			len=j*2+1;
			if(len>max)
			{
				max=len;
				x=p[i-j];
				y=p[i+j];
			}
		}
		for(j=0;i-j>=0 && i+j+1<m;j++)
		{
			if(after[i-j]!=after[i+j+1]) break;
			len=(j+1)*2;
			if(len>max)
			{
				max=len;
				x=p[i-j];
				y=p[i+j+1];
			}
		}
		
	}
	fout<<max<<endl;
	for(;x<=y;x++)
		fout<<origin[x];
	fout<<endl;
	fout.close();
	fin.close();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值