【BZOJ3998】【TJOI2015】String Theory (SAM)

Description

For a given string of length N, consider whether or not to count repeated substrings, and find its Kth substring.


Solution

Go back and engage in culture for so long, and now I don't feel like writing code. I have even adjusted a SAM for a long time, and I have rehabilitated training. . .

For the query to calculate repeated substrings, we can directly accumulate them size.
If we do not count duplicates, we will sizeassign directly to 1 That's it.

Then just run DFS directly.


Code

/**************************************
 * Au: Hany01
 * Prob: [BZOJ3998][TJOI2015] 弦论
 * Date: May 3rd, 2018
 * 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 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 = 500005;

struct SAM
{
    int ch[26], fa, len;
}t[maxn << 1];

int sz[maxn << 1], tot = 1, las = 1, sum[maxn << 1], per[maxn << 1], c[maxn];

int n;
char s[maxn];

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

void dfs(int u, int k)
{
    if (k <= sz[u]) return ;
    k -= sz[u];
    rep(i, 26) if (t[u].ch[i])
    {
        if (sum[t[u].ch[i]] >= k)
        {
            putchar(i + 97);
            dfs(t[u].ch[i], k);
            return ;
        } else k -= sum[t[u].ch[i]];
    }
}

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

    static int ty, k;

    //Init
    scanf("%s", s), n = strlen(s);
    ty = read(), k = read();
    rep(i, n) extend(s[i] - 97);

    //Sort
    For(i, 1, tot) ++ c[t[i].len];
    For(i, 1, n) c[i] += c[i - 1];
    For(i, 1, tot) per[c[t[i].len] --] = i;

    //Deal with the requirement
    if (ty) {
        Fordown(i, tot, 1) sz[t[per[i]].fa] += sz[per[i]];
        sz[1] = 0;
    } else For(i, 2, tot) sz[i] = 1;

    //Get the sum
    Fordown(i, tot, 1) {
        sum[per[i]] = sz[per[i]];
        rep(c, 26) sum[per[i]] += sum[t[per[i]].ch[c]];
    }

    //Print
    if (k > sum[1]) puts("-1");
    else dfs(1, k);

    return 0;
}

Guess you like

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