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;
}


全部评论

相关推荐

牛奶配面包:第二个经典博弈题目吧
点赞 评论 收藏
分享
个人背景:学院二本计科专业&nbsp;大二开始实习个人经历:安克创新&nbsp;、理想汽车、字节跳动碎碎念:我做事只有三分钟热度。看到进了大厂的同学,我会羡慕,也会跟着努力上进;但遇到好看的小说,我又会放下手头的事沉迷其中,之前的坚持也就中断了。我有些自卑,总觉得自己学历和外貌都不够好。之前偶然在网上受到关注,我就喜欢上了上网,因为这里有很多人认可我。但我也很在意别人的评价,偶尔看到嘲讽的言论,会触发我的自卑情绪,让我感到愤怒。有时候我会强硬地回怼,有时候又会懦弱地选择无视。我也有虚荣心。不管是拿到安克、理想还是字节的机会,我在分享的时候都会带着这份心思。我会特意强调自己学历不好,是为了衬托出过程的艰难,以此显得自己更厉害。我知道,人往往会炫耀自己缺少的东西,来掩盖内心的空洞。我总想着走捷径,不太喜欢踏踏实实地做事。找实习的时候,我花了更多时间在研究面试技巧上,而不是提升专业能力。我会反复听面试录音分析技巧,看面试教程学习怎么和不同的面试官沟通,还会每天自言自语练习语言表达,同学都觉得我有点奇怪。我的实习生涯里,侥幸和运气占了很大一部分。我总在想,如果有一天我失去了这份幸运,这些特质可能会让我一蹶不振。ps:&nbsp;很多人会问我学习路线和经验&nbsp;但是就像我上面说的&nbsp;我的实习过程靠的很多是关键节点的运气&nbsp;技术上面我可能不如很多人&nbsp;&nbsp;所以请大家理性求助和理性参考我的回答&nbsp;附上我的投递记录
我的offer在哪里...:从去年看到现在,飞升哥就是榜样
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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