hdu 4787 GRE Words Revenge(在线AC自动机)

在线ac自动机:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>

using namespace std;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define ceil(x, y) (((x) + (y) - 1) / (y))

const int SIZE = 2;
const int N = 1e5 + 10;
const int M = 1e3 + 10;
const int INF = 0x7f7f7f7f;
const int MAX_WORD = 5e6 + 10;
const double EPS = 1e-9;
const int MOD = 2015;
const int TH = 2e3;

struct AC {
    int sz;
    int ch[N][SIZE];
    bool ed[N];
    int f[N];

    int newnode() {
        memset(ch[sz], 0, sizeof(ch[sz]));
        ed[sz] = false;
        f[sz] = 0;
        return sz++;
    }

    void init() {
        sz = 0;
        newnode();
    }

    void insert(char *s) {
        int u = 0;
        for (int i = 1; s[i]; i++) {
            int v = s[i] - '0';
            if (!ch[u][v])
                ch[u][v] = newnode();
            u = ch[u][v];
        }
        ed[u] = true;
    }

    void getfail() {
        queue<int> q;
        for (int i = 0; i < SIZE; i++)
            if (ch[0][i])
                q.push(ch[0][i]);

        while (!q.empty()) {
            int r = q.front();
            q.pop();
            for (int i = 0; i < SIZE; i++) {
                int v = ch[r][i];
                if (v) {
                    q.push(v);
                    int u = f[r];
                    while (u && !ch[u][i]) u = f[u];
                    f[v] = ch[u][i];
                }
            }
        }
    }

    int find(char *s) {
        int t = 0;
        int u = 0;
        for (int i = 1; s[i]; i++) {
            int v = s[i] - '0';
            while (u && !ch[u][v]) u = f[u];
            u = ch[u][v];
            int p = u;
            while (p) {
                if (ed[p])
                    t++;
                p = f[p];
            }
        }
        return t;
    }
}ac, buf;//建2个ac自动机,buf用来存少量的数据

int ans;
char str[MAX_WORD];

void init() {
    ac.init();
    buf.init();
}

void dfs(int r1, int r2) {//将buf中以r2为根结点的树合并到ac中以r1为根结点的树中
    for (int i = 0; i < SIZE; i++) {
        if (buf.ch[r2][i]) {
            if (!ac.ch[r1][i])
                ac.ch[r1][i] = ac.newnode();
            int t = ac.ch[r1][i];
            ac.ed[t] |= buf.ed[buf.ch[r2][i]];
            dfs(ac.ch[r1][i], buf.ch[r2][i]);
        }
    }
}

void join() {
    dfs(0, 0);      //暴力把buf的字典树合并到ac的字典树中去
    buf.init();     //清空buf
    ac.getfail();   //求新的ac自动机的失配边
}

void _swap(int is, int ie, int js, int je) {
    int leni = ie - is + 1, lenj = je - js + 1;
    if (leni > 0 && lenj > 0) {
        for (int i = 0; i < min(leni, lenj); i++)
            swap(str[is + i], str[js + i]);
        if (leni > lenj)
            _swap(is + lenj, ie, js, je);
        else
            _swap(js, js + leni - 1, js + leni, je);
    }
}

int main() {        
    int t_case;
    scanf("%d", &t_case);
    for (int i_case = 1; i_case <= t_case; i_case++) {
        int n;
        scanf("%d", &n);
        init();
        ans = 0;
        printf("Case #%d:\n", i_case);
        for (int i = 0; i < n; i++) {
            scanf("%s", str);
            int len = strlen(str);
            int k = ans % (len - 1);
            _swap(1, k, k + 1, len - 1);
            if (str[0] == '+') {
                buf.insert(str);
                buf.getfail();
                if (buf.sz > TH)//超出阈值,则合并
                    join();
            }
            else {
                ans = ac.find(str) + buf.find(str);
                printf("%d\n", ans);
            }
        }
    }
    return 0;
}



我的ac自动机模板:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 26;
class Node{
public :
    Node *fail;
    Node *next[maxn];
    int cnt;
    Node(){
        memset(next,0,sizeof(next));
        fail=NULL;
        cnt=0;
    }
};
class AC_automato :public Node{
public:
    Node *root;
    void clear(){
        root = new Node();
    }

    void insert(char *s){
        Node *p=root;
        while(*s!=NULL){
            int t=*s-'a';
            if(p->next[t]==NULL)p->next[t]=new Node();
            p=p->next[t];
            s++;
        }
        p->cnt++;
    }

    void build(){
        queue<Node *>q;
        while(!q.empty())q.pop();
        q.push(root);
        Node *now,*p;
        while(!q.empty()){
            now=q.front();
            q.pop();
            for(int i=0;i<maxn;i++){
                if(now->next[i]==NULL)continue;
                if(now==root)now->next[i]->fail=root;
                else{
                    p=now->fail;
                    while(p){
                        if(p->next[i]){
                            now->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL)now->next[i]->fail=root;
                }
                q.push(now->next[i]);
            }
        }
    }


    int find(char *s){
        int ans=0;
        Node *p=root;
        while(*s){
            int now=*s-'a';
            while(p->next[now]==NULL && p!=root)p=p->fail;
            p=p->next[now];
            if(p==NULL)p=root;

            Node *p2=p;
            while(p2!=root && p2->cnt!=-1){
                ans+=p2->cnt;
                p2->cnt=-1;
                p2=p2->fail;
            }
            s++;
        }
        return ans;
    }
}ac;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值