链接: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. js取当前周几

    纯javascript取当前周几 var dayNames = new Array("星期天","星期一","星期二","星期三& ...

  2. iOS之UIKit系列教程<一>

    前言:博主接触iOS的编程也有一段时间,今天把有关UI控件的一些知识在这里做一些总结. 申明:此系列文章都是使用目前最新版本swift3.0.1进行讲解的,与其他版本可能略有差异. 一,UIKit之设 ...

  3. jdk 安装 环境变量配置

    右键选择 计算机→属性→高级系统设置→高级→环境变量 1.系统变量→新建 变量名:JAVA_HOME 变量值:(变量值填写你的jdk的安装目录,例如本人是 C:\Program Files\Java\ ...

  4. git误删文件找回方法/git版本回退方法

    使用git命令 git rm css/\*.css 我删掉了css文件夹下所有以.css结尾的文件,那么要怎样才能把文件找回来呢,下面说说方法,删掉其他的文件也是一样的方式找回. 第一步:使用git ...

  5. CentOS7下默认目录安装mono+jexus教程

    一.阅读前须知: 1.本文属于安装完Centos7之后的步骤 2.如果还不了解mono,请点击mono 3.本篇主要内容是使用默认目录安装mono+jexus教程,使用自定义目录请查看使用自定义目录安 ...

  6. 如何在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作

    Phpstorm除了能直接打开localhost文件之外,还可以连接FTP,除了完成正常的数据传递任务之外,还可以进行本地文件与服务端文件的异同比较,同一文件自动匹配目录上传,下载,这些功能是平常ID ...

  7. .net 面试基础题

    Reference Link:http://www.yjbys.com/bbs/326026.html const关键字用来声明编译时常量,readonly用来声明运行时常量 密封类不能同时为抽象类 ...

  8. 基于SSH框架的网上商城的质量属性

    常见质量属性 1.性能 性能就是一个东西有多快,通常指响应时间或延迟. 响应时间:从发出请求到收到响应所用的时间,比如用户点击网页中的超链接或桌面应用程序中的按钮 延迟:消息从A点到B点,通过你的系统 ...

  9. enum 与 enum class

    c++11中引入了新的枚举类型---->强制枚举类型 // unscoped enum: enum [identifier] [: type] {enum-list};  // scoped e ...

  10. Android app AOP添加埋点技术总结

    目标:通过面向切面编程来实现对源代码无侵入的埋点.     方式 能力 缺点 学习曲线   XPosed 运行期hook 能hook自己应用进程的方法: 能hook别的应用的方法: 能hook系统方法 ...