• USACO 6.2 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
    

     ————————————————题解

    话说样例什么鬼……?

    Manacher最长回文子串匹配算法,简单易写

    就是USACO上isalpha函数不好用????

    /*
    ID: ivorysi
    LANG: C++
    PROG: calfflac
    */
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <set>
    #include <vector>
    #include <algorithm>
    #define siji(i,x,y) for(int i=(x);i<=(y);++i)
    #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
    #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
    #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
    #define inf 0x5f5f5f5f
    #define ivorysi
    #define mo 97797977
    #define hash 974711
    #define base 47
    #define fi first
    #define se second
    #define pii pair<int,int>
    #define esp 1e-8
    typedef long long ll;
    using namespace std;
    char str[40005],cmp[40005];
    int l,cnt,pos[40005];
    void init() {
        char t[105];
        t[81]='';
        while(fgets(t+1,80,stdin)!=NULL) {
            strcat(str+1,t+1);
            //str[strlen(str+1)]='';
        }
        l=strlen(str+1);
        cmp[0]='%';
        cmp[++cnt]='@';
        siji(i,1,l) {
            if(str[i]>='a' && str[i] <='z') {
                cmp[++cnt]=str[i];
                pos[cnt]=i;
                cmp[++cnt]='@';
            }
            else if(str[i]>='A' && str[i] <='Z') {
                cmp[++cnt]=str[i]-'A'+'a';
                pos[cnt]=i;
                cmp[++cnt]='@';
            }
        }
        cmp[cnt+1]='%';
        cmp[cnt+2]='';
    }
    int p[40005],id,mx;
    void solve() {
        init();
        siji(i,1,cnt) {
            if(mx>i) {
                p[i]=min(p[id*2-i],mx-i+1);
                //要么就是对称的部分,如果它对称点长度超过了最大回文串的左边界,那就用右边界减去i
            }
            else {
                p[i]=1;
            }
            for(;cmp[i+p[i]]==cmp[i-p[i]];++p[i]) ;
            if(mx<i+p[i]-1) {
                mx=i+p[i]-1;
                id=i;
            }
        }
        id=1;
        siji(i,1,cnt) {
            if(p[id]<p[i]) id=i;
        }
        int ansl,ansr;
        if(cmp[id+p[id]-1]!='@') ansr=pos[id+p[id]-1];
        else  ansr=pos[id+p[id]-2];
        str[ansr+1]='';
        if(cmp[id-p[id]+1]!='@') ansl=pos[id-p[id]+1];
        else ansl=pos[id-p[id]+2];
        printf("%d
    %s
    ",p[id]-1,str+ansl); 
    }
    int main(int argc, char const *argv[])
    {
    #ifdef ivorysi
        freopen("calfflac.in","r",stdin);
        freopen("calfflac.out","w",stdout);
    #else
        freopen("f1.in","r",stdin);
    #endif
        solve();
        return 0;
    }
  • 相关阅读:
    Oracle查看锁表并杀死进程
    Oracle查看表空间、是否开启扩展
    Linux日常命令记录
    【转载】LUbuntu,Ubuntu下设置自定义桌面分辨率
    windows下查找端口并杀死进程
    【转载】JAVA中IO流操作的基本规律总结
    【转载】hashCode()、equals()以及compareTo()方法的理解
    RuntimeException、Exception联系区别
    Java内存、数组
    【概念理解】同步异步、阻塞非阻塞
  • 原文地址:https://www.cnblogs.com/ivorysi/p/6622665.html
Copyright © 2020-2023  润新知