题意:给你n个单词,再给你一串字符,求在字符中有多少个单词出现过

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 10010
#define MAXLEN 1000010
struct node{
node *child[26];
node *fail;
int count;
void init(){
for(int i=0;i<26;i++)
child[i]=NULL;
count=0;
fail=NULL;
}
}*q[50*N];
node *root;
int head,tail;
void insert(char *str){//构建前缀树
node *now,*next;
int i=0,t;
now=root;
while(str[i]){
t=str[i]-'a';
if(now->child[t]!=NULL)
now=now->child[t];
else{
next=new node;
next->init();
now->child[t]=next;
now=next;
}
i++;
}
now->count++;
}
void build_fail(){
int i;
node *now,*p;
head=tail=0;
now=root;
q[tail++]=now;
while(head<tail){
now=q[head];
for(i=0;i<26;i++){
if(now->child[i]==NULL) continue;
if(now==root) now->child[i]->fail=root;
else{
p=now->fail;
while(p!=NULL){
if(p->child[i]!=NULL){
now->child[i]->fail=p->child[i];
break;
}
p=p->fail;
}
if(p==NULL) now->child[i]->fail=root;
}
q[tail++]=now->child[i];
}
head++;
}
}
int query(char *str){
node *now,*temp;
int i,t,cnt;
i=cnt=0;
now=root;
while(str[i]){
t=str[i]-'a';
while(now->child[t]==NULL&&now!=root) now=now->fail;
now=now->child[t];
if(now==NULL) now=root;
temp=now;
while(temp!=NULL&&temp->count!=-1){
cnt+=temp->count;
temp->count=-1;
temp=temp->fail;
}
i++;
}
return cnt;
}
int main(int argc, char** argv) {
int t,n;
char str[MAXLEN];
scanf("%d",&t);
while(t--){
scanf("%d",&n);
root=new node;
root->init();
while(n--){
scanf("%s",str);
insert(str);
}
build_fail();
scanf("%s",str);
printf("%d\n",query(str));
}
return 0;
}

hdu 2222 Keywords_ac自动机模板的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. HDU 2222 & ac自动机模板

    题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...

  3. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  4. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  5. HDU 2222 (AC自动机)

    HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...

  6. hdu 2222 ac自动机更新模板 for onSite contest

    http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...

  7. HDU 2222 ----AC自动机

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  8. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  9. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

随机推荐

  1. VS2008远程调试方法

    在网上找了好多资料才把这个调试环境搭好,下面总结一下: 先说明两个概念: 1.      目标机:远程需要调试的机子,也就是被调试程序exe所在的机子,该机子可以安装VS2008或者不安装vs2008 ...

  2. MongoDB 复制

    副本集:简单说,副本集就是有自动故障恢复功能的主从集群.主从集群和副本集最为明显的区别:副本集没有固定的“主节点”.整个集群会选出一个主节点.当其不能工作时,变更到其他节点.二者看起来非常相似:副本集 ...

  3. 网易云课堂_C++开发入门到精通_章节6:多态

    课时33示例--为多态基类声明虚析构函数 微软校园招聘笔试题 #include <iostream> class Base { public: char Value() { return ...

  4. Unity position和localposition

    1. position是根据世界原点为中心 2. localPosition是根据父节点为中心,如果没有父节点,localpositon和position是没有区别的 3.选中一个物体左上角Globa ...

  5. Spring的MethodInvokingFactoryBean

    通过MethodInvokingFactoryBean 可以向某静态方法注入参数. 如: <bean class="org.springframework.beans.factory. ...

  6. Python 练习 —— 2048

    1. 引言 2048 这段时间火的不行啊,大家都纷纷仿造,"百家争鸣",于是出现了各种技术版本号:除了手机版本号,还有C语言版.Qt版.Web版.java版.C#版等,刚好我接触P ...

  7. Ubuntu亮度无法调节或调节无法保存的问题

    装了搜狗输入法之后,系统设置里面的很多软件都没有了.以前屏幕太亮在电源里面可以调节,现在不行了.没办法,只能找其他的办法了. 在网上查了很多资料,经自己的实验,找到了一个成功的方法. 首先进入 /sy ...

  8. JQ 更改li 颜色

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. jdbc——CallableStatement,cst调用存储过程

    package CST对象调用存储过程; import java.sql.CallableStatement; import java.sql.Types; import org.junit.Test ...

  10. 2014年1月24日 Oracle 事务导读

    形象举例:  从 A 账户向 B 账户转账 10000 元 步骤: 1. A - 10000 2. B + 10000 事务的作用就是确保这两步无误执行后提交,若有一个执行不成功则失败. 结束事务的5 ...