usaco 1.3.3( calfflac )

题目:

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

题意其实还是蛮好懂,就是求最长回文子串,输出(可还是卡了或多天)。最初是枚举子串起点终点,但是只能过几组样例。后来看题是输入不对(输入要求是一行或多行),改了后还是发现过不了,后来看了下解题报告,说是枚举每一个位置(中心可以是一个也可以是两个),向两边扩散看能达到最大的子串是多长。于是就改了,但还是错了好几次。
最近电脑又黑屏了,显示是盗版系统导致很多软件都不能用(包括豌豆荚,以及编程软件,有高手看了能帮我一下么,哎,瞎吐槽看来没用啊)
代码:
/*
ID: 614433244
LANG: C++
TASK: calfflac
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int ans,i,p,h1,h2,len,j,anslen;
char s[20010],str[20010];
int turn(char c)
{
     if (c>='A' && c<='Z') return c-'A';
     if (c>='a' && c<='z') return c-'a';
     return -1;
}
int main()
{
     freopen("calfflac.in","r",stdin);
     freopen("calfflac.out","w",stdout);
     len=0;
     while (gets(str))
     {
          p=strlen(str);
          for (i=0;i<p;i++) s[i+len]=str[i];
          len+=p;  s[len]='\n'; len++;
     }
     ans=0;
     for (i=0;i<len;i++)
     {
           p=1;
           h1=h2=i;
           while (h1>=0 && turn(s[h1])==-1) h1--;
           while (h2<len && turn(s[h2])==-1) h2++;
           while (h1>=0 && h1<len && turn(s[h1])==turn(s[h2]))
           {
                 if (p>ans)
                 {
                      ans=p;
                      for (j=h1;j<=h2;j++) str[j-h1]=s[j];
                      anslen=h2-h1+1;
                 }
                 h1--;
                 while (h1>=0 && turn(s[h1])==-1) h1--;
                 h2++;
                 while (h2<len && turn(s[h2])==-1) h2++;
                 p+=2;
           }
           p=2;
           h1=i; h2=i+1;
           while (h1>=0 && turn(s[h1])==-1) h1--;
           while (h2<len && turn(s[h2])==-1) h2++;
           while (h1>=0 && h1<len && turn(s[h1])==turn(s[h2]))
           {
                 if (p>ans)
                 {
                      ans=p;
                      for (j=h1;j<=h2;j++) str[j-h1]=s[j];
                      anslen=h2-h1+1;
                 }
                 h1--;
                 while (h1>=0 && turn(s[h1])==-1) h1--;
                 h2++;
                 while (h2<len && turn(s[h2])==-1) h2++;
                 p+=2;
           }
     }
     printf("%d\n",ans);
     for (i=0;i<anslen;i++)
     {
           if (str[i]!='\n')  printf("%c",str[i]);
           else printf("\n");
     }
     printf("\n");
     return 0;
}

转载于:https://www.cnblogs.com/rolyxiao/archive/2012/07/07/2580713.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值