hdu2896 AC自动机

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
#define pr(x) cout << #x << ": " << x << "  " 
#define pl(x) cout << #x << ": " << x << endl;
#define pri(a) printf("%d\n",(a));
#define xx first
#define yy second
#define sa(n) scanf("%d", &(n))
#define sal(n) scanf("%lld", &(n))
#define sai(n) scanf("%I64d", &(n))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define vep(c) for(decltype((c).begin() ) it = (c).begin(); it != (c).end(); it++) 
const int mod = int(1e9) + 7, INF = 0x3fffffff;

const int maxn =  200 * 500 + 12;
const int maxcharset = 128;
const char charst = 0;
char buf[10004];
int id;
bool used[510];
bool flag;

struct Trie
{
    int next[maxn][maxcharset], fail[maxn], ids[maxn];
    int root, L;
    int newnode(void) {
        for (int i = charst;i < maxcharset;i++) next[L][i] = -1;
        ids[L++] = -1;
        return L - 1;
    }
    void init(void) {
        L = 0;
        root = newnode();
    }
    void insert(char buf[]) {
        int len = (int)strlen(buf);
        int now = root;
        for(int i = 0;i < len;i++) {
            if(next[now][buf[i] - charst] == -1)
                next[now][buf[i] - charst] = newnode();
            now = next[now][buf[i] - charst];
        }
        ids[now] = id;
    }
    void build(void) {
        queue<int> Q;
        fail[root] = root;
        for(int i = 0;i < maxcharset;i++)
            if(next[root][i] == -1) next[root][i] = root;
            else {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        while (!Q.empty())  {
            int now = Q.front(); Q.pop();
            for(int i = 0;i < maxcharset;i++)
                if(next[now][i] == -1) next[now][i] = next[fail[now]][i];
                else {
                    fail[next[now][i]]=next[fail[now]][i];
                    Q.push(next[now][i]);
                }
        }
    }
    int query(char buf[])
    {
        memset(used, 0, sizeof(used));
        int len = strlen(buf);
        int now = root;
        int res = 0;
        for(int i = 0;i < len;i++) {
            now = next[now][buf[i] - charst];
            int temp = now;
            while(temp != root) {
                if (ids[temp] != -1 && !used[ids[temp]]) flag = true, used[ids[temp]] = true;
                temp = fail[temp];
            }
        }
        return res;
    }
};

Trie ac;
int main(void)
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif
    int n, m;
    while (~sa(n)) {
        ac.init();
        rep (i, 0, n) {
            id = i + 1;
            scanf("%s", buf);
            ac.insert(buf);
        }

        sa(m);
        ac.build();
        int total = 0;
        rep (i, 0, m) {
            scanf("%s", buf);
            flag = false;
            ac.query(buf);
            if (flag) total++, printf("web %d:", i + 1);
            rep (j, 1, n + 1) {
                if (used[j]) flag = true, printf(" %d", j);
            }
            if (flag) puts("");
        }
        printf("total: %d\n", total);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值