首页 > 编程语言 > 详细

Long Long Message(后缀数组)

时间:2017-06-06 22:57:50      阅读:441      评论:0      收藏:0      [点我收藏+]
Long Long Message
Time Limit: 4000MS   Memory Limit: 131072K
Total Submissions: 30427   Accepted: 12337
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."
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>

using namespace std;

/****************************************后缀数组模板****************************************/
const int maxn=1000000+100;
struct SuffixArray
{
    char s[maxn];
    int sa[maxn];//保存着排序后的后缀
    int rank[maxn];//保存着每个后缀的名词
    int height[maxn];//保存着相邻前缀的最长公共子序列的长度
    int t1[maxn],t2[maxn],c[maxn],n;
    int dmin[maxn][20];
    void build_sa(int m)//m为每个后缀后面加的元素,保证在原字符串中不出现
    {
        int i,*x=t1,*y=t2;
        for(i=0;i<m;i++) c[i]=0;
        for(i=0;i<n;i++) c[x[i]=s[i]]++;
        for(i=1;i<m;i++) c[i]+=c[i-1];
        for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
        for(int k=1;k<=n;k<<=1)
        {
            int p=0;
            for(i=n-k;i<n;i++) y[p++]=i;
            for(i=0;i<n;i++)if(sa[i]>=k) y[p++]=sa[i]-k;
            for(i=0;i<m;i++) c[i]=0;
            for(i=0;i<n;i++) c[x[y[i]]]++;
            for(i=1;i<m;i++) c[i]+=c[i-1];
            for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]] = y[i];
            swap(x,y);
            p=1,x[sa[0]]=0;
            for(i=1;i<n;i++)
                x[sa[i]]= y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]? p-1:p++;
            if(p>=n) break;
            m=p;
        }
    }
    void build_height()//n不能等于1,否则出BUG
    {
        int i,j,k=0;
        for(i=0;i<n;i++)rank[sa[i]]=i;
        for(i=0;i<n;i++)
        {
            if(k)k--;
            j=sa[rank[i]-1];
            while(s[i+k]==s[j+k])k++;
            height[rank[i]]=k;
        }
    }
    void initMin()
    {
        for(int i=1;i<=n;i++) dmin[i][0]=height[i];
        for(int j=1;(1<<j)<=n;j++)
            for(int i=1;i+(1<<j)-1<=n;i++)
                dmin[i][j]=min(dmin[i][j-1] , dmin[i+(1<<(j-1))][j-1]);
    }
    int RMQ(int L,int R)//取得范围最小值
    {
        int k=0;
        while((1<<(k+1))<=R-L+1)k++;
        return min(dmin[L][k] , dmin[R-(1<<k)+1][k]);
    }
    int LCP(int i,int j)//求后缀i和j的LCP最长公共前缀
    {
        int L=rank[i],R=rank[j];
        if(L>R) swap(L,R);
        L++;//注意这里
        return RMQ(L,R);
    }
}sa;
/****************************************后缀数组模板****************************************/

char s[maxn*2+5];

int main(int argc, char *argv[])
{
    // freopen("in.txt","r",stdin);
    scanf("%s",sa.s);
    int n=strlen(sa.s);
    sa.s[n]=1;
    scanf("%s",sa.s+n+1);
    sa.n=strlen(sa.s);
    sa.build_sa(2000);
    sa.build_height();
    int _max=sa.height[0];
    for(int i=1;i<sa.n+1;i++){
        int a=sa.sa[i],b=sa.sa[i-1];
        if(a>b) swap(a,b);
        if( a<n&&b>n ){//属于两个字符串
            _max=max(_max,sa.height[i]);
        }
    }
    printf("%d\n",_max);
    return 0;
}

 

Long Long Message(后缀数组)

原文:http://www.cnblogs.com/wuwangchuxin0924/p/6953907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!