HDU 2222 Keywords Search AC自动机

AC自动机 入门题

http://acm.hdu.edu.cn/showproblem.php?pid=2222

/*********************************************
    Problem : HDU
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++) //遍历
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --) //反向遍历
#define repS(it,p) for(auto it = p.begin() ; it != p.end() ; it ++) //遍历一个STL容器
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next) //遍历u所连接的点
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 5e5+5;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m,k;

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};

const int MAX_SIZE = 26;
int tot ; 
char s[1000005];

class AC_automation {
private:   
    struct Node {
        int next[MAX_SIZE]; //节点的儿子
        int stat ; //有几个字符是以当前节点结尾的
    }t[MAXN]; //Trie 树上的节点
    int fail[MAXN]; //失配指针

public:    
    void init() {
        tot = 1; cls(t,0); cls(fail,0);
    }

    void insert(char * str) {
        int len = strlen(str) - 1;
        rep(i,0,len) str[i] -= 'a';
        int pre = 0 , nxt = 0 ;
        rep(i,0,len) {
            nxt = t[pre].next[str[i]];
            if(nxt == 0) {
                t[pre].next[str[i]] = tot ;
                nxt = tot ++;
            }
            pre = nxt;
            //printf("%d -> ",nxt);
        }
        //puts("");
        t[nxt].stat ++;
    }

    void get_fail() {
        queue<int>q;
        q.push(0);
        while(!q.empty()) {
            int u = q.front();
            rep(i,0,MAX_SIZE-1) {
                if(t[u].next[i]) {
                    q.push(t[u].next[i]);
                    int tmp = u;
                    while(fail[tmp]) {
                        if(t[fail[tmp]].next[i]) {
                            fail[t[u].next[i]] = t[fail[tmp]].next[i]; break;
                        }
                        tmp = fail[tmp];
                    } 
                    if(u != 0 && !fail[t[u].next[i]]) {//处理fail[tmp] = 0 时的情况
                        if(t[0].next[i]) fail[t[u].next[i]] = t[0].next[i];
                    }
                }
            }
            q.pop();
        }
        //rep(i,1,tot-1) printf("%d %d %d\n",i,fail[i],t[i].stat);  
    }

    int query(char * str) {
        int ret = 0;
        int len = strlen(str) - 1;
        rep(i,0,len) str[i] -= 'a';
        int now = 0;
        rep(i,0,len) {
            while(now && (!t[now].next[str[i]])) now = fail[now];
            now = t[now].next[str[i]];
            int tmp = now;
            while(tmp && (t[tmp].stat != -1)) {
                ret += t[tmp].stat;
                t[tmp].stat = -1;                
                tmp = fail[tmp];
            }
        }
        return ret;
    }
};

AC_automation ac;

void input() {
    ac.init();
    scanf("%d",&n);
    char str[55];
    rep(i,1,n) {
        scanf("%s",str);
        ac.insert(str);
    }
    ac.get_fail();
    scanf("%s",s);
}

void solve() {
    printf("%d\n",ac.query(s));
}

int main(void) {
    //freopen("a.in","r",stdin);
    scanf("%d",&T); while(T--) {
        input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值