链接: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. 10个常见的Node.js面试题

    如果你希望找一份有关Node.js的工作,但又不知道从哪里入手评测自己对Node.js的掌握程度. 本文就为你罗列了10个常见的Node.js面试题,分别考察了Node.js编程相关的几个主要方面. ...

  2. coreseek操作

    开启服务$  /usr/local/coreseek/bin/searchd -c /usr/local/coreseek/etc/csft.conf 重新索引: /usr/local/coresee ...

  3. Linux 新手的学习笔记

    系统分区: MBR分区(主分区不超过4个,单个分区不超过2TB) fdisk -l fdisk /dev/sdb n(添加一个分区)p(添加一个主分区,主分区)e(添加一个扩展分区)l(添加一个逻辑分 ...

  4. 无法对 索引 'IndexName' 执行 删除,因为它不存在,或者您没有所需的权限。

    先写结论: 语法:  DROP INDEX 表名.索引名 如果索引明明存在..却报标题上那个错..请直接去看是否是表名与库中的不一样.. 请一定去检查一下..别问我为什么这么说.. if exists ...

  5. [Asp.net mvc]OutputCacheAttribute

    什么是Cache? 缓存在web应用中是一种以空间换去时间的技术,把频繁访问并且不经常变化的数据存储到内存中,以达到快速访问的目的.在web应用中是比较常见的优化方式. OutputCacheAttr ...

  6. struts.xml

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    " ...

  7. V4L2框架分析学习一

    转载于http://www.techbulo.com/1193.html 1.概述 Video4Linux2是Linux内核中关于视频设备的内核驱动框架,为上层的访问底层的视频设备提供了统一的接口.凡 ...

  8. ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合

    一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...

  9. jQuery学习之prop和attr的区别示例介绍

    1..prop( propertyName ) 获取匹配集合中第一个元素的Property的值 2. .prop( propertyName, value ) .prop( map ) .prop( ...

  10. tomcat 快速部署静态文件

    server.conf配置: <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software ...