# 1.3.3 Calf Flac

1.3.3 Calf Flac

``Confucius say: Madam, I'm Adam.``

``````11
``````

``````#include <stdio.h>
#include <string.h>
//#include <time.h>
struct{
int start;
int end;
int length;
}f;
char str[20015],a[20015];
int b[20015];
int main(){
int i=0,j=0,s,t;
//	long l1,l2;
//int k;
while(scanf("%c",&str[i])!=EOF){
if(str[i]>='a' && str[i]<='z'){
a[j]=str[i];
b[j++]=i;
}
if(str[i]>='A' && str[i]<='Z'){
a[j]=str[i]+32;
b[j++]=i;
}
i++;
}
//	l1=clock();
/*	for(i=0;i<j;i++){
for(k=i+1;k<j;k++)
if(a[k]==a[i]){
t=k-1;
for(s=i+1;s<=i+(k-i)/2;){
if(a[s]!=a[t])
break;
s++;
t--;
}
if(s>i+(k-i)/2 && f.length<(k-i+1)){
f.start=b[i];
f.end=b[k];
f.length=k-i+1;
}
}
}*/
//这种方法每一个字母a[i]对应后面的字母a[t]都从i+1个字母开始到结尾j，
//如果a[t]==a[i]就从两端到中间寻找、判断是否为回文
//缺点：太浪费时间
/***********改正后*************/
//从同学处请教到节约时间的算法，就是每个字母a[i]都出发到两端去*/
for(i=0;i<j-1;i++){
if(a[i]==a[i+1]){//偶数个
if(i==0){
f.start=b[i];
f.end=b[i+1];
f.length=2;
}
else{
for(s=i-1,t=i+2;s>=0 && t<j;s--,t++){
if(a[s]!=a[t])
break;
}
if(t-s-1>f.length){
f.start=b[s+1];
f.end=b[t-1];
f.length=t-s-1;
}
}
}
//奇数个
for(s=i-1,t=i+1;s>=0 && t<j;s--,t++){
if(a[s]!=a[t])
break;
}
if(t-s-1>f.length){
f.start=b[s+1];
f.end=b[t-1];
f.length=t-s-1;
}
}
if(!f.length){
printf("1\n%c",a[0]);
return 0;
}
//	l2=clock();
//	printf("%ldms\n",l2-l1);
printf("%d\n",f.length);
for(i=f.start;i<=f.end;i++)
printf("%c",str[i]);
return 0;
}/*11

``````

``````#include<stdio.h>
#include<string.h>
void qail(int o,int e,char ch1[20001],char ch[20001])
{
int t=1,i,j,max=-1,p,q,r=0,w=1,m=0,h;
for (i=0;i<e-1;i++)
{
if (ch1[i]==ch1[i+1]||ch1[i]==ch1[i+1]+32||ch1[i]==ch1[i+1]-32)
{
t++;
w=t;
h=1;
}
else
{
h=0;w=t;
for(j=0;;j++)
{
if ((ch1[i+1+j]==ch1[i-w-j]||ch1[i+1+j]==ch1[i-w-j]+32||ch1[i+1+j]==ch1[i-w-j]-32)&&((i-w-j)>=0&&(i+1+j)<e))
t+=2;
else
break;
}
}
if (t>max)
{
max=t;
p=i-w-j+1;
}
if (!h)
{
t=1;
}
}
printf("%d\n",max);
for (q=0;q<o;q++)
{
if (m>max-1)
break;
if ((ch[q]>='A'&&ch[q]<='Z')||(ch[q]>='a'&&ch[q]<='z'))
r++;
if (r>=p+1)
{
if ((ch[q]>='A'&&ch[q]<='Z')||(ch[q]>='a'&&ch[q]<='z'))
m++;
printf("%c",ch[q]);
}
}
printf("\n");
}
int main()
{
int l,j=0,i,l1,n=0;
char str[20001],str1[20001];
while (scanf("%c",&str[n])!=EOF)
n++;
l=strlen(str);
for (i=0;i<l;i++)
{
if ((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
{
str1[j]=str[i];
j++;
}
}
l1=strlen(str1);
qail(l,l1,str1,str);
return 0;
}
``````