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. 微信小程序轮播图

    swiper标签 <!--index.wxml--> <swiper class="swiper" indicator-dots="true" ...

  2. 用anaconda安装最新的TensorFlow版本

    Google发布了TensorFlow1.4正式版 在anaconad搜索依旧是1.2的版本,通过一番查阅,找到了方法 1,打开anaconda-prompt 2,激活你要安装的环境 activate ...

  3. 使用JavaScript实现一个俄罗斯方块

    清明假期期间,闲的无聊,就做了一个小游戏玩玩,目前游戏逻辑上暂未发现bug,只不过样子稍微丑了一些-.-项目地址:https://github.com/Jiasm/tetris在线Demo:http: ...

  4. C# 使用 GDI+ 实现添加中心旋转(任意角度)的文字

    这篇文章是 GDI+ 总结系列的第三篇,如果对 GDI+ 的基础使用不熟悉的朋友可以先看第一篇文章<C# 使用 GDI+ 画图>. 需求 需求是要实现给图片添加任意角度旋转的文字,文字的旋 ...

  5. sts中maven

    建立一个maven web的工程 网上有很多关于maven的下载,配置等,我这里就不多说了. 下面介绍主要介绍关于在sts中建立一个maven时最开始出现的错误问题. 创建maven工程 file-& ...

  6. C# 基于Bootstrap的三级联动

    实现效果如图: 一.声明市.县.乡对应的下拉控件select <div class="form-group"> <label class="col-sm ...

  7. Django admin 中抛出 'WSGIRequest' object has no attribute 'user'的错误

    这是Django版本的问题,1.9之前,中间件的key为MIDDLEWARE_CLASSES, 1.9之后,为MIDDLEWARE.所以在开发环境和其他环境的版本不一致时,要特别小心,会有坑. 将se ...

  8. C语言的一些输出格式

    %e      printf()的一种输出格式 科学表示的一种浮点数 1.24==1.240000e+000     1240000==1.240000e+006                   ...

  9. scrapy中的request

    scrapy中的request 初始化参数 class scrapy.http.Request( url [ , callback, method='GET', headers, body, cook ...

  10. beautiful soup

    beautiful soup是一个可以从html或者xml文件中提取数据的python库,它能够通过你喜欢的转换器实现惯用的文档导航.查找.修改文档的方式. beautiful soup 会帮你节省数 ...