Calf Flac
来源:互联网 发布:苹果办公软件下载 编辑:程序博客网 时间:2024/06/06 06:58
题目:
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
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
题意:
这个题就是说从一句话中找出最长的回文段(去掉空格,标点之后)。若有多个最长的输出第一个。输出时要求输出的是原文,也就是不去掉符号之前的文章。
思路:
刚开始想用找到每一个相同的字母,然后往两侧在找,然后超时了。
后来想还不如直接从一个字母往两侧去找,但涉及到的问题是不确定次回文串是奇数还是偶数个长度,想到了一个比较简便的方法,就是从第K个字母往后找一直找到和它不相同的另一个字母(例为第K+9个),这时在开始用第K+9个字母和第K-1个字母比较,然后再依次往两侧拓展着比较。这样就不用考虑回文串是奇数还是偶数长度了。特殊的数据也是可以的。比如有人举得aaa,cbbabbab,axazzaxazza。另外通过这题我又更深的理解了EOF,什么叫end of file
代码:
/*ID:fyhwyx1PROG:calfflacLANG:C*/#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,f=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(){ freopen("calfflac.in","r",stdin); freopen("calfflac.out","w",stdout); 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;}
0 0
- Calf Flac
- Calf Flac
- Calf Flac
- Calf Flac
- Calf Flac
- Calf Flac
- Calf Flac
- Calf Flac(USACO)
- Chapter 1 Calf Flac
- USACO 1.3-Calf Flac
- Section 1.3 Calf Flac
- [USACO] Calf Flac
- usaco Calf Flac
- USACO 1.3 Calf Flac
- USACO 1.3 Calf Flac
- usaco Calf Flac
- USACO:Calf Flac
- USACO Calf Flac
- Android 一个apk多个ICON执行入口
- JAVA学习之面试题2
- Struts2类型转换
- 在win7系统硬盘安装Fedora20
- 悲剧的root
- Calf Flac
- Image uImage与zImage的区别【转】
- java Structs 介绍
- HDU 2296 Ring(AC自动机+DP)
- 3.1 python中的数据类型 [python入门教程]
- Ubuntu 必备
- 马年学习Nginx/Node.js/websocket
- 高精度运算——A mod B
- 3.2 python中的运算符 [python入门教程]