链接: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. BZOJ2730: [HNOI2012]矿场搭建

    传送门 图的连通性相关的必和割点割边之类的有关. 题目要求对于一个无向图,任意一点被删除后,所有点都和某些指定点是联通的. 这道题比较简单的做法就是求出来所有的块.对于一个块,如果块里有两个及两个以上 ...

  2. easyui datagrid 悬浮事件

    easyui的单元格提示窗体  鼠标悬浮事件 function findAllPreven() { var infoname = $('#area').val(); areadatagrid=$('# ...

  3. jquery隐藏侧边栏和折叠侧边栏方法

    两种效果如下所示: 隐藏侧边栏: 折叠侧边栏: 下面,分享隐藏侧边栏实现方法: 实现思路:给body切换class,通过class控制侧边栏和主体部分left 来实现效果 html部分: <di ...

  4. 安装cocoapods及相关问题解决

    申明:本博客大部分内容转载自简书http://www.jianshu.com/p/b64b4fd08d3c,但还有些问题博主在这里做了补充. Mac系统版本:10.12.1 一.什么是CocoaPod ...

  5. JVM:查看java内存情况命令

    jmap (linux下特有,也是很常用的一个命令) 观察运行中的jvm物理内存的占用情况. 参数如下: -heap :打印jvm heap的情况 -histo: 打印jvm heap的直方图.其输出 ...

  6. Java学习笔记15

    do-while循环时while循环的变体语法如下:do{  // 循环体 语句(组);}while(循环继续条件); 如果循环中的语句至少需要执行一次,那么建议使用do-while循环. for循环 ...

  7. CMake

    使用CMake编译跨平台静态库 http://www.tuicool.com/articles/3uu2Yj cmake命令 安装.用法简介 https://fukun.org/archives/04 ...

  8. Apache Shiro 学习记录2

    写完上篇随笔以后(链接).....我也想自己尝试一下写一个Strategy.....Shiro自带了3个Strategy,教程(链接)里作者也给了2个.....我想写个都不一样的策略.....看来看去 ...

  9. Bubble Cup 8 finals D. Tablecity (575D)

    题意: (无输入,纯输出题) 一个城市用1000列2行的格子表示,一个小偷藏在城市的某一处. 在每一小时的开始, 在(X, Y)位置的小偷可以移动到 (X - 1, Y), (X + 1, Y),(X ...

  10. oracle根据某个字段去重实例

    if not object_id('Tempdb..#T') is null drop table #T Go Create table #T([ID] int,[Name] nvarchar(1), ...