HDU 5384 AC自动机模版

点击打开链接

题意:n个A,m个B,对于每个A,输出所有B在A中出现的次数

思路:和AC自动机模版题2222相同,唯一的区别就是n个A不能开二维,自己处理一下就OK了

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=500010;
const int N=26;
struct node{
    node *fail;
    node *next[N];
    int num;
    node(){
        fail=NULL;
        num=0;
        memset(next,NULL,sizeof(next));
    }
}*q[maxn];
char str1[100010],str2[10010],str[10010];
int tmp[100010];
int head,tail;
void insert_Trie(char *str,node *root){
    node *p=root;
    int i=0;
    while(str[i]){
        int id=str[i]-'a';
        if(p->next[id]==NULL) p->next[id]=new node();
        p=p->next[id];i++;
    }
    p->num++;
}
void build_ac(node *root){
    root->fail=NULL;
    q[head++]=root;
    while(head!=tail){
        node *temp=q[tail++];
        node *p=NULL;
        for(int i=0;i<N;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 query(node *root,char str2[]){
    int i=0,ans=0;
    node *p=root;
    while(str2[i]){
        int id=str2[i]-'a';
        while(p->next[id]==NULL&&p!=root) p=p->fail;
        p=p->next[id];
        p=(p==NULL)?root:p;
        node *temp=p;
        while(temp!=root&&temp->num!=-1){
            ans+=temp->num;
            temp=temp->fail;
        }
        i++;
    }
    return ans;
}
void del(node *p){
     if(p==NULL)return ;
     for(int i=0;i<26;i++)del(p->next[i]);
     delete p;
}
int main(){
    int T,n,m;
    scanf("%d",&T);
    while(T--){
        node *root=new node();
        head=tail=0;
        scanf("%d%d",&n,&m);
        int k=0;
        for(int i=0;i<n;i++){
            scanf("%s",str);
            int len=strlen(str);
            tmp[i]=len;
            for(int j=0;j<len;j++) str1[k++]=str[j];
        }
        for(int i=0;i<m;i++){
            scanf("%s",str2);
            insert_Trie(str2,root);
        }
        build_ac(root);k=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<tmp[i];j++) str[j]=str1[k++];
            str[tmp[i]]='\0';
            int ans=query(root,str);
            printf("%d\n",ans);
        }
        del(root);
    }
    return 0;
}
时间: 2024-10-06 23:46:39

HDU 5384 AC自动机模版的相关文章

hdu 5384 AC自动机

AC自动机裸题,真的很裸…… 先把前面的 n 个待匹配串存起来,再把后面 m 个模式串构造 AC自动机,然后再一个个询问待匹配串就没了…… 1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 using namespace std; 5 const int maxm=600006; 6 7 char s[100005][10005],word[10005]; 8 int nxt[maxm][26],t

HDU 2222 AC自动机(模版题)

Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 70290    Accepted Submission(s): 23917 Problem Description In the modern time, Search engine came into the life of everybody lik

HDU 2222-Keywords Search-AC自动机模版题

纯粹的模版... 学习模版总会是一个快乐的过程.... #include <cstdio> #include <cstdlib> #include <string> #include <climits> #include <iostream> #include <vector> #include <set> #include <cmath> #include <cctype> #include &l

hdu 2896 AC自动机

// hdu 2896 AC自动机 // // 题目大意: // // 给你n个短串,然后给你q串长字符串,要求每个长字符串中 // 是否出现短串,出现的短串各是什么 // // 解题思路: // // AC自动机,插入单词,构建自动机,然后查询就可以了. // // 感悟: // // 哎,自己AC自动机果然刚学,还只会个模板,自动机构没构建 // 都不知道...继续加油吧~~~FIGHTING! #include <cstdio> #include <cstring> #inc

hdu 2296 aC自动机+dp(得到价值最大的字符串)

Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3180    Accepted Submission(s): 1033 Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a rom

hdu 2457 AC自动机+dp

DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2004    Accepted Submission(s): 1085 Problem Description Biologists finally invent techniques of repairing DNA that contains segments c

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 3065 AC自动机(各子串出现的次数)

病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10324    Accepted Submission(s): 3633 Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的“万恶之源”.这是一个庞大的病毒网站,他有着好多好多的病毒,但

hdu 5880 AC自动机

Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 20 Problem Description Steam is a digital distribution platform developed by Valve Corporation offering