LA 4513 Stammering Aliens

在这道题上学会了两种方法解决。一种是用哈希lcp法,另一种用后缀数组求解。
Dr. Ellie Arroway has established contact with an extraterrestrial civilization. However, all efforts to decode their messages have failed so far because, as luck would have it, they have stumbled upon a race of stuttering aliens! Her team has found out that, in every long enough message, the most important words appear repeated a certain number of times as a sequence of consecutive characters, even in the middle of other words. Furthermore, sometimes they use contractions in an obscure manner. For example, if they need to say bab twice, they might just send the message babab, which has been abbreviated because the second b of the first word can be reused as the first b of the second one.
Thus, the message contains possibly overlapping repetitions of the same words over and over again. As a result, Ellie turns to you, S.R. Hadden, for help in identifying the gist of the message.
Given an integer m, and a string s, representing the message, your task is to find the longest substring of s that appears at least m times. For example, in the message baaaababababbababbab, the length-5 word babab is contained 3 times, namely at positions 5, 7 and 12 (where indices start at zero). No substring appearing 3 or more times is longer (see the first example from the sample input). On the other hand, no substring appears 11 times or more (see example 2).
In case there are several solutions, the substring with the rightmost occurrence is preferred (see example 3).
Input
The input contains several test cases. Each test case consists of a line with an integer m (m ≥ 1), the minimum number of repetitions, followed by a line containing a string s of length between m and 40000, inclusive. All characters in s are lowercase characters from ‘a’ to ‘z’. The last test case is denoted by m = 0 and must not be processed.
Output
Print one line of output for each test case. If there is no solution, output ‘none’; otherwise, print two integers in a line, separated by a space. The first integer denotes the maximum length of a substring appearing at least m times; the second integer gives the rightmost possible starting position of such a substring.
Sample Input
3
baaaababababbababbab
11
baaaababababbababbab
3
cccccc
0
Sample Output
5 12 none 42

第一种后缀数组求解,

