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;
    }
  • 相关阅读:
    Pandas+Numpy 数据中空值的处理操作:判断、查找、填充及删除
    跑新项目时遇到的报错及解决方案
    Java Stream流排序null以及获取指定条数数据
    通信端口Com口被占用的原因分析
    查询sq字段逗号分隔的方式
    IIS及时回收
    oracle中创建sequence指定起始值
    js 面向对象代码
    C# 将html实体编码转换到正常字符 & #40;格式
    DataTable列查询加排序
  • 原文地址:https://www.cnblogs.com/shenben/p/5459916.html
Copyright © 2011-2022 走看看