【BZOJ2780】【SPOJ】Sevenk Love Oimaster (Generalized Suffix Automata)

Description

There are n large strings and m queries, each time a string s is given to ask how many large strings appear in it.


Solution

An introduction to generalized suffix automata.

lastIn fact, it can be set to after inserting a string root.
Then count how many substrings of the input string the string represented by each node is.
For each inquiry, just walk through the SAM directly.

As for the time complexity, I feel really fascinated. .


Code

/**************************************
 * Au: Hany01
 * Date: May 5th, 2018
 * Prob: [BZOJ2780][SPOJ JZPGYZ] Sevenk Love Oimaster
 * Email: [email protected]
**************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia 
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    register char c_; register int _, __;
    for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-')  __ = -1;
    for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 10005, maxl = 100005, maxq = 360005;

int tot = 1, las, len[maxl << 1], fa[maxl << 1], ch[maxl << 1][26], mk[maxl << 1], cnt[maxl << 1], n, m, L[maxn];
char p[maxq];
string s[maxn];

inline void extend(int c)
{
    int np = ++ tot, p = las;
    las = tot, len[np] = len[p] + 1;
    while (p && !ch[p][c]) ch[p][c] = np, p = fa[p];
    if (!p) fa[np] = 1;
    else {
        int q = ch[p][c];
        if (len[q] == len[p] + 1) fa[np] = q;
        else {
            int nq = ++ tot;
            fa[nq] = fa[q], Cpy(ch[nq], ch[q]), len[nq] = len[p] + 1;
            fa[q] = fa[np] = nq;
            while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
        }
    }
}

int main()
{
#ifdef hany01
    File("bzoj2780");
#endif

    ios::sync_with_stdio(false);

    cin >> n >> m;
    For(i, 1, n) {
        cin >> s[i];
        L[i] = s[i].length(), las = 1;
        rep(j, L[i]) extend(s[i][j] - 97);
    }

    For(i, 1, n) {
        int u = 1;
        rep(j, L[i]) {
            u = ch[u][s[i][j] - 97];
            int v = u;
            while (v && mk[v] != i) mk[v] = i, ++ cnt[v], v = fa[v];
        }
    }

    while (m --)
    {
        cin >> p;
        int u = 1;
        rep(i, strlen(p)) u = ch[u][p[i] - 97];
        printf("%d\n", cnt[u]);
    }

    return 0;
}
//露清枕簟藕花香,恨悠扬。
//    -- 顾敻《虞美人·触帘风送景阳钟》

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643411&siteId=291194637