• poj 2774 后缀数组 两个字符串的最长公共子串


    Long Long Message
    Time Limit: 4000MS   Memory Limit: 131072K
    Total Submissions: 31904   Accepted: 12876
    Case Time Limit: 1000MS

    Description

    The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother. 

    The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out: 

    1. All characters in messages are lowercase Latin letters, without punctuations and spaces. 
    2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. 
    3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. 
    E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. 
    4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different. 

    You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat. 

    Background: 
    The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be. 

    Why ask you to write a program? There are four resions: 
    1. The little cat is so busy these days with physics lessons; 
    2. The little cat wants to keep what he said to his mother seceret; 
    3. POJ is such a great Online Judge; 
    4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( 

    Input

    Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

    Output

    A single line with a single integer number – what is the maximum length of the original text written by the little cat.

    Sample Input

    yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
    yeaphowmuchiloveyoumydearmother
    

    Sample Output

    27

    Source

    POJ Monthly--2006.03.26,Zeyuan Zhu,"Dedicate to my great beloved mother."
    题意:
    给定两个字符串 A 和 B,求最长公共子串。
    代码:
    //论文题,一个字符串的最长重复子串就是heigh数组的最大值,这里把后一个字符串接到前一个的前面中间用一个字符分开,然后求不在
    //同一个字符串中的最大的heigh数组值。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int MAXN=300000;
    int sa[MAXN+9],he[MAXN+9],ra[MAXN+9],xx[MAXN+9],yy[MAXN+9],buc[MAXN+9],f[MAXN+9][30];
    char s[MAXN+9];
    int len,m;
    void get_suf()
    {
        int *x=xx,*y=yy;
        for(int i=0;i<m;i++) buc[i]=0;
        for(int i=0;i<len;i++) buc[x[i]=s[i]]++;
        for(int i=1;i<m;i++) buc[i]+=buc[i-1];
        for(int i=len-1;i>=0;i--) sa[--buc[x[i]]]=i;
        for(int k=1;k<=len;k<<=1){
            int p=0;
            for(int i=len-1;i>=len-k;i--) y[p++]=i;
            for(int i=0;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
            for(int i=0;i<m;i++) buc[i]=0;
            for(int i=0;i<len;i++) buc[x[y[i]]]++;
            for(int i=1;i<m;i++) buc[i]+=buc[i-1];
            for(int i=len-1;i>=0;i--) sa[--buc[x[y[i]]]]=y[i];
            swap(x,y);
            p=1;x[sa[0]]=0;
            for(int i=1;i<len;i++){
                if(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])
                    x[sa[i]]=p-1;
                else x[sa[i]]=p++;
            }
            if(p>=len) break;
            m=p;
        }
        for(int i=0;i<len;i++) ra[sa[i]]=i;
        int k=0;
        for(int i=0;i<len;i++){
            if(ra[i]==0) { he[0]=0; continue; }
            if(k) k--;
            int j=sa[ra[i]-1];
            while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
            he[ra[i]]=k;
        }
    }
    int solve(int len1)
    {
        int ans=0;
        for(int i=1;i<len;i++){
            if(he[i]>ans&&((sa[i]>len1&&sa[i-1]<len1)||(sa[i]<len1&&sa[i-1]>len1)))
                ans=he[i];
        }
        return ans;
    }
    int main()
    {
        scanf("%s",s);
        int len1=strlen(s);
        s[len1]='#';
        scanf("%s",s+len1+1);
        len=strlen(s);
        m=200;
        get_suf();
        printf("%d
    ",solve(len1));
        return 0;
    }
  • 相关阅读:
    在centos7 ubuntu15.04 上通过bosh-lite 搭建单机环境cloudfoundry
    Linux里如何查找文件内容
    StarUML2 建模工具全平台破解及license验证简要分析
    深入理解 Neutron -- OpenStack 网络实现(4):网络名字空间
    深入理解 Neutron -- OpenStack 网络实现(3):VXLAN 模式
    深入理解 Neutron -- OpenStack 网络实现(2):VLAN 模式
    深入理解 Neutron -- OpenStack 网络实现(1):GRE 模式
    linux 设置pip 镜像 Pip Warning:–trusted-host 问题解决方案
    C# IO流的操作(一)
    C# 序列化、反序列化
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/7617953.html
Copyright © 2020-2023  润新知