usaco Calf Flac 解题报告

题意:

据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文。你的工作就是去寻找这些牛制造的奇观(最棒的回文)。

在寻找回文时不用理睬那些标点符号、空格(但应该保留下来以便做为答案输出),只用考虑字母'A'-'Z'和'a'-'z'。要你寻找的最长的回文的文章是一个不超过20,000个字符的字符串。我们将保证最长的回文不会超过2,000个字符(在除去标点符号、空格之前)。

题解:

枚举每一个字母,看它能否作为一个回文中间的那个字符,在这里面找最大的那个就好了

代码:

我写的好搓啊,冗长。。

/*
ID:		lishicao
PROG:	calfflac
LANG:	C++
*/
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std ;

ifstream fin  ( "calfflac.in"  ) ;
ofstream fout ( "calfflac.out" ) ;

char  str[20005] ;

int  main()
{
	int  Count = 0 , Max = 0 , Start , End , i ;
	char ch ;
	while( ! fin.eof() ){
		str[Count] = fin.get() ;
		Count ++ ;
	}
	for( i = 0 ; i < Count ; i ++ )
	{
	    if( ! ( ( str[i] >= 'a' && str[i] <= 'z' ) || ( str[i] >= 'A' && str[i] <= 'Z' ) ) ) continue ;
		int  start , end , nexts , nexte ;
		int  cnt ;

		start = i ; end = i ;
		cnt = 1 ;
		while( 1 )
		{
			int j ;
			for( j = start - 1 ; j >= 0 ; j -- )
				if( ( str[j] >= 'A' && str[j] <= 'Z' ) || ( str[j] >= 'a' && str[j] <= 'z' ) ) break ;
			nexts = j ;


			for( j = end + 1 ; j < Count ; j ++ )
				if( ( str[j] >= 'A' && str[j] <= 'Z' ) || ( str[j] >= 'a' && str[j] <= 'z' ) ) break ;
			nexte = j ;

			if( nexts < 0 || nexte >= Count ) break ;
			if( str[nexts] - 'A' != str[nexte] - 'a' && str[nexts] - 'a' != str[nexte] - 'a' && str[nexts] - 'a' != str[nexte] - 'A' ) break ;
			start = nexts ; end = nexte ;
			cnt += 2 ;
		}
		if( cnt > Max )
		{
			Start = start ;
			End = end ;
			Max = cnt ;
		}

		start = i ;
		int  j ;
        for( j = start + 1 ; j < Count ; j ++ )
            if( ( str[j] >= 'A' && str[j] <= 'Z' ) || ( str[j] >= 'a' && str[j] <= 'z' ) ) break ;
        end = j ;

        if( str[start] - 'A' != str[end] - 'a' && str[start] - 'a' != str[end] - 'a' && str[start] - 'a' != str[end] - 'A' ) continue ;

		cnt = 2 ;
		while( 1 )
		{
			int j ;
			for( j = start - 1 ; j >= 0 ; j -- )
				if( ( str[j] >= 'A' && str[j] <= 'Z' ) || ( str[j] >= 'a' && str[j] <= 'z' ) ) break ;
			nexts = j ;

			for( j = end + 1 ; j < Count ; j ++ )
				if( ( str[j] >= 'A' && str[j] <= 'Z' ) || ( str[j] >= 'a' && str[j] <= 'z' ) ) break ;
			nexte = j ;

			if( nexts < 0 || nexte >= Count ) break ;
			if( str[nexts] - 'A' != str[nexte] - 'a' && str[nexts] - 'a' != str[nexte] - 'a' && str[nexts] - 'a' != str[nexte] - 'A' ) break ;
			start = nexts ; end = nexte ;
			cnt += 2 ;
		}
		if( cnt > Max )
		{
			Start = start ;
			End = end ;
			Max = cnt ;
		}
	}
	fout << Max << endl ;
	for( i = Start ; i <= End ; i ++ )
		fout << str[i]  ;
	fout << endl ;
	return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值