【BZOJ5137】【Usaco2017 Dec】Standing Out from the Herd(广义SAM)

Description

定义一个字符串的「独特值」为只属于该字符串的本质不同的非空子串的个数。如 “amy” 与 “tommy” 两个串,
只属于 “amy” 的本质不同的子串为 “a” “am” “amy” 共 3 个。只属于 “tommy” 的本质不同的子串为 “t” “to” ”
tom” “tomm” “tommy” “o” “om” “omm” “ommy” “mm” “mmy” 共 11 个。 所以 “amy” 的「独特值」为 3 ,”tommy
” 的「独特值」为 11 。给定 N 个字符集为小写英文字母的字符串,所有字符串的长度和小于 10^5 ,求出每个
字符串「独特值」


Solution

康复训练。。。

建出广义SAM,自下而上合并即可。


Code

/**************************************
 * Au: Hany01
 * Prob: BZOJ5137
 * Date: Jul 2nd, 2018
 * Email: [email protected]
**************************************/

#include

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 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 = 100005;

int n, tot = 1, las, fa[maxn << 1], ch[maxn << 1][26], len[maxn << 1], id[maxn << 1], lens, mxlen, Ans[maxn], beg[maxn << 1], nex[maxn << 1], v[maxn << 1], e;
char s[maxn];

inline void extend(int c, int cur)
{
    int p = las, np = ++ tot;
    len[las = np] = len[p] + 1, id[np] = cur;
    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;
            len[nq] = len[p] + 1, fa[nq] = fa[q], Cpy(ch[nq], ch[q]), fa[np] = fa[q] = nq;
            if (id[np] == id[q]) id[nq] = id[q];
            while (p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
        }
    }
}

inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }

inline void dfs(int u)
{
    for (register int i = beg[u]; i; i = nex[i]) dfs(v[i]);
    if (id[u] != id[fa[u]]) id[fa[u]] = 0;
    Ans[id[u]] += len[u] - len[fa[u]];
}

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

    n = read();
    For(i, 1, n) {
        scanf("%s", s), las = 1;
        rep(j, lens = strlen(s)) extend(s[j] - 97, i);
        chkmax(mxlen, lens);
    }

    For(i, 2, tot) add(fa[i], i);
    dfs(1);

    For(i, 1, n) printf("%d\n", Ans[i]);

    return 0;
}

你可能感兴趣的:(BZOJ,SAM,USACO)