poj_2774 Long Long Message(后缀数组)

Long Long Message
http://poj.org/problem?id=2774
Time Limit: 4000MS Memory Limit: 131072K
Total Submissions: 39564 Accepted: 15673
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.”

题意:找两个数组的最长公共子串
思路:后缀数组的模板题;
先看后缀数组中的每个数组的意义:

sa[i]:排名为i的后缀的位置
rak[i]:从第i个位置开始的后缀的排名,下文为了叙述方便,把从第i个位置开始的后缀简称为后缀i
tp[i]:基数排序的第二关键字,意义与sa一样,即第二关键字排名为i的后缀的位置
tax[i]:i号元素出现了多少次。辅助基数排序
lcp(x,y):字符串x与字符串y的最长公共前缀,在这里指x号后缀与与y号后缀的最长公共前缀
height[i]:lcp(sa[i],sa[i−1]),即排名为i的后缀与排名为i−1的后缀的最长公共前缀
H[i]:height[rak[i]],即i号后缀与它前一名的后缀的最长公共前缀

通过heigh数组的定义我们可以知道把两个数组连起来(中间用特殊符号隔开),
通过找到最长前缀和,就可知道最长公共子串。注意要保证两个子字符串要在两个不同的数组中。
具体内容见代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 200010;
const int maxn2= 10005;
const int inf =0x3f3f3f3f;
char str[maxn];
int l,m,height[maxn];
int tp[maxn],sa[maxn],ran[maxn],tax[maxn];
void sort(){ //基数排序,相比用sort排序要降低时间复杂度
    for(int i=0;i<=m;i++) tax[i]=0;  //清空桶
    for(int i=1;i<=l;i++) tax[ran[i]]++; //装入桶
    for(int i=1;i<=m;i++) tax[i]+=tax[i-1]; //求前缀和
    for(int i=l;i>=1;i--) sa[ tax[ran[tp[i]]]-- ] = tp[i]; //从大到小枚举第二关键词位置为I时他的排名
}
void get_sa(){
    m=255;
    for(int i=1;i<=l;i++) ran[i]=str[i]-'0'+1,tp[i]=i;
    sort();
    for(int w=1,p=0;p<l;m=p,w<<=1){  //倍增
        p=0; //此时的p只是一个计数器
        for(int i=1;i<=w;i++) tp[++p]=l-w+i;
        for(int i=1;i<=l;i++) if(sa[i]>w) tp[++p]=sa[i]-w;  //对第二关键词进行排序
        sort();  //利用上一轮的ran更新本轮的sa
        swap(tp,ran);  //此时的tp数组已经无用;
        ran[sa[1]]=p=1;
        for(int i=2;i<=l;i++){
            ran[sa[i]]=(tp[sa[i-1]]==tp[sa[i]]&&tp[sa[i-1]+w]==tp[sa[i]+w])?p:++p;
        }
    }
    int k=0,j;  //通过sa数组来获得height数组
    for(int i=1;i<=l;i++) ran[sa[i]]=i;
    for(int i=1;i<=l;i++){
        if(k) k--;
        j=sa[ran[i]-1];
        while(str[i+k]==str[j+k]) k++;
        height[ran[i]]=k;
    }
}
int main() {
    while(~scanf("%s",str+1)){
        l=strlen(str+1);int n=l;
        str[++l]='1';
        scanf("%s",str+1+l);
        l=strlen(str+1);
        str[++l]='0';
        get_sa();
        int ans=0;
        for(int i=1;i<=l;i++){
            if((sa[i]<=n&&sa[i-1]>n)||(sa[i]>n&&sa[i-1]<=n))
                ans=max(ans,height[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值