USACO Section 1.3.5 Calf Flac

题目

<center style="font&#45;family&#58;&#39;文泉驿微米黑&#39;&#59;font&#45;size&#58;14px&#59;"> Calf Flac
</center>

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

思路

找回文串,首先想到便是枚举中间,向两头扩展。

这个题目麻烦的地方在于有很多干扰的字符,这时处理的方法有两种:

第一在最开始把那些垃圾字符放一边,最后输出的时候补上。

第二最开始不用处理,而是扩展的时候进行判断是不是合法字符。

听上去第二种简单一点,但是真正写起来第一种的优势就很明显了,因为第二种在枚举的时候要判断,扩展的时候要判断,如果是复制粘贴的代码的话一处错了还得改别处。

总之很蛋疼。我两种方法都写了,但是第一种代码短了差不多20行。下面贴的也是第一种的。

代码

/*
ID: zhrln1
LANG: C++
TASK: calfflac
*/
#include <cstdio>
char s[22222],ss[22222],ch;
int l,ls,b[22222];
int trun(char c){
	if (c>='a' && c<='z') return c;
	if (c>='A' && c<='Z') return c+'a'-'A';
	return 0;
}
int main(){
	freopen("calfflac.in","r",stdin);
	freopen("calfflac.out","w",stdout);
	ss[0]=s[0]='#';
	while (scanf("%c",&ch)!=EOF){
		ss[++ls]=ch;
		if (trun(ch)) {
			s[++l]=trun(ch);
			b[l]=ls;
        }
	}
	int ans=1,ansi=1,j1,j2;
	for (int i(2);i<=l;i++){
		for (j1=i,j2=i;j1>1 && j2<l && s[j1-1]==s[j2+1];j1--,j2++);
		if (j2-j1+1>ans) {
			ans=j2-j1+1;
			ansi=j1;
		}
		if (s[i]!=s[i+1]) continue;
		for (j1=i,j2=i+1;j1>1 && j2<l && s[j1-1]==s[j2+1];j1--,j2++);
		if (j2-j1+1>ans){
			ans=j2-j1+1;
			ansi=j1;
		}
	}
	printf("%d\n",ans);
	int p=0;
	for (int i(b[ansi]);p<ans;i++){
		printf("%c",ss[i]);
		if (trun(ss[i])) p++;
	}
	printf("\n");
	return 0;
}


全部评论

相关推荐

03-26 13:04
已编辑
电子科技大学 算法工程师
xiaowl:你这个简历“条目上”都比较有深度性,但是实际上面试官又没法很好的评估你是怎么达到很多看上去很厉害的结果的。要避免一些看上去很厉害的包装,比如高效的内存复用策略的表达,如果仅是简单的一些内存共享机制,而且面试上也没有深挖的空间,就不要这样表达。比如,工程化模式本质上可能就是定义了一些abstract class,那也就没特别多值得讲的内容。建议简历上应该侧重那些你花了大量时间和精力解决、研究的问题,不要过分追求“丰富”,而是关注在技术深入度、问题解决能力的表现上。
没有实习经历,还有机会进...
点赞 评论 收藏
分享
03-10 10:57
已编辑
门头沟学院 推荐算法
夜夜还好:我们学校说为了学生就业,更新了课程,我今天大二,上学期在学jsp,html,这学期上来工程实践,要求用springboot+vue,说什么这些技术要我们提前自己准备,要不你把学费还我吧,我给b站充个会员,人家教的比你多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务