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;
    }
  • 相关阅读:
    回归cnblog
    第一篇博文
    apache 配置网站目录,虚拟目录,新端口
    linux 虚拟机设置IP访问外网
    Volist标签 key值的使用
    php header调试,yii2打log
    Yii2 框架下bootstrap 弹窗预览视频等~
    几个简单的css设置问题:div居中,ul li不换行 ,内容超出自动变省略号等
    鼠标右键弹窗(实现重命名等)的设计
    HTML5 Canvas绘图系列之一:圆弧等基础图形的实现
  • 原文地址:https://www.cnblogs.com/shenben/p/5459916.html
Copyright © 2011-2022 走看看