usaco6.2.1 Calf flac

来源:互联网 发布:空气净化器选购 知乎 编辑:程序博客网 时间:2024/05/16 23:45

一 原题

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)

11Madam, I'm Adam


二 分析

给定一个长度不超过20000的字符串,找出其中最长的回文串,回文串长度不超过2000。枚举中间字符,向两边扩展。因为最后少输出一个换行WA了一次。


三 代码

运行结果:
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!


AC代码:
/*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;}


原创粉丝点击