Long Long Message POJ - 2774 (后缀数组 + RMQ)

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

题意:给你两个字符串,求出串1和串2最长相同字串的长度。
思路:我们把两个串链接在一起,设串1长度为 len , 两个串总长度为 n ,求出后缀数组, 从 1 开始遍历到 n, 如果 Sa[i] <= len, 则将 i 放入一个数组 que, 之后枚举 que,我们假定排名为 i 和 j 的后缀的起点是串1的字符,
排名 i + 1 到 j - 1 的后缀起点是串2的字符, 那么sufixx[Sa[k]] (\(i + 1 \leq k \leq j - 1\)) 与串1中的任意后缀的LCP中的最大值,一定等于 min(len - Sa[i] + 1, LCP(Sa[i], Sa[k])) 或 min(len - Sa[j] + 1, LCP(Sa[j], Sa[k]))
任意两个后缀的 LCP 可用 RMQ 求出, 总的时间复杂度为 o(len * log(n))。

#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <math.h>
#include <string.h>
#include <map>
#include <iostream>
using namespace std;
const int maxn = 5e5 + 50;
const int mod = 20090717;
int INF = 1e9;
typedef pair<int, int> pii;
#define fi first
#define se second
int Sa[maxn], Height[maxn], Tax[maxn], Rank[maxn], tp[maxn], a[maxn], n, m;
char str[maxn];

void Rsort(){
    for(int i = 0; i <= m; i++) Tax[i] = 0;
    for(int i = 1; i <= n; i++) Tax[Rank[tp[i]]]++;
    for(int i = 1; i <= m; i++) Tax[i] += Tax[i - 1];
    for(int i = n; i >= 1; i--) Sa[Tax[Rank[tp[i]]]--] = tp[i];
}

int cmp(int *f, int x, int y, int w){
    if(x + w > n || y + w > n) return 0; // 注意防止越界,多组输入的时候这条必须有
    return f[x] == f[y] && f[x + w] == f[y + w];
}

void Suffix(){
    for(int i = 1; i <= n; i++) Rank[i] = a[i], tp[i] = i;
    m = 200, Rsort();
    for(int w = 1, p = 1, i; p < n; w += w, m = p){
        for(p = 0, i = n - w + 1; i <= n; i++) tp[++p] = i;
        for(i = 1; i <= n; i++) if(Sa[i] > w) tp[++p] = Sa[i] - w;
        Rsort(), swap(Rank, tp), Rank[Sa[1]] = p = 1;
        for(int i = 2; i <= n; i++)  Rank[Sa[i]] = cmp(tp, Sa[i], Sa[i - 1], w) ? p : ++p;
    }
    int j, k = 0;
    for(int i = 1; i <= n; Height[Rank[i++]] = k){
        for(k = k ? k - 1 : k, j = Sa[Rank[i] - 1]; a[i + k] == a[j + k]; ++k);
    }
}

int dpmi[maxn][60];
void RMQ(){
    for(int i = 1; i <= n; i++){
        dpmi[i][0] = Height[i];
    }
    for(int j = 1; (1 << j) <= n; j++){
        for(int i = 1; i + (1 << j) - 1 <= n; i++){
            dpmi[i][j] = min(dpmi[i][j - 1], dpmi[i + (1 << (j - 1))][j - 1]);
        }
    }
}

int QueryMin(int l, int r){
    int k = log2(r - l + 1);
    return min(dpmi[l][k], dpmi[r - (1 << k) + 1][k]);
} 

int QueryLcp(int i, int j){
    if(i > j) swap(i, j);
    i++;
    return QueryMin(i, j);
}

int que[maxn], bit[maxn];
int main(int arg, char const *argv[])
{
    scanf("%s", str + 1);
    int len = strlen(str + 1);
    for(int i = 1; i <= len; i++) bit[i] = 1; 
    scanf("%s", str + len + 1);
    n = strlen(str + 1);
    for(int i = len + 1; i <= n; i++) bit[i] = 0;
    for(int i = 1; i <= n; i++){
        a[i] = str[i];
    }
    Suffix();
    RMQ();
    int cnt = 0;
    int ans = 0;
    for(int i = 1; i <= n; i++){
        if(Sa[i] <= len) {
            que[++cnt] = i;
        }
    }
    for(int i = 1; i <= cnt; i++){
        for(int j = que[i - 1] + 1; j < que[i]; j++){
            int res = QueryLcp(que[i - 1], j);
            res = min(len - Sa[que[i - 1]] + 1, res);
            ans = max(ans, res);

            res = max(ans, QueryLcp(j, que[i]));
            res = min(len - Sa[que[i]] + 1, res);
            ans = max(ans, res);
        }
    }
    printf("%d\n", ans);
    return 0;
}

posted @ 2020-07-11 09:19  从小学  阅读(106)  评论(0编辑  收藏  举报