LightOJ 1427(AC自动机

题目:给出若干模板串和一个母串,求每个模板串在母串中出现的次数.

思路:AC自动机+后缀链接(last数组)

/*
* @author:  Cwind
*/
///#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <map>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <set>
#include <cmath>
using namespace std;
#define IOS std::ios::sync_with_stdio (false);std::cin.tie(0)
#define pb push_back
#define PB pop_back
#define bk back()
#define fs first
#define se second
#define sq(x) (x)*(x)
#define eps (3e-7)
#define IINF (1<<29)
#define LINF (1ll<<59)
#define INF (1000000000)
#define FINF (1e3)
#define clr(x) memset((x),0,sizeof (x));
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<int,int> P;

const int maxv=1e5+3000;
const int csize=26;
int ch[maxv*4][csize];
int sz;
bool term[maxv*4];
vector<int> termlist[maxv*4];
void Tclear(){
    sz=1;
    clr(ch[0]);clr(term);
}
int last[maxv*4];
void insert(char *s,int id){
    int u=0,n=strlen(s);
    for(int i=0;i<n;i++){
        int c=s[i]-‘a‘;
        if(!ch[u][c]){
            memset(ch[sz],0,sizeof ch[sz]);
            termlist[sz].clear();
            term[sz]=0;
            ch[u][c]=sz++;
        }
        u=ch[u][c];
    }
    term[u]=1;
    termlist[u].pb(id);
}
int f[maxv*4];
queue<int> Q;
void getFail(){
    f[0]=0;
    for(int c=0;c<csize;c++){
        int u=ch[0][c];
        if(u){
            f[u]=0;
            Q.push(u);
        }
    }
    while(!Q.empty()){
        int r=Q.front();Q.pop();
        for(int c=0;c<csize;c++){
            int u=ch[r][c];
            if(!u) continue;
            Q.push(u);
            int v=f[r];
            while(v&&!ch[v][c]){
                v=f[v];
            }
            f[u]=ch[v][c];
            last[u]=term[f[u]]?f[u]:last[f[u]];
        }
    }
}

const int maxlen=1e6+3000;
const int maxn=600;
int T,n;
char r[maxlen];
char M[maxlen];
int ans[maxn];
int cas=0;
int main(){
    freopen("/home/slyfc/CppFiles/in","r",stdin);
    //freopen("defense.in","r",stdin);
    //freopen("defense.out","w",stdout);
    cin>>T;
    while(T--){
        cin>>n;
        printf("Case %d:\n",++cas);
        clr(ans);clr(last);
        Tclear();
        scanf("%s",M);
        for(int i=0;i<n;i++){
            scanf("%s",r);
            insert(r,i+1);
        }
        getFail();
        int len=strlen(M);
        int v=0;
        for(int i=0;i<len;i++){
            int c=M[i]-‘a‘;
            while(v&&!ch[v][c]) v=f[v];
            v=ch[v][c];
            int vv=v;
            while(vv!=0){
                for(int j=0;j<termlist[vv].size();j++){
                    ans[termlist[vv][j]]++;
                }
                vv=last[vv];
            }
        }
        for(int i=1;i<=n;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}

时间: 2024-08-10 07:46:07

LightOJ 1427(AC自动机的相关文章

light oj 1427(ac自动机)

1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int N = 510*505; 6 const int M = 27; 7 8 9 map<string,int>Map; 10 struct Trie 11 { 12 int next[N][M],fail[N],end[N]; 13 int root,L; 14 int newnode() 15 { 16 for(int i = 0; i < 26

LightOJ 1427 Substring Frequency (II) (AC自动机)

题意:给定一个文本串和 n 个子串,问你子串在文本串出现的次数. 析:很明显的AC自动机,只要把先把子串进行失配处理,然后再去用文本串去匹配,在插入子串时就要标记每个串,注意串可能是相同的,这个我错了两次,最后匹配一次就OK了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib

暑假集训day9补充(AC自动机)

推荐网站http://blog.csdn.net/niushuai666/article/details/7002823 AC自动机嘛,此AC(aho-corasick)非彼AC(Accepted). 我也不是很会解释 有一题是必须打的hdu2222. #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int mn=

ac自动机基础模板(hdu2222)

In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey also wants to bring this feature to his image retrieval system. Every image have a long description, when users type some keywords to find the image, th

HDU 2825 Wireless Password AC自动机+dp

训练赛第二场的I题,上完体育课回来就把这题过了,今天训练赛rank1了,还把大大队虐了,而且我还过了这道题 (虽然我也就过了这道题...),第一次在比赛中手写AC自动机还带dp的,心情大好. 给一个字符串集合,求包含该集合超过K个字符的,长度为L的字符串的个数. 显然是在AC自动机上跑dp,设dp[u][L][k]表示当前在结点u,还要走L步,当前状态为k的个数.一开始第三维表示的是包含k个字符串,但是题目要求不含重复的,那就只能状压了.转移为dp[u][L][k]+=dp[v][L-1][nk

HDU 2896-病毒侵袭(ac自动机)

题意: 给定多个模式串,每给一个母串,输出包含模式串的编号,最后输出包含模式串的母串的数量. 分析: ac自动机模板 #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <st

hdu2222 Keywords Search &amp; AC自动机学习小结

传送门:http://http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路:AC自动机入门题,直接上AC自动机即可. 对于构建AC自动机,我们要做的只有三件事: 1)构建字典树 2)构建失败指针 3)构建trie图(这道题好像不做这一步也能A...但是这一步不做是会被卡成O(n^2)的...) 1)第一步还是比较好理解的 根是虚根,边代表字母,那么根到终止节点的路径就是一个字符串,这样对于前缀相同的字符串我们就可以省下存公共前缀的空间. 加入一个模式

hdoj 2896 病毒侵袭(AC自动机)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2896 思路分析:题目为模式匹配问题,对于一个给定的字符串,判断能匹配多少个模式:该问题需要静态建树,另外需要对AC自动机的模板加以修改, 对于每个匹配的模式的最后一个单词的fail指针指向root,即可实现一个字符串进行多次模式匹配: 代码如下: #include <queue> #include <cstdio> #include <cstring> #include &

从Trie谈到AC自动机

ZJOI的SAM让我深受打击,WJZ大神怒D陈老师之T3是SAM裸题orz...我还怎么混?暂且写篇`从Trie谈到AC自动机`骗骗经验. Trie Trie是一种好玩的数据结构.它的每个结点存的是字母,因此得名`字母树`. 出一张图让大家感受下. (image powered by SaiBu NaoCu) 上面那是一棵插入了 ape,app,applicant,application,bake,ban,banana 等词的Trie.红色结点表示接受态. 显然,查找时只需顺着链照下来,插入只需