USACO Section 1.3 Calf 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

题意:给定一个字符串以文件结束,可能有很多行,求最大回文串,只比较字母,忽略大小写和其他符号。
分析:有两个难点
第一:输入,就看做是一个案例,以文件结束。
第二:传统DP是有问题的
if (str[i]==str[i-d[i-1]-1]) d[i]=d[i]+2;
else if (str[i]==str[i-2]) d[i]=3;
else if (str[i]==strp[i-1]) d[i]=2;
else d[i]=1;
如:a b a c a b a c a b a
d[i] 1 1 3 1 3 5 7 1 3 5 7
实际: 1 1 3 1 3 5 7 5 7 9 11
故这个DP方程是行不通的。
直接枚举,从中间向两边枚举,分两种情况,基数个或者偶数个。
View Code
  1 /*
  2 ID: dizzy_l1
  3 LANG: C++
  4 TASK: calfflac
  5 */
  6 #include<cstdio>
  7 #include<algorithm>
  8 #include<fstream>
  9 #include<iostream>
 10 #include<string>
 11 #define N 2010
 12 
 13 using namespace std;
 14 
 15 bool is_alp(char c)
 16 {
 17     if(('a'<=c&&c<='z')||('A'<=c&&c<='Z'))
 18         return true;
 19     return false;
 20 }
 21 
 22 bool judge(char c1,char c2)
 23 {
 24     if(c1==c2) return true;
 25     if(c1-32==c2) return true;
 26     if(c1+32==c2) return true;
 27     return false;
 28 }
 29 
 30 int main()
 31 {
 32     freopen("calfflac.in","r",stdin);
 33     freopen("calfflac.out","w",stdout);
 34     string str;char ch;
 35     int i,j,k,len,t,ans_max,ans_i,ans_j;
 36     while(scanf("%c",&ch)!=EOF)
 37         str+=ch;
 38     len=str.length();
 39     ans_max=1;
 40     ans_i=0;
 41     for(k=1;k<len;k++)//基数
 42     {
 43         i=k-1;j=k+1;
 44         t=1;
 45         if(!is_alp(str[k]))continue;//这里卡我一下 a\na 答案就是3  (\n表示换行)
 46          while(i>=0&&j<len)
 47         {
 48             if(!is_alp(str[i]))
 49             {
 50                 i--;continue;
 51             }
 52             if(!is_alp(str[j]))
 53             {
 54                 j++;continue;
 55             }
 56             if(judge(str[i],str[j]))
 57             {
 58                 t+=2;i--;j++;
 59             }
 60             else break;
 61         }
 62         if(ans_max<t)
 63         {
 64             i++;j--;
 65             while(!is_alp(str[i])) i++;
 66             while(!is_alp(str[j])) j--;
 67             ans_max=t;
 68             ans_i=i;
 69             ans_j=j;
 70         }
 71     }
 72     for(k=1;k<len;k++)//偶数
 73     {
 74         i=k-1;j=k;
 75         t=0;
 76         while(i>=0&&j<len)
 77         {
 78             if(!is_alp(str[i]))
 79             {
 80                 i--;continue;
 81             }
 82             if(!is_alp(str[j]))
 83             {
 84                 j++;continue;
 85             }
 86             if(judge(str[i],str[j]))
 87             {
 88                 i--;j++;t+=2;
 89             }
 90             else break;
 91         }
 92         if(ans_max<t)
 93         {
 94             i++;j--;
 95             while(!is_alp(str[i])) i++;
 96             while(!is_alp(str[j])) j--;
 97             ans_max=t;
 98             ans_i=i;
 99             ans_j=j;
100         }
101     }
102     cout<<ans_max<<endl;
103     for(i=ans_i;i<=ans_j;i++)
104     {
105         cout<<str[i];
106     }
107     cout<<endl;
108     return 0;
109 }

 

转载于:https://www.cnblogs.com/zhourongqing/archive/2012/05/24/2516857.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值