Sample Input
1
5 6
orz
sto
kirigiri
danganronpa
ooooo
o
kyouko
dangan
ronpa
ooooo
ooooo
 
Sample Output
1
1
0
3
7
 

题意:给n个母串,给m个匹配串,求每个母串依次和匹配串匹配,能得到的数目和

开始用KMP超时了,突然看见AC,把模板改一下就过了- -

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define N 500010
char str[10010], keyword[10005];
char l[100005][10005];
int head, tail; struct node
{
node *fail;
node *next[26];
int count;
node() //init
{
fail = NULL;
count = 0;
for(int i = 0; i < 26; ++i)
next[i] = NULL;
}
}*q[N];
node *root; void insert(char *str) //建立Trie
{
int temp, len;
node *p = root;
len = strlen(str);
for(int i = 0; i < len; ++i)
{
temp = str[i] - 'a';
if(p->next[temp] == NULL)
p->next[temp] = new node();
p = p->next[temp];
}
p->count++;
} void build_ac()
{
q[tail++] = root;
while(head != tail)
{
node *p = q[head++];
node *temp = NULL;
for(int i = 0; i < 26; ++i)
{
if(p->next[i] != NULL)
{
if(p == root)
p->next[i]->fail = root;
else
{
temp = p->fail;
while(temp != NULL)
{
if(temp->next[i] != NULL)
{
p->next[i]->fail = temp->next[i];
break;
}
temp = temp->fail;
}
if(temp == NULL)
p->next[i]->fail = root;
}
q[tail++] = p->next[i]; //入队
}
}
}
} int query()
{
int index, len, result;
node *p = root; //Tire入口
result = 0;
len = strlen(str);
for(int i = 0; i < len; ++i)
{
index = str[i] - 'a';
while(p->next[index] == NULL && p != root)
p = p->fail;
p = p->next[index];
if(p == NULL)
p = root;
node *temp = p;
while(temp != root && temp->count != -1)
{
result += temp->count;
//temp->count = -1;
temp = temp->fail;
}
}
return result;
} int main()
{
int ncase, num,n;
scanf("%d", &ncase);
while(ncase--)
{
head= tail = 0;
root = new node();
scanf("%d%d", &n,&num);
getchar();
for(int i = 1;i <= n;i++)
gets(l[i]);
for(int i = 0; i < num; ++i)
{
gets(keyword);
insert(keyword);
}
build_ac();
for(int i = 1; i <= n; i++)
{
memccpy(str,l[i],0,sizeof(l[i]));
//build_ac();
printf("%d\n", query());
}
}
return 0;
}

  

2015 多校联赛 ——HDU5384(AC自动机)的更多相关文章

  1. 2015 多校联赛 ——HDU5334(构造)

    Virtual Participation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  2. 2015 多校联赛 ——HDU5302(构造)

    Connect the Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. 2015 多校联赛 ——HDU5294(最短路,最小切割)

    Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  4. 2015 多校联赛 ——HDU5325(DFS)

    Crazy Bobo Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  5. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  6. 2015 多校联赛 ——HDU5323(搜索)

    Solve this interesting problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. 2015 多校联赛 ——HDU5319(模拟)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  8. 2015 多校联赛 ——HDU5301(技巧)

    Your current task is to make a ground plan for a residential building located in HZXJHS. So you must ...

  9. 2015 多校联赛 ——HDU5303(贪心)

    Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Other ...

随机推荐

  1. System V IPC 之消息队列

    消息队列和共享内存.信号量一样,同属 System V IPC 通信机制.消息队列是一系列连续排列的消息,保存在内核中,通过消息队列的引用标识符来访问.使用消息队列的好处是对每个消息指定了特定消息类型 ...

  2. JQ 上传文件(单个,多个,分片)

    最原始的方式: 前端代码: <div> <span>最原始的方式</span><br /> <span>条件1:必须是 post 方式< ...

  3. JS的if和switch

    var aa=parseInt(prompt("请输入你的年龄")); //定义输入 if(aa<18){ //输出小于18,返回值少年 alert("少年&quo ...

  4. Mysql数据库mys和ora库的备份与恢复脚本

    !/bin/bash Time=$(date +%Y%md%H%M%S) Back_dir="$HOME/mysqlback/${Time}" function Detect_u_ ...

  5. istio入门(04)istio的helloworld-部署构建

    参考链接: https://zhuanlan.zhihu.com/p/27512075 安装Istio目前仅支持Kubernetes,在部署Istio之前需要先部署好Kubernetes集群并配置好k ...

  6. Linux实战案例(1)CentOS修改主机名(hostname)

    1.临时修改主机名 显示主机名: oracle@localhost:~$ hostname localhost 修改主机名: oracle@localhost:~$ sudo hostname orc ...

  7. CURL学习总结(1)

    1.curl是什么?         百度百科定义:     curl是利用URL语法在命令行方式下工作的开源文件传输工具.它被广泛应用在Unix.多种Linux发行版中,并且有DOS和Win32.W ...

  8. docker实践3

    我的docker学习笔记3   $docker run ubuntu echo'hello world' $docker run -i -t ubuntu /bin/bash #ps -ef #exi ...

  9. apigw鉴权分析(1-4)新浪微博开放平台 - 鉴权分析

    一.访问入口 http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E 微博开放接口的调用,如 ...

  10. Linux实战案例(5)关闭Centos的防火墙

    1.检查防火墙的状态 [root@LxfN1 ~]# service iptables status表格:filterChain INPUT (policy ACCEPT)num target pro ...