ac自动机板子

struct ACAM {
    int tr[N][26] = {} ; // N表示单词字符个数之和 
    int ne[N] = {} ;
    int idx = 0 ;
    int cnt[N] = {} ;
    int q[N] = {} ; //记录路径
    int hh = 0 , tt = 0 ;
    
    void insert(string s) {
        int p = 0 ; 
        for(auto t : s) {
            int c = t - 'a' ;
            if(!tr[p][c]) tr[p][c] = ++ idx ;
            p = tr[p][c] ; 
           
        }
        
        //以下是你要进行的操作
        cnt[p] ++ ; //p表示字符串在trie树上的位置
        
    }
    
    void build() {

        for(int i = 0 ; i < 26 ; i ++) 
            if(tr[0][i])
                q[tt ++] = {tr[0][i]} ;
                
        while(hh < tt) {
            int t = q[hh ++] ;
            
            for(int i = 0 ; i < 26 ; i ++) {
                int p = tr[t][i] ;
                
                if(!p) tr[t][i] = tr[ne[t]][i] ;
                else {
                    ne[p] = tr[ne[t]][i] ; 
                    q[tt ++] = p ; 
                }
            }
        }
        
    }
    
    //以下随意添加
    
};

例题 https://www.acwing.com/problem/content/description/1284/

#include <iostream>

using namespace std ;

const int N = 5e5 + 10 ;

struct ACAM {
    int tr[N][26] = {} ; // N表示单词字符个数之和 
    int ne[N] = {} ;
    int idx = 0 ;
    int cnt[N] = {} ;
    int q[N] = {} ; //记录路径
    int hh = 0 , tt = 0 ;
    
    void insert(string s) {
        int p = 0 ; 
        for(auto t : s) {
            int c = t - 'a' ;
            if(!tr[p][c]) tr[p][c] = ++ idx ;
            p = tr[p][c] ; 
           
        }
        
        //以下是你要进行的操作
        cnt[p] ++ ; //p表示字符串在trie树上的位置
        
    }
    
    void build() {

        for(int i = 0 ; i < 26 ; i ++) 
            if(tr[0][i])
                q[tt ++] = {tr[0][i]} ;
                
        while(hh < tt) {
            int t = q[hh ++] ;
            
            for(int i = 0 ; i < 26 ; i ++) {
                int p = tr[t][i] ;
                
                if(!p) tr[t][i] = tr[ne[t]][i] ;
                else {
                    ne[p] = tr[ne[t]][i] ; 
                    q[tt ++] = p ; 
                }
            }
        }
        
    }
    
    //以下随意添加
    int op(string s){ //你要执行的操作
        int p = 0 ; 
        int ans = 0 ; 
        
        for(auto t : s) {
            char c = t - 'a' ;
            p = tr[p][c] ;
            int f = p ;
            
            while(f) {
                ans += cnt[f] ;
                cnt[f] = 0 ; 
                f = ne[f] ;
            }
        }
        
        return ans ; 
    }
    
};

int main(){
    int T ;
    cin >> T ;
    while(T --) {
        int n ;
        cin >> n ; 
        ACAM acam ;
        for(int i = 0 ; i < n ; i ++) {
            string s ;
            cin >> s ; 
            acam.insert(s) ; 
        }
        
        acam.build() ;
        
        string res ;
        cin >> res ;
        cout << acam.op(res) << endl ; 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值