//
// main.cpp
// 口吃的外星人(后缀数组+公共前缀)
//
// Created by zhoujl on 15/11/7.
// Copyright (c) 2015年 zhoujl. All rights reserved.
//
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
using namespace std;
#define cls(a, n) memset(a, n, sizeof(a))
#define REP(i, n) rep(i, 0, n)
#define inf 0x7ffffffff
#define rep(i, l, n) for (int i = l; i < n; i++)
typedef long long LL;
const int maxn = 400*100+10;
struct suffix {
    int sa[maxn], t1[maxn], t2[maxn], c[maxn], height[maxn], d[maxn][50], rank[maxn];
    char s[maxn];
    int n;
    void build_sa(int m) {
        int * x = t1, *y= t2;
        REP(i, m) c[i] = 0;
        REP(i, n) c[x[i] = (s[i]-'a' + 1)]++;
        rep(i, 1 ,m) c[i] += c[i-1];
        for (int i = n-1; i >= 0; i--) sa[--c[x[i]]] = i;
        for (int j = 1;  j < n; j <<= 1) {
            int p = 0, k;
            for (k = n-j; k < n; k++)
                y[p++] = k;
            rep(i, 0, n) {
                if (sa[i] - j >= 0)
                    y[p++] = sa[i] - j;
            }
            REP(i, m) c[i] = 0;
            REP(i, n) c[x[y[i]]]++;
            rep(i, 1 ,m) c[i] += c[i-1];
            for (int i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
            swap(x, y);
            p = 1;
            x[sa[0]] = 0;
            rep(i, 1, n) {
                if (y[sa[i]] == y[sa[i-1]] && y[sa[i]+j] == y[sa[i-1]+j]) {
                    x[sa[i]] = p - 1;
                } else {
                    x[sa[i]] = p++;
                }
            }
            if (p >= n)
                break;
            m = p;
        }
    }
    void build_ht() {
        int k = 0;
        rep(i, 1, n+1) rank[sa[i]] = i;
        rep(i, 0, n) {
            if (k) k--;
            while (rank[i] && s[i+k] == s[sa[rank[i]-1]+k]) {
                k++;
            }
            height[rank[i]] = k;// height[]的合法范围为 1-N, 其中0是结尾加入的字符
        }
    }
    void RMQ_init() {
        int i, j;
        rep(i, 1, n+1)
        d[i][0] = height[i];
        for (i = 1; (1<<i) <= n; i++) {
            for (j = 1; j+(1<<i)-1 <= n; j++) {
                d[j][i] = min(d[j][i-1] , d[j+(1<<(i-1))][i-1]);
            }
        }
    }
    int RMQ(int l, int r) {
        int k = 0;
        while ((1<<(k+1)) <= r-l+1) k++;
        return min(d[l][k], d[r-(1<<k)+1][k]);
    }
    void clear() {

    }
};
suffix tree;
int main() {
    int m;
    while (scanf("%d", &m) && m != 0) {
        scanf("%s", tree.s);
        tree.n = strlen(tree.s); 
        tree.clear();
        tree.s[tree.n] = 'a'-1;
        tree.n++;//注意区分此处为n+1,因为添加了一个结尾字符用于区别比较
        tree.build_sa(27);
        tree.n--;
        tree.build_ht();
        tree.RMQ_init();
        int maxl = 0;
        int pos = 0;
        if (m == 1) {
            printf("%d %d\n", tree.n, 0);
            continue;
        }
        for (int i = tree.n-1; i >= 0; i--) {
            if (tree.rank[i]+m-1 <= tree.n) {
                int max2 = tree.RMQ(tree.rank[i]+1, tree.rank[i]+m-1);
                if (maxl < max2) {
                    maxl = max2;
                    pos = i;
                    int p = tree.rank[i]+m-1;
                    while (p >= tree.rank[i]+1) {
                        if (tree.sa[p] > pos)
                            pos = tree.sa[p];
                        p--;
                    }
                } else if (maxl == max2) {
                    pos = max(pos, i);
                    int p = tree.rank[i]+m-1;
                    while (p >= tree.rank[i]+1) {
                        if (tree.sa[p] > pos)
                            pos = tree.sa[p];
                        p--;
                    }
                }
            }
        }
        if (maxl) {
            printf("%d %d\n", maxl, pos);
        } else {
            printf("none\n");
        }
    }
    return 0;
}

第二种哈希lcp算法求解

为每个后缀计算一个哈希值,满足递推式:H[i] = H[i-1]x + s[i],其中,H[n] = 0

对于长度为L的字符串,定义它的Hash值为Hash[i,L] = H[i] - H[i+L] x ^ L,其中x值是任意的。H和Hash数组保存成unsigned long long

//
// main.cpp
// 口吃的外星人(哈希值的lcp算法)
//
// Created by zhoujl on 15/11/4.
// Copyright (c) 2015年 zhoujl. All rights reserved.
//
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
typedef long long LL;
#define cls(a, n) memset(a, n, sizeof(a))
#define rep(i, l, n) for (int i = l; i < n; i++)
const int maxn = 400010;
const int x = 123;
LL h[maxn], xp[maxn], hasha[maxn];
int ranka[maxn];
char s[maxn];
int m, pos, n;
int cmp(int a, int b) {
    return (hasha[a] < hasha[b]) ||(hasha[a] == hasha[b] && a < b);
}
int possible(int L) {
    int c = 0;
    pos = -1;
    rep(i, 0, n-L+1) {
        ranka[i] = i;
        hasha[i] = h[i] - h[i+L]*xp[L];
    }
    sort(ranka, ranka+n-L+1, cmp);
    rep(i, 0, n-L+1) {
        if (i == 0 || hasha[ranka[i]] != hasha[ranka[i-1]]) c = 0;
        if (++c >= m)
            pos = max(pos, ranka[i]);
    }
    return pos >= 0;
}
int main() {
    while (scanf("%d", &m) == 1 &&m != 0) {
        scanf("%s", s);
        n = strlen(s);
        h[n] = 0;
        for (int i = n-1; i >= 0; i--) {
            h[i] = h[i+1]*x + s[i]-'a';
        }
        xp[0] = 1;
        rep(i, 1, n) xp[i] = xp[i-1] * x;
        if (!possible(1)) {
            printf("none\n");
        } else {
            int L = 1, R = n+1;
            while (R - L > 1) {
                int M = (R+L) / 2;
                if (possible(M)) L = M;
                else R = M;
            }
            possible(L);
            printf("%d %d\n", L, pos);
        }
    }
    return 0;
}

你可能感兴趣的:(后缀数组-哈希lcp算)