【BZOJ3172】【TJOI2013】单词(AC自动机)

Description

某人读论文,一篇论文是由许多单词组成。但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次。


Solution

AC自动机模板题,建出自动机后,将单词用另一种符号连起来、匹配即可。


Code

/************************************************
 * Au: Hany01
 * Date: Jun, 1st, 2018
 * Prob: [BZOJ3172][TJOI2013] 单词
 * Email: [email protected]
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#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 x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

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 int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 1000305, maxp = 205;

int n, pos[maxp], lenp, lens;
char p[maxn], s[maxn];

struct AhoCorasickAutomaton
{

    int ch[maxn][27], fail[maxn], tot, cnt[maxn], flag[maxn];

    inline void insert(char* s, int len, int id)
    {
        int u = 1;
        rep(i, len) {
            if (ch[u][s[i] - 97]) u = ch[u][s[i] - 97];
            else u = ch[u][s[i] - 97] = ++ tot;
        }
        pos[id] = u, flag[u] = 1;
    }

    inline void getfail()
    {
        static queue<int> q;
        rep(i, 26) if (ch[1][i]) q.push(ch[1][i]), fail[ch[1][i]] = 1;
        while (!q.empty()) {
            int u = q.front(), v, w; q.pop();
            rep(i, 26) if (v = ch[u][i]) {
                w = fail[u];
                while (!ch[w][i] && w) w = fail[w];
                fail[v] = w ? ch[w][i] : 1;
                q.push(v);
            }
        }
    }

    inline void match(char* s, int len)
    {
        int u = 1;
        rep(i, len) {
            while (!ch[u][s[i] - 97] && u) u = fail[u];
            if (u) u = ch[u][s[i] - 97]; else u = 1;
            ++ cnt[u]; int t = u;
            while (t) t = fail[t], ++ cnt[t];
        }
    }

}ac;

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

    n = read(), ac.tot = 1;
    For(i, 1, n) {
        scanf("%s", p), lenp = strlen(p);
        rep(j, lenp) s[lens + j] = p[j];
        lens += lenp, s[lens ++] = '{', ac.insert(p, lenp, i);
    }
    ac.getfail(), ac.match(s, lens);
    For(i, 1, n) printf("%d\n", ac.cnt[pos[i]]);

    return 0;
}
//夜长争得薄情知,春初早被相思染。
//    -- 姜夔《踏莎行·自沔东来》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/80535882
今日推荐