Long Long Message POJ - 2774 后缀数组求公共子串

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/86569345

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

题意:求出两个字符串的最长公共子串。

解题思路:因为字符串的任何的一个子串都是这个字符串的某个后缀的前缀,所以求A和B的最长公共子串等价于求A的后缀和B的后缀的最长公共前缀的最大值。如果枚举A和B的所有后缀,这样做效率会很低。由于要计算A的后缀和B的后缀的最长公共前缀,所以先将第二个字符串写在第一个字符串后面,中间用一个没有出现过的字符隔开,再求出这个新的字符串的后缀数组。不难发现,实际上只有当suffix(sa[i - 1) 和suffi(sa[i])不是同一个字符串的两个后缀时,height[i] 才是满足条件的。而其中的最大值就是答案。算法时间复杂度为O(|A| + |B|) ,由此看来,这是一个非常优秀的算法。

/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std ;
typedef unsigned long long ull ;
typedef long long ll ;
const int Maxn = 2e5 + 10 ;
const int INF = 0x3f3f3f3f ;
const double PI = acos(-1.0) ;
const ull seed = 133 ;

int sa[Maxn],Rank[Maxn],height[Maxn];
int wa[Maxn],wb[Maxn],Ws[Maxn],wv[Maxn];
char s1[Maxn], s2[Maxn];
int n ;

int cmp (int *r,int a,int b,int l){
    return r[a] == r[b] && r[a + l] == r[b + l];
}

void get_sa (int *r,int n,int m){
    int i , j, p , *x = wa,*y = wb,*t;
    for (i = 0; i < m; i++) Ws[i] = 0;
    for (i = 0; i < n; i++) Ws[x[i] = r[i]]++;
    for (i = 1; i < m; i++) Ws[i] += Ws[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--Ws[x[i]]] = i;
    for (j = 1, p = 1; p < n; j *= 2, m = p){
        for (p = 0,i = n - j; i < n; i++) y[p++] = i;
        for (i = 0; i < n; i++) if (sa[i] >= j) y[p++] = sa[i] - j ;
        for (i = 0; i < n; i++) wv[i] = x[y[i]];
        for (i = 0; i < m; i++) Ws[i] = 0;
        for (i = 0; i < n; i++) Ws[wv[i]]++;
        for (i = 0; i < m; i++) Ws[i] += Ws[i - 1];
        for (i = n - 1; i >= 0; i--) sa[--Ws[wv[i]]] = y[i];
        for (t = x, x = y, y = t,p = 1, x[sa[0]] = 0, i = 1; i < n ; i++){
            x[sa[i]] = cmp(y,sa[i - 1],sa[i],j) ? p - 1 : p++;
        }
    }
}
void get_height(int *r,int n){
    int k = 0, j ;
    for (int i = 1 ;i <= n; i++) Rank[sa[i]] = i;
    for (int i = 0; i < n; height[Rank[i++]] = k){
        for (k ? k-- : 0, j = sa[Rank[i] - 1]; r[i + k] == r[j + k]; k++);
    }
}

int a[Maxn] ;
char s[Maxn << 1] ;

int main (){
    cin >> s1 >> s2 ;
    int k = 0 ;
    for (int i = 0; s1[i]; i++) s[k++] = s1[i] ;
    s[k++] = '{' ;
    for (int i = 0; s2[i]; i++) s[k++] = s2[i] ;
//    cout << s << endl ;
    int len = strlen(s) ;
    for (int i = 0; i < len; i++) a[i] = s[i] ;
    a[len] = 0 ;
    get_sa(a, len + 1, 300) ;
    get_height(a, len) ;
    int n = strlen(s1) ;
    int ans = 0 ;
    for (int i = 2; i < len; i++){
        if (height[i] > ans){
            if (sa[i - 1] >= 0 && sa[i - 1] < n && sa[i] >= n ) ans = max(ans, height[i]) ;
            if (sa[i] >= 0 && sa[i] < n && sa[i - 1] >= n) ans = max(ans, height[i]) ;
        }

    }
    cout << ans << endl ;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/86569345