usaco6.2.1 Calf flac
来源:互联网 发布:空气净化器选购 知乎 编辑:程序博客网 时间:2024/05/16 23:45
一 原题
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)
11Madam, I'm Adam
二 分析
三 代码
USER: Qi Shen [maxkibb3]TASK: calfflacLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.000 secs, 4292 KB] Test 2: TEST OK [0.000 secs, 4292 KB] Test 3: TEST OK [0.000 secs, 4292 KB] Test 4: TEST OK [0.000 secs, 4292 KB] Test 5: TEST OK [0.000 secs, 4292 KB] Test 6: TEST OK [0.000 secs, 4292 KB] Test 7: TEST OK [0.000 secs, 4292 KB] Test 8: TEST OK [0.028 secs, 4292 KB]All tests OK.Your program ('calfflac') produced all correct answers! This is yoursubmission #2 for this problem. Congratulations!
/*ID:maxkibb3LANG:C++PROB:calfflac*/#include<cstdio>#include<algorithm>const int MAX = 2e4 + 5;char s[MAX], t[MAX];int pos[MAX];int main() { freopen("calfflac.in", "r", stdin); freopen("calfflac.out", "w", stdout); int n1 = 0, n2 = 0; while(scanf("%c", &s[n1]) != -1) { if(s[n1] >= 'A' && s[n1] <= 'Z') { pos[n2] = n1; t[n2++] = s[n1] - 'A' + 'a'; } else if(s[n1] >= 'a' && s[n1] <= 'z') { pos[n2] = n1; t[n2++] = s[n1]; } n1++; } int ans = 0, st, ed; for(int i = 0; i < n2; i++) { int len, i1, i2; int j = 0, b = std::min(i, n2 - 1 - i); while(j <= b) { if(t[i - j] != t[i + j]) break; j++; } i1 = pos[i - j + 1], i2 = pos[i + j - 1]; len = 2 * (j - 1)+ 1; if(len > ans) { ans = len; st = i1, ed = i2; } j = 0, b = std::min(i, n2 - i - 2); while(j <= b) { if(t[i - j] != t[i + 1 + j]) break; j++; } i1 = pos[i - j + 1], i2 = pos[i + j]; if(j == 0) i1 = pos[i], len = 1; else len = 2 * j; if(len > ans) { ans = len; st = i1, ed = i2; } j = 0, b = std::min(i - 1, n2 - 1 - i); while(j <= b) { if(t[i - 1 - j] != t[i + j]) break; j++; } i1 = pos[i - j], i2 = pos[i + j - 1]; if(j == 0) i2 = pos[i], len = 1; else len = 2 * j; if(len > ans) { ans = len; st = i1, ed = i2; } } printf("%d\n", ans); for(int i = st; i <= ed; i++) printf("%c", s[i]); printf("\n"); return 0;}
- usaco6.2.1 Calf flac
- USACO6.2.1 Calf Flac(calfflac)
- 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
- 这六段代码隐藏着深度学习的前世今生!
- python语言 pyspark中dataframe修改列名
- Python3 读取文件内容万金油方法
- andengine_box2d在高版本androd手机上出现问题
- sdnu 1009
- usaco6.2.1 Calf flac
- 设计模式之单例模式
- java编译并打包成可执行jar和exe文件
- 查看域名对应ip
- FreeMarker
- 系统服务的控制
- redhat6.6的nfs服务端配置
- 大数据产品推荐:百融信贷决策审批系统
- 4分钟学会网页样式