zoukankan      html  css  js  c++  java
  • POJ 3974 Palindrome

    D - Palindrome
    Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Description

    Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?" 

    A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not. 

    The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!". 

    If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

    Input

    Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity). 

    Output

    For each test case in the input print the test case number and the length of the largest palindrome. 

    Sample Input

    abcbabcbabcba
    abacacbaaaab
    END

    Sample Output

    Case 1: 13
    Case 2: 6

    求最长回文串的长度。

    (manacher算法)

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #define m(s) memset(s,0,sizeof s);
    using namespace std;
    const int N=1e6+5;
    int l,cas,len,p[N<<1];
    char s[N],S[N<<1];
    void manacher(){
        int ans=0,id=0,mx=-1;
        for(int i=1;i<l;i++){
            if(id+mx>i) p[i]=min(p[id*2-i],id+mx-i);
            while(i-p[i]-1>=0&&i+p[i]+1<=l&&S[i-p[i]-1]==S[i+p[i]+1]) p[i]++;
            if(id+mx<i+p[i]) id=i,mx=p[i];
            ans=max(ans,p[i]);
        }
        printf("Case %d: %d
    ",++cas,ans);
    }
    int main(){
        while(scanf("%s",s)==1){
            if(s[0]=='E') break;
            len=strlen(s);m(p);m(S);
            l=-1;
            for(int i=0;i<len;i++) S[++l]='#',S[++l]=s[i];
            S[++l]='#';
            manacher();
        }
        return 0;
    }

    //====================================================

    //hash有点慢
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    typedef int i64;
    const int N=1e6+5;
    int n,m,cas,ans,a[N<<1];char s[N];
    i64 P,pow[N<<1],hash_l[N<<1],hash_r[N<<1];
    void get_hash(){
        pow[0]=1;hash_r[0]=hash_l[m+1]=0;
        for(int i=1;i<=m;i++) pow[i]=pow[i-1]*P;
        for(int i=1;i<=m;i++) hash_r[i]=hash_r[i-1]*P+a[i];
        for(int i=m;i>=1;i--) hash_l[i]=hash_l[i+1]*P+a[i];
    
    }
    int main(){
        P=29;
        while(scanf("%s",s+1)==1){
            if(s[1]=='E') break;
            n=strlen(s+1);ans=m=0;
            for(int i=1;i<=n;i++){
                a[++m]='#';
                a[++m]=s[i]-'a';
            }
            a[++m]='#';
            get_hash();
            int l,r,mid;
            for(int i=1;i<=m;i++){
                l=0;
                if(i-1<m-i) r=i;
                else r=m-i+1;
                while(r-l>1){
                    mid=l+r>>1;
                    i64 hash_to_l=hash_r[i-1]-hash_r[i-mid-1]*pow[mid];
                    i64 hash_to_r=hash_l[i+1]-hash_l[i+mid+1]*pow[mid];
                    if(hash_to_l==hash_to_r) l=mid;
                    else r=mid;
                }
                ans=max(ans,l);
            }
            printf("Case %d: %d
    ",++cas,ans);
        }
        return 0;
    }
  • 相关阅读:
    (数据结构)十分钟搞定时间复杂度(算法的时间复杂度)
    深入学习二叉树
    我对工作的认识
    mysql系列纠错;
    关于普通指针转换为智能指针的一些问题
    vscode The VS Code Server failed to start
    git 分支管理
    git change_id的理解
    git 指令速查
    c++ make_shared()函数理解 (待整理)
  • 原文地址:https://www.cnblogs.com/shenben/p/5459916.html
Copyright © 2011-2022 走看看