[POI2000] 最长公共子串


求多个串的最长公共子串。


听说如果会 SAM 的话这题就很简单了

然而我只会用 SA 做,具体做法是先用不同的分隔符把字符串接成一个串

然后还是按 height[i] 从大到小将 i 的集合与 i1 的集合合并

对每个集合维护一个 set[i],用来记录 i 的集合中出现了哪些字符串的子串 (注意 set[i] 也是一个集合)

当存在 set[i] 为全集时,就得到最长公共子串了。

时间复杂度: O(LlogL)


//POI 2000

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x = (x<<3) + (x<<1) + (c-'0');
    x *= flag;
    return;
}
template<class Num>void write(Num x)
{
    if(x < 0) putchar('-'), x = -x;
    static char s[20];int sl = 0;
    while(x) s[sl++] = x%10 + '0',x /= 10;
    if(!sl) {putchar('0');return;}
    while(sl) putchar(s[--sl]);
}
const int maxn = 1e4 + 20, maxT = 7, slen = 2005;

int T, n;
char s[maxT][slen], str[maxn];
int in[maxn];

std::pair<int,int> add[maxn];

int sa[maxn], rank[maxn];
int c[maxn], height[maxn];
int set[maxn], fa[maxn];

void make_sa(int m)
{
    static int t0[maxn], t1[maxn];
    int *x = t0, *y = t1;

    for(int i = 1; i <= m; i++) c[i] = 0;
    for(int i = 1; i <= n; i++) c[x[i] = str[i]]++;
    for(int i = 1; i <= m; i++) c[i] += c[i - 1];
    for(int i = n; i >= 1; i--) sa[c[x[i]]--] = i;

    for(int k = 1; k <= n; k <<= 1)
    {
        int p = 0;
        for(int i = 0; i < k; i++) y[++p] = n - i;
        for(int i = 1; i <= n; i++)
            if(sa[i] > k) y[++p] = sa[i] - k;

        for(int i = 1; i <= m; i++) c[i] = 0;
        for(int i = 1; i <= n; i++) c[x[y[i]]]++;
        for(int i = 1; i <= m; i++) c[i] += c[i - 1];
        for(int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];

        std::swap(x, y);

        x[sa[p = 1]] = 1;
        for(int i = 2; i <= n; i++)
            x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && sa[i] + k <= n && sa[i - 1] + k <= n && y[sa[i] + k] == y[sa[i - 1] + k] ? p : ++p;
        if(p == n) break;
        m = p;  
    }

}
void make_height()
{
    int k = 0;

    for(int i = 1; i <= n; i++) rank[sa[i]] = i;
    for(int i = 1; i <= n; i++)
    {
        if(k != 0) k--;
        if(rank[i] == 1) continue;
        int j = sa[rank[i] - 1];
        while(str[i + k] == str[j + k]) k++;
        height[rank[i]] = k;
    }
}
void init()
{
    read(T);
    for(int i = 1; i <= T; i++)
        scanf("%s", s[i] + 1);

    for(int i = 1; i <= T; i++)
    {
        int l = strlen(s[i] + 1);
        for(int j = 1; j <= l; j++)
        {
            str[n + j] = s[i][j];
            in[n + j] = i - 1;
        }
        n += l + 1;
        str[n] = '$' + i;
        in[n] = -1;
    }

    make_sa(256), make_height();
}
int find(int x)
{
    return x == fa[x] ? x : (fa[x] = find(fa[x]));
}
bool gather(int x,int y)
{
    x = find(x), y = find(y);
    if(x == y) return false;

    if(x > y) std::swap(x, y);
    fa[y] = x, set[x] |= set[y];

    return set[x] == (1 << T) - 1;
}
int sovle()
{
    for(int i = 1; i <= n; i++)
    {
        fa[i] = i;

        if(in[sa[i]] != -1) 
            set[i] = 1 << in[sa[i]];
    }

    for(int i = 1; i < n; i++)
        add[i] = std::make_pair(height[i + 1], i + 1);

    std::sort(add + 1, add + n);

    for(int i = n - 1; i >= 1; i--)
    {
        int r = add[i].first, v = add[i].second;
        if(gather(v - 1, v)) return r;
    }
    return 0;
}
int main()
{
    freopen("pow.in","r",stdin);
    freopen("pow.out","w",stdout);

    init();

    write(sovle());

    fclose(stdin);
    fclose(stdout);
    return 0;
}
阅读更多
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页