题意:给出n个单词,一篇文章,询问有几个单词在文章中出现过。

/*
AC自动机的裸题。
题目标号牛的一比。
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 500010
#define M 1000010
using namespace std;
int a[N][],danger[N],point[N],mark[N],q[N],n,size;
char ch[M];
void insert(){
int len=strlen(ch),now=;
for(int i=;i<len;i++){
int t=ch[i]-'a';
if(!a[now][t])a[now][t]=++size;
now=a[now][t];
}
danger[now]++;
}
void acmach(){//构建自动机
int h=,t=;
q[]=;point[]=;
while(h<t){
int now=q[++h];
for(int i=;i<=;i++){
if(!a[now][i])continue;
int k=point[now];
while(!a[k][i])k=point[k];
point[a[now][i]]=a[k][i];//类似于kmp的失败数组
q[++t]=a[now][i];
}
}
}
void solve(){
int len=strlen(ch),k=,ans=;
for(int i=;i<len;i++){
mark[k]=;
int t=ch[i]-'a';
while(!a[k][t])k=point[k];
k=a[k][t];
if(mark[k])continue;
for(int j=k;j;j=point[j]){
ans+=danger[j];
danger[j]=;//每个单词只记录一次
}
}
printf("%d\n",ans);
}
int main(){
int T;scanf("%d",&T);
while(T--){
memset(a,,sizeof(a));
memset(point,,sizeof(point));
memset(danger,,sizeof(danger));
memset(mark,,sizeof(mark));
size=;
for(int i=;i<=;i++)a[][i]=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%s",ch);
insert();
}
acmach();
scanf("%s",ch);
solve();
}
return ;
}

Keywords Search(hdu 2222)的更多相关文章

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

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

  2. ACM学习历程—HDU2222 Keywords Search(字典树)

    Keywords Search Description In the modern time, Search engine came into the life of everybody like G ...

  3. AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)

    首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...

  4. hdu----(2222)Keywords Search(ac自动机)

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

  5. HDU2222 Keywords Search(AC自动机)

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

  6. HDU 2222 Keywords Search (AC自动机)

    题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...

  7. HDU 2222 Keywords Search(AC自动机)题解

    题意:给你几个keywords,再给你一段文章,问你keywords出现了几次. 思路:这里就要用到多模匹配算法AC自动机了,AC自动机需要KMP和字典树的知识,匹配时是在字典树上,失配我们就要用到类 ...

  8. hdu 2222 Keywords Search(AC自动机)

    /* 啥也不说了,直接套模板... */ 1 #include<iostream> #include<map> #include<string> #include& ...

  9. 【HDU】2222 Keywords Search(AC自动机)

    题目 传送门:QWQ 分析 $ AC $自动机模板,黈力的码风真的棒极了,这是我抄他的. 还有 题号不错 代码 #include <cstdio> #include <cstring ...

随机推荐

  1. mstsc 远程序桌面登录的 c#开发

    public AxMSTSCLib.AxMsRdpClient rdpClient; rdpClient = new AxMSTSCLib.AxMsRdpClient(); rdpClient.Doc ...

  2. apt-get 相關設定

    /etc/apt/apt.conf.d/01proxy 若加了以下這行,則 apt-get 都會透過下方網址get Acquire::http::Proxy "http://aptcache ...

  3. cocopod 中添加第三方框架,包含静态库文件,使用svn添加上传

    step one: 进入静态库文件的目录 cd 路径: step two:使用命令添加 svn add 静态库名字; 然后更新一下代码就OK

  4. Angular 2 - 5 分钟快速入门

    原文地址: https://angular.io/docs/ts/latest/quickstart.html 让我们从 0 开始创建一个简单的 Angular 2 应用. 下载任何版本的 angul ...

  5. 【转】HTML5的小知识点小集合

    html5的小知识点小集合 html5知识   1.  Doctype作用?标准模式与兼容模式各有什么区别? (1).<!DOCTYPE>声明位于位于HTML文档中的第一行,处于<h ...

  6. tamtam-nuget-imageserver

    https://bitbucket.org/tamtam-nl/tamtam-nuget-imageserver/src/eaddb1ac943fcaa9e7ef210ed5a5ccf630b8699 ...

  7. 百度地图API简单应用

    在做移动端应用时经常用到百度地图API,百度API有强大的示例和文档,开发之前去百度相关网站注册密钥,很块博主只花了几分钟 百度地图API范例 百度地图API文档说明 例子1:输入特定关键字绘制地图标 ...

  8. 深入理解javascript原型和闭包(7)——原型的灵活性

    在Java和C#中,你可以简单的理解class是一个模子,对象就是被这个模子压出来的一批一批月饼(中秋节刚过完).压个啥样,就得是个啥样,不能随便动,动一动就坏了. 而在javascript中,就没有 ...

  9. mahout 安装测试

    1 下载 在http://archive.apache.org/dist/mahout下载相应版本的mahout 版本,获取官网查看http://mahout.apache.org 相关的信息

  10. [Machine Learning & Algorithm] 决策树与迭代决策树(GBDT)

    谈完数据结构中的树(详情见参照之前博文<数据结构中各种树>),我们来谈一谈机器学习算法中的各种树形算法,包括ID3.C4.5.CART以及基于集成思想的树模型Random Forest和G ...