题意:
据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文。你的工作就是去寻找这些牛制造的奇观(最棒的回文)。
在寻找回文时不用理睬那些标点符号、空格(但应该保留下来以便做为答案输出),只用考虑字母'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 ;
}