zoukankan      html  css  js  c++  java
  • usaco1.3Calf Flac(枚举)

    就是找出最长的一个回文串 。。 一开始用了个朴素的dp。。结果应该是内存爆了开不了那么大数组。 然后就暴力枚举了回文的中间数字。过了。。感觉还是太低效

    题目:

    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
    


    代码:

      1 /*
      2 ID:doubles3
      3 PROB:calfflac
      4 LANG:C++
      5 */
      6 #include<iostream>
      7 #include<cstdlib>
      8 #include<stdio.h>
      9 #include<fstream>
     10 #include<math.h>
     11 #include <algorithm>
     12 #include<string.h>
     13 #include<string>
     14 #include<vector>
     15 using namespace std;
     16 
     17 #define MAX 20000+100
     18 char str[MAX];
     19 vector< pair<char,int> > want;
     20 int len;
     21 void input()
     22 {
     23     int pstr=0;
     24     char c;
     25     while( scanf("%c",&c)!=EOF)
     26     {
     27             str[pstr++] = c;
     28     }
     29    for(int i=0;i<strlen(str);i++)
     30    {
     31        if( str[i]>='a'&&str[i]<='z' || str[i] >='A' &&str[i]<='Z')
     32        {
     33             want.push_back( make_pair( tolower(str[i]), i  ));
     34        }
     35    }
     36    len = want.size();
     37 }
     38 //int dp[MAX][MAX];
     39 int ans, LEFT,RIGHT;
     40 //int solve(int L,int R)
     41 //{
     42 //    int &d = dp[L][R];
     43 //    if(d) return d;
     44 //    if( L==R) return 1;
     45 //    if(L>R) return 0;
     46 //
     47 //    if( want[L].first == want[R].first)
     48 //        d = solve( L+1,R-1)+2;
     49 //    else
     50 //        d = max(solve(L+1,R ), solve(L,R-1));
     51 //
     52 //    if( d>ans)
     53 //    {
     54 //        LEFT = L;
     55 //        RIGHT = R;
     56 //        ans = d;
     57 //    }
     58 //    return d;
     59 //}
     60 int burst(int l,int r)
     61 {
     62 
     63     if(l<0 || r>len-1)return 0;
     64 
     65     if(want[l].first != want[r].first)return 0;
     66 
     67     while( l-1>=0 && r+1< len &&want[l-1].first == want[r+1].first)
     68     {
     69             l-- ; r++;
     70     }
     71     l = max(0,l);
     72     r = min( r, len-1);
     73     if( r-l+1 > ans)
     74     {
     75             ans = r-l+1;
     76             LEFT = l;
     77             RIGHT = r;
     78 
     79     }
     80     return 0;
     81 }
     82 
     83 
     84 int main()
     85 {
     86     freopen("calfflac.in","r",stdin);
     87     freopen("calfflac.out","w",stdout);
     88     input();
     89     //solve(0,len-1);
     90 //    cout<<dp[0][len-1]<<endl;
     91     ans = 0;
     92     for(int i=0;i<len;i++)
     93     {
     94         burst(i,i);
     95         burst(i-1,i);
     96         burst(i,i+1);
     97     }
     98 
     99         cout<<ans<<endl;
    100         for(int i= want[LEFT].second;i<=want[RIGHT].second;i++)
    101         {
    102             cout<<str[i];
    103         }
    104         //cout<<LEFT<<" "<<RIGHT<<endl;
    105     cout<<endl;
    106     return 0;
    107 }
  • 相关阅读:
    PHP7放弃大礼包(微信支付回调签名错误)
    PHP CURL中传递cookie的方法
    php-5.3源码编译autoconf版本不符合解法
    单例模式使用小介绍
    centos源码编译安装nginx过程记录
    PHP语言开发Paypal支付demo的具体实现
    Redis安全与持久化(适合小白阅读)
    mac当你有多个版本的命令存在是怎么使用最新版本
    设置让php能够以root权限来执行exec() 或者 shell_exec()
    git冲突解决
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3536844.html
Copyright © 2011-2022 走看看