zoukankan      html  css  js  c++  java
  • USACO calf flac

    题意:忽略字母外的符号和大小写,求连续最长回文子串(n<=20000)。

    O(n^2)方法

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cstdlib>
     4 
     5 #define N 20010
     6 
     7 char s1[N], s2[N], s[N];
     8 int dp[4][N], flag[N];
     9 
    10 int is_other(char x)
    11 {
    12     if(x>='A'&&x<='Z' || x>='a'&&x<='z')
    13         return 0;
    14     return 1;
    15 }
    16 
    17 int is_good(char x, char y)
    18 {
    19     if(x<'a')
    20         x += 'a' - 'A';
    21     if(y<'a')
    22         y += 'a' - 'A';
    23     if(x==y)
    24         return 1;
    25     return 0;
    26 }
    27 
    28 int main()
    29 {
    30     FILE *fin = fopen("calfflac.in","r");
    31     FILE *fout = fopen("calfflac.out""w");
    32 
    33     char ch;
    34     int len = 0, len1 = 0;
    35     while((ch = fgetc(fin))!=EOF)
    36         s[len++] = ch;
    37     for(int i=0; i<len; i++)
    38     {
    39         if(is_other(s[i])==1)
    40             continue;
    41         s1[len1] = s[i];
    42         flag[len1] = i;
    43         if(s[i]>='a' && s[i]<='z')
    44             s1[len1] -= 'a' - 'A';
    45         len1++;
    46     }
    47     for(int i=0; i<len1; i++)
    48         s2[i] = s1[len1 - i - 1];
    49     s1[len1] = '';
    50     s2[len1] = '';
    51     //printf("%s %s ",s1, s2);
    52 
    53     memset(dp,0,sizeof(dp));
    54     int maxsubseq = 0, maxindex = 0;
    55     for(int i=0; i<len1; i++)
    56     {
    57         for(int j=0; j<len1; j++)
    58         {
    59             if(s1[i]==s2[j])
    60             {
    61                 if(i==0 || j==0)
    62                     dp[i%2][j] = 1;
    63                 else
    64                     dp[i%2][j] = dp[(i-1)%2][(j-1)] + 1;
    65                 if(maxsubseq < dp[i%2][j])
    66                 {
    67                     maxsubseq = dp[i%2][j];
    68                     maxindex = i - maxsubseq + 1;
    69                 }
    70             }
    71             else
    72                 dp[i%2][j] = 0;
    73             //printf("%d", dp[i%2][j]);
    74         }
    75         //printf(" ");
    76     }
    77     //fprintf(fout,"11 ");
    78     fprintf(fout, "%d ", maxsubseq);
    79     if(maxsubseq>0)
    80         fprintf(fout, "%c",s[flag[maxindex]]);
    81     for(int i=0; i < maxsubseq - 1; i++)
    82     {
    83         int j = maxindex + i;
    84         for(int k=flag[j]+1; k<=flag[j+1]; k++)
    85             fprintf(fout, "%c",s[k]);
    86     }
    87     fprintf(fout, " ");
    88     return 0;
    89 
    90 }
    View Code 

     第8组数据超时,用了1.65s

     方法二:从某字符开始向两边检验,枚举一遍,更新最大长度及初始位置,第8组用时0.065s

    有两种情况:

    1、子串为奇数串(枚举的字符作为中心,不比较)

    2、子串为偶数串 (枚举的字符作为左起第一个,要比较)

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cstdlib>
     4 
     5 #define N 20010
     6 
     7 char s1[N], s2[N], s[N];
     8 int dp[4][N], flag[N];
     9 
    10 int is_other(char x)
    11 {
    12     if(x>='A'&&x<='Z' || x>='a'&&x<='z')
    13         return 0;
    14     return 1;
    15 }
    16 
    17 int is_good(char x, char y)
    18 {
    19     if(x<'a')
    20         x += 'a' - 'A';
    21     if(y<'a')
    22         y += 'a' - 'A';
    23     if(x==y)
    24         return 1;
    25     return 0;
    26 }
    27 
    28 int main()
    29 {
    30     FILE *fin = fopen("calfflac.in","r");
    31     FILE *fout = fopen("calfflac.out""w");
    32 
    33     char ch;
    34     int len = 0, len1 = 0;
    35     while((ch = fgetc(fin))!=EOF)
    36         s[len++] = ch;
    37     for(int i=0; i<len; i++)
    38     {
    39         if(is_other(s[i])==1)
    40             continue;
    41         s1[len1] = s[i];
    42         flag[len1] = i;
    43         if(s[i]>='a' && s[i]<='z')
    44             s1[len1] -= 'a' - 'A';
    45         len1++;
    46     }
    47     s1[len1] = '';
    48     printf("%s ",s1);
    49 
    50     memset(dp,0,sizeof(dp));
    51     int maxsubseq = 0, maxindex = 0;
    52 
    53     for(int i=0; i<len1; i++)
    54     {
    55         int tmpn1 = 0, tmpn2 = 0;
    56         for(int j=1; j<len1; j++)
    57         {
    58             if(i-j<0 || i+j>len1 || s1[i-j] != s1[i+j])
    59                 break;
    60             tmpn1 ++;
    61             if(maxsubseq < tmpn1*2+1)
    62             {
    63                 maxsubseq = tmpn1*2+1;
    64                 maxindex = i - tmpn1;
    65             }
    66         }
    67 
    68         for(int j=0; j<len1; j++)
    69         {
    70             if(i-j<0 || i+1+j>len1 || s1[i-j] != s1[i+1+j])
    71                 break;
    72             tmpn2 ++;
    73             if(maxsubseq < tmpn2*2+1)
    74             {
    75                 maxsubseq = tmpn2*2;
    76                 maxindex = i - tmpn2 + 1;
    77             }
    78         }
    79     }
    80 
    81 
    82     fprintf(fout, "%d ", maxsubseq);
    83     if(maxsubseq>0)
    84         fprintf(fout, "%c",s[flag[maxindex]]);
    85     for(int i=0; i < maxsubseq - 1; i++)
    86     {
    87         int j = maxindex + i;
    88         for(int k=flag[j]+1; k<=flag[j+1]; k++)
    89             fprintf(fout, "%c",s[k]);
    90     }
    91     fprintf(fout, " ");
    92     return 0;
    93 
    94 }
    View Code 
    参考:http://blog.csdn.net/moien_podiene/article/details/7526419

     题目描述:

    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


  • 相关阅读:
    [poj3974] Palindrome 解题报告 (hashmanacher)
    SQL Android
    SharedPreferences Android
    本地广播 localBroadcastManager Android
    RecyclerView Android
    自定义ListView Android
    布局 Android
    传值 Android
    活动的生命周期 Android
    CodeForces 907F Power Tower(扩展欧拉定理)
  • 原文地址:https://www.cnblogs.com/byluoluo/p/3432226.html
Copyright © 2011-2022 走看看