zoukankan      html  css  js  c++  java
  • USACO1.3.3Calf Flac

    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

    吐槽:被这题坑死了。。。先是手贱把记录字符位置的数组也定义成了字符类型的,由于字符本身也是整型,小数据肯定没问题。。。所以样例过了。。。改正之后重新提交,只过了五组,其余的莫名其妙的错了。。。搞了好久才发现是记录位置的数组开小了。。。只开到2005,没看清题目,题目说的是最长回文串长度不超过2000,我以为是过滤非字母字符之后的长度。。。
    题解:暴力搜索。。。枚举每一个点作为中心,然后从两边扩展。可以定义一个数组用于记录过滤后的字符串,并用一个数组记录过滤前字母的位置,然后进行枚举,要分奇偶两种情况进行,假设当前中心为i,那么奇数就是从左位置为i-1,右位置为i+1进行扩展,偶数从左位置i,右位置i+1进行扩展。
    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:calfflac
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 #include<string.h>
     8 #include<ctype.h>
     9 char a[20005],b[20005];
    10 int maxlen,len,left,right,c[20005];
    11 void solve(int l,int r)
    12 {
    13     int ll;
    14     while(l>=0&&r<=len&&b[l]==b[r])
    15     {
    16         l--;
    17         r++;
    18     }
    19     ll=r-l-1;
    20     if(ll>maxlen)
    21     {
    22         maxlen=ll;
    23         left=c[l+1];
    24         right=c[r-1];
    25     }
    26 }
    27 int main(void)
    28 {
    29     freopen("calfflac.in","r",stdin);
    30     freopen("calfflac.out","w",stdout);
    31     int i,m;
    32     m=0;
    33     len=-1;
    34     maxlen=-10000;
    35     while(scanf("%c",&a[m++])!=EOF)
    36         if(isalpha(a[m-1]))
    37         {
    38             len++;
    39             b[len]=toupper(a[m-1]);
    40             c[len]=m-1;
    41 
    42         }
    43     for(i=1; i<len; i++)
    44     {
    45         solve(i-1,i+1);
    46         solve(i,i+1);
    47     }
    48     printf("%d\n",maxlen);
    49     for(i=left; i<=right; i++)
    50         printf("%c",a[i]);
    51     printf("\n");
    52     return 0;
    53 
    54 }
     
  • 相关阅读:
    0607pm克隆&引用类&加载类&面向对象串讲&函数重载
    0607am抽象类&接口&析构方法&tostring&小知识点
    静态
    面向对象--继承和多态
    面向对象的三个特性:封装
    ALV可输入状态下输入金额字段变小数的问题
    退出程序是跳过屏幕自检 比如 必输 EXIT-COMMAND
    ALV的报表对用户定义格式的控制(ALV I_SAVE)
    获利能力分析COPA的BAPI:BAPI_COPAACTUALS_POSTCOSTDATA 通过增强返回凭证号
    一个使用CDS VIEW 的 DEMO
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2863397.html
Copyright © 2011-2022 走看看