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. WORK

    团队展示 队伍信息 队名:小狗队 队长:刘映华(201421122021) 队员:兰运良(201421122030).郭和水(201421122017) 团队项目描述 团队项目描述是基于之前的四则运算 ...

  2. Tornado 网站demo 一

    web服务器的工作过程 创建 listen socket, 在指定的监听端口, 等待客户端请求的到来 listen socket 接受客户端的请求, 得到 client socket, 接下来通过 c ...

  3. 【iOS】swift-获取webView的高度

        func webViewDidFinishLoad(webView: UIWebView) {         let webHeightStr = webView.stringByEvalu ...

  4. 《高级软件测试》11.14.安装和运行Jira

    今日任务完成情况如下: 小段:研究Jira在Linux的安装教程 小费:尝试在Ubuntu下安装Jira 小高:查阅了关于Jira软件的介绍和安装教程,下载准备明天安装,并学习使用 小王:注册Jira ...

  5. Spring-Data-JPA整合MySQL和配置

    一.简介 (1).MySQL是一个关系型数据库系统,是如今互联网公司最常用的数据库和最广泛的数据库.为服务端数据库,能承受高并发的访问量. (2).Spring-Data-Jpa是在JPA规范下提供的 ...

  6. 第一篇:Python入门

    一.编程与编程语言 编程的目的: 计算机的发明,是为了用机器取代/解放人力,而编程的目的则是将人类的思想流程按照某种能够被计算机识别表达方式传递给计算机,从而达到让计算机能够像人脑/电脑一样自动执行的 ...

  7. 【TensorFlow随笔】关于一个矩阵与多个矩阵相乘的问题

    问题描述: Specifically, I want to do matmul(A,B) where  'A' has shape (m,n)  'B' has shape (k,n,p) and t ...

  8. AngularJS1.X学习笔记12-Ajax

    说到Ajax,你一定是思绪万千,想到XMLHttpRequest,$.ajax(),跨域,异步之类的.本文将探讨一下AngularJS的Ajax. 一.一个简单的例子 <!DOCTYPE htm ...

  9. api-gateway实践(05)新网关工作 - 缓存定义

    一.缓存分类 1.服务注册信息 1.1.[GroupCode_VersionCode]对应[Version定义]的缓存                       缓存类型:hash         ...

  10. SpringCloud的服务消费者 (一):(rest+ribbon)访问注册的微服务

    采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,Feign底层调用Ribbon2.注册在EurekaServer中的微服务api,不 ...