字符串处理的比较麻烦的题目,最难的地方就是要输出原文.
这道题目首先要预处理一下,把不是字母的全部去掉,再变成大写字母(为了便于比较),存到一个string的变量里面,原文保存到时候要注意用
一行一行的读,而这个函数是不包括换行符'\n'的,所以自己别忘了手动加上
为了提高效率,我们的回文函数用的是从中间往两边扩散的方法进行处理的,回文有两种形式,一种是奇回文,另一种是偶回文
都要进行判断
回文函数找到的是处理过后的字符串中 的开始回文的位置及长度,那么如何才能输出原文呢?
我们对字符串处理的时候只是把字母放进去了,所以在输出的时候要从第s个字母以后的内容中输出,一直输出输出的 内容中包括len个字母
那么我们的就完成了原文的输出
/*
ID: zhuihun1
PROG: calfflac
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include<algorithm>
#define MAXSIZE 5005
using namespace std;
string yuanwen,str;
int s=0,len=0;
ofstream fout ("calfflac.out");
ifstream fin ("calfflac.in");
void huiwen()
{
int i,j,n,x;
n=(int)str.length();
for(i=1;i<n;i++)
{
//判断偶回文
x=0;
j=1;
while(str[i+j]==str[i-j+1]&&i-j+1>-1&&i+j<n)
{
x+=2;j++;
}
if(x>len)
{
len=x;
s=i-x/2+1;
}
//判断奇回文
x=1;
j=1;
while(str[i+j]==str[i-j]&&i-j>-1&&i+j<n)
{
x+=2;j++;
}
if(x>len)
{
len=x;
s=i-x/2;
}
}
}
void display()
{
int i,n,x=0,j=0;
char c;
n=(int)yuanwen.length();
for(i=0;i<n;i++)
{
c=yuanwen[i];
if((c>='a'&&c<='z')||(c>='A'&& c<='Z'))
x++;
if(x>s&&j<len)
{
fout<<c;
if((c>='a'&&c<='z')||(c>='A'&& c<='Z'))
j++;
}
}
}
int main()
{
string tmp="";
char c;
int i;
while(getline(fin,tmp))
{
yuanwen=yuanwen+tmp+'\n';
for(i=0;i<(int)tmp.length();i++)
{
c=tmp[i];
if(c>='a'&&c<='z')
str+=c-32;
else if(c>='A'&& c<='Z')
str+=c;
}
}
huiwen();
fout<<len<<endl;
display();
fout<<endl;
return 0;
}