链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222

思路:裸AC自动机,直接贴代码做模板

 #include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<queue>
using namespace std;
char str[+]; struct node
{
int count;
struct node *next[];
struct node *fail;
void init()
{
int i;
for(i=;i<;i++)
next[i]=NULL;
count=;
fail=NULL;
}
}*root;
void insert()
{
int len,k;
node *p=root;
len=strlen(str);
for(k=;k<len;k++)
{
int pos=str[k]-'a';
if(p->next[pos]==NULL)
{
p->next[pos]=new node;
p->next[pos]->init();
p=p->next[pos];
}
else
p=p->next[pos];
}
p->count++;
}
void getfail()
{
int i;
node *p=root,*son,*temp;
queue<struct node *>que;
que.push(p);
while(!que.empty())
{
temp=que.front();
que.pop();
for(i=;i<;i++)
{
son=temp->next[i];
if(son!=NULL)
{
if(temp==root) {son->fail=root;}
else
{
p=temp->fail;
while(p)
{
if(p->next[i])
{
son->fail=p->next[i];
break;
}
p=p->fail;
}
if(!p) son->fail=root;
}
que.push(son);
}
}
}
}
void query()
{
int len,i,cnt=;
len=strlen(str);
node *p,*temp;
p=root;
for(i=;i<len;i++)
{
int pos=str[i]-'a';
while(!p->next[pos]&&p!=root) p=p->fail;
p=p->next[pos];//
if(!p) p=root;//
temp=p;
/*不要用*temp=*p 因为*p表示一个node,而*temp也表示一个node 但是由于*temp没有分配空间 所以是不能进行赋值的 但是可以用temp指针去指向p*/
while(temp!=root)
{
if(temp->count>=)
{
cnt+=temp->count;
temp->count=-;
}
else break;
temp=temp->fail;
}
}
printf("%d\n",cnt);
}
int main()
{
int cas,n;
scanf("%d",&cas);
while(cas--)
{
root=new node;
root->init();
root->fail=NULL;
scanf("%d",&n);
int i;
getchar();
for(i=;i<n;i++)
{
gets(str);
insert();
}
getfail();
gets(str);
query();
}
return ;
}

hdu 2222 Keywords Search的更多相关文章

  1. HDU 2222 Keywords Search(查询关键字)

    HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...

  2. HDU 2222 Keywords Search(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  3. HDU 2222 Keywords Search(瞎搞)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  4. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  5. hdu 2222 Keywords Search 模板题

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  6. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  7. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  8. HDU 2222 Keywords Search(AC自动机模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...

  9. 【刷题】HDU 2222 Keywords Search

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

随机推荐

  1. 【技巧】为ComboBox添加自动提示

    一.需求来源 最近有个项目用到了ComboBox控件,在演示的时候,要对Word文档中选中部分添加符合DocBook标准的标签,这些标签是DocBook中元素的集合,数据量不多,大概170多个吧,但是 ...

  2. 关于php编程的一些小技巧

    1. NULL,0和空的区分判断 (1)0与空的区分 <?php  $a = 0;if($a==''){ echo '此判断条件不能区分';}else{ echo '此判断条件可以区分';} ? ...

  3. IT这一行,如可高速下载国外资源之迅雷设置免费SSH代理下载国外资源

    本文转自SUN'S BLOG 原文地址:IT这一行,如可高速下载国外资源之迅雷 我们这些做IT这一行的人,经常,下载一些国外的一些资源,可是让人蛋碎的是,往往这些资源下载都慢的像蜗牛,真的让人无法忍受 ...

  4. Service Provider Interface

    @(Java)[SPI] Service Provider Interface API的一种设计方法,一般用于一些服务提供给第三方实现或者扩展,可以增强框架的扩展或者替换一些组件. 结构 Servic ...

  5. ASP.NET MVC移动M站建设-使用51Degree 移动设备的识别

    上一篇,介绍了移动M站的建设.说的很简单.觉得好像也没把M站给讲清楚.估计是对移动M站 认识还不够深刻吧.这里,在讲一讲51Degree 这个组件. 51degrees 号称是目前最快.最准确的设备检 ...

  6. Event Loop个人理解

    javascript引擎单线程程序,其执行同步操作会按顺序向下执行,执行异步操作则使用事件循环模型. js引擎线程遇到异步操作,会将异步操作交给对应的观察者, 异步操作包括: dom事件 click, ...

  7. easyui如何动态改变列的编辑属性

    动态改变列的编辑属性 var tt=$('#dg').datagrid('getColumnOption', 'yearContent'); //通过列名获得此列 tt.editor={type:'t ...

  8. 【mysql函数】FIND_IN_SET函数用法

    当你的数据存储为一下格式时,想查出带有某个id的所有数据时,FIND_IN_SET这个函数可以帮到你. ',C_BranchId)

  9. mysql 主主复制搭建用的命令

    # mysql 容器的启动docker run -d -p 3306:3306 -v /database_files/products/data:/var/lib/mysql -v /database ...

  10. [Machine Learning & Algorithm] 神经网络基础

    目前,深度学习(Deep Learning,简称DL)在算法领域可谓是大红大紫,现在不只是互联网.人工智能,生活中的各大领域都能反映出深度学习引领的巨大变革.要学习深度学习,那么首先要熟悉神经网络(N ...