USACO SECTION 1.3 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: conicoc1
LANG: C
TASK: calfflac
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int IsEnglish(char text[],int n)
{
	if(  (text[n]>64 &&text[n]<91) || (text[n]>96&&text[n]<123))
		return 1;
	else 
		return 0;
}

int Calf(char text[],int left,int right)
{
	int i;
	int count;
	for(i=0,count=0;left+i<=right;i++)
	{
		if(IsEnglish(text,left+i))
			count++;
	}
	return count;	
}

int main()
{
	FILE *fin,*fout,*ftxt;
	fin=fopen("calfflac.in","r");
	fout=fopen("calfflac.out","w");
	
	char text[20000];
	int left,right;
	int FLeft,FRight;
	
	int TextLength;
	int CalfLength=0;
	int i=0,j;
	int flag=0;
	
	
	while(fscanf(fin,"%c",&text[i])!=EOF)
	{
		i++;
	}
	text[i+1]='\0';
	TextLength=i;
	
//	fprintf(fout,"%s\n",text);
//	printf("%c%c%c\n",text[5],text[6],text[7]);
	for(i=0;i<TextLength-1;i++)
	{
		if(!IsEnglish(text,i))   //如果text[i]不是英文字母,那么continue; 
			continue;
		
//		printf("i=%d:\n",i);	
		j=i+1;
		while(!IsEnglish(text,j))
			j++;
//	printf("j=%d\n",j);
		if(text[i]==text[j]||text[i]+32==text[j]||text[i]-32==text[j]) //比较后一个是字母的字符 
		{
			for(left=i,right=j;left>=0&&right<=TextLength-1;left--,right++)
			{
				while(!IsEnglish(text,left))
					left--;
				while(!IsEnglish(text,right))
					right++;
				if(text[left]!=text[right] && text[left]+32!=text[right] &&text[left]-32!=text[right])
					break;
			}
			if(CalfLength<Calf(text,left+1,right-1))
			{
				CalfLength=Calf(text,left+1,right-1);
				FLeft=left+1;
				FRight=right-1;
			}
		}
		
		j++;
		while(!IsEnglish(text,j))
			j++;
//	printf("j=%d\n",j);
		if(text[i]==text[j]||text[i]+32==text[j]||text[i]-32==text[j])
		{
			for(left=i,right=j;left>=0&&right<=TextLength-1;left--,right++)
			{
				while(!IsEnglish(text,left)&&left>=0)
					left--;
				while(!IsEnglish(text,right)&&right<=TextLength-1)
					right++;
				if(text[left]!=text[right] && text[left]+32!=text[right] &&text[left]-32!=text[right])
					break;
			}
			if(CalfLength<Calf(text,left+1,right-1))
			{
				CalfLength=Calf(text,left+1,right-1);
				FLeft=left+1;
				FRight=right-1;
			}
		}
	}
	
	while(!IsEnglish(text,FLeft))
		FLeft++;
	while(!IsEnglish(text,FRight))
		FRight--;

	fprintf(fout,"%d\n",CalfLength);
	for(i=0;i<=FRight-FLeft;i++)
	{
		fprintf(fout,"%c",text[FLeft+i]);
	}
	fprintf(fout,"\n");
	
	return 0;
}


关于最长回文的问题应该是有动态规划的解法,不过我现在依旧不能很清晰的理解什么是动态规划。。学下去看看

这道题一开始用了超时的算法:依次从大到小寻找回文长度可能为2000至1的字符串,对于每个长度分别从text的开头开始遍历至strlen(text)-回文长度,再在每次遍历中比较。。貌似有O(m*n^2)的时间复杂度。。。

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值