AC自动机自我理解和模板

时间:2023-03-09 20:16:34
AC自动机自我理解和模板

给出长度为m的文本

查询 n个单词出现的次数

用kmp 复杂度 n*m*(单词平均长度)

用字典树 复杂度 m*每次字典树遍历的平均深度)

AC自动机  复杂度 m  (思路可以理解为kmp+字典树 )

正在学  代码没修改完

#include<bits/stdc++.h>
using namespace std;
;
;
struct node
{
    node *fail;
    node *next[kind];
    int cnt;
    node()
    {
        fail=NULL;
        cnt=;
        memset(next,NULL,sizeof(next));
    }
}*q[maxn];
];
]; //模式串
int head,tail;    //队列的头尾指针
void insert(char *str,node *root)  // **************建树
{
    node *p=root;
    ,index;
    while(str[i])
    {
        index=str[i]-'a';
        if(p->next[index]==NULL) p->next[index]=new node();
        p=p->next[index];
        i++;
    }p->cnt++;
}
void build_ac_automation(node *root)
{
    root->fail=NULL;
    q[head++]=root;//  tou zhi zheng
    while(head!=tail)
    {
        node *temp=q[tail++];
        node *p=NULL;
        ;i<;i++)
        {
            if(temp->next[i]!=NULL)
            {
                if(temp==root) temp->next[i]->fail=root;
                else
                {
                    p=temp->fail;
                    while(p!=NULL)
                    {
                        if(p->next[i]!=NULL)
                        {
                            temp->next[i]->fail=p->next[i];
                            break;
                        }
                        p=p->fail;
                    }
                    if(p==NULL) temp->next[i]->fail=root;
                }
                q[head++]=temp->next[i];
            }
        }
    }
}
int ac_query(node *root)
{
    ;
    ;
    int index;
    int l=strlen(str);
    node *p=root;
    while(str[i])
    {
        index=str[i]-'a';
        index=str[i]-'a';
        while(p->next[index]==NULL && p!=root) p=p->fail;
        p=p->next[index];
        p=(p==NULL)?root:p;
        node *temp=p;
        )
        {
            num+=temp->cnt;
            temp->cnt=-;
            temp=temp->fail;
        }
        i++;
    }
    return num;
}
int32_t main()
{

}

banzi   https://www.cnblogs.com/MingSD/p/8733762.html

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
const int INF = 0x3f3f3f3f;
;
typedef pair<int,int> pll;
, M = 1e3;
];
][];
];
];
;
void Insert(){
    , len = strlen(str);
    ; i < len; i++){
        int id = str[i] - 'a';
        ){
            cnt[tot] = ;
            fair[tot] = ;
            trie[rt][id] = tot++;
        }
        rt = trie[rt][id];
    }
    cnt[rt]++;
}
void Build_tree(){
    queue<int> q;
    q.push();
    int p;
    while(!q.empty()){
        int tmp = q.front();
        q.pop();
        ; i < ; i++){
            ){
                )
                    fair[trie[tmp][i]] = ;
                else{
                    p = fair[tmp];
                    while(p){
                        if(trie[p][i]){
                            fair[trie[tmp][i]] = trie[p][i];
                            break;
                        }
                        else p = fair[p];
                    }
                    ;
                }
                q.push(trie[tmp][i]);
            }
        }
    }
}
int Query(){
    , ret = , len = strlen(str);
    ; i < len; i++){
        int id = str[i] - 'a';
        ) rt = fair[rt];
        rt = trie[rt][id];
        ) rt = ;
        int tmp = rt;
        ){
            ){
                ret += cnt[tmp];
                cnt[tmp] = -;
            }
            else break;
            tmp = fair[tmp];
        }
    }
    return ret;
}
void init(){
    ; i < tot; i++){
        ; j < ; j++)
            trie[i][j] = ;
    }
    tot = ;
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--)
    {
        init();
        int n;
        scanf("%d", &n);
        while(n--)
        {
            scanf("%s", str);
            Insert();
        }
        Build_tree();
        scanf("%s", str);
        printf("%d\n", Query());
    }
    ;
}