hdu 2222 Keywords Search
链接: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的更多相关文章
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search(瞎搞)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- hdu 2222 Keywords Search 模板题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search(AC自动机模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出多个单词,最后再给出一个模式串,求在该模式串中包含了多少个单词. 思路: AC自动机的模板题. ...
- 【刷题】HDU 2222 Keywords Search
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
随机推荐
- 初识JVM--java虚拟机的基本知识
JVM运行时的内存块 pc寄存器:(线程独有)保存线程运行的java方法地址 Java 虚拟接栈(线程独有):用于存储线程的栈帧 Java堆(全局共享):随着Java虚拟机的启动 创建 存储着所有对象 ...
- JMeter 分布式部署
Jmeter 是java 应用,对于CPU和内存的消耗比较大,使用单台机器模拟以千计的并发用户就有些力不从心,甚至会引起JAVA内存溢出错误. 为了让jmeter工具提供更大的负载能力,jmeter短 ...
- Express4+Mongodb超简单入门实例
开始前,请确保mongodb已经能正常工作,安装教程:windows下MongoDB的安装及配置 , 请自行安装配置.下面进入正文: 第一步:命令行创建数据库.表,并插入一条数据 命令如下: //创建 ...
- JSON资料整理
http://www.cnblogs.com/zxlovenet/p/3566802.html
- [Unreal]学习笔记之灯光说明
利用灯光通道,实现局部照亮效果 没有更改Channel之前的效果: 需要将网格物体设置为可移动 将灯光和被照亮物体的Channel设置为同样并且非0零: 设置成功后,就可以实现局部照亮
- 详解SQL盲注测试高级技巧
原文地址: http://www.freebuf.com/articles/web/30841.html
- R语言——绘制半圆形图
好久没发点新的作品了.......也许...... Que sera, seraWhatever will be, will be
- Eclipse 中文的设置
步骤如下:一.下载:在Eclipse官网下载相应版本的中文包. 二.中文包安装:1.解压中文语言包中的两个文件夹至Eclipse文件夹的dropins文件夹中.(目录不要放错)2.安装:方法一:使用命 ...
- [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)
局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...
- 图解SQL的Join 转自coolshell
对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的,有left的,有时候,对于Select出来的结果集是什么样子有点不是很清楚.Codin ...