题目大意:统计一共出现了多少次模板串。

题目分析:AC自动机的模板题。不过这题有坑,相同的模板串不能只算一次。

代码如下:

# include<iostream>
# include<cstdio>
# include<queue>
# include<map>
# include<string>
# include<cstring>
# include<algorithm>
using namespace std; const int N=10000; int ch[N*50+5][26];
int val[N*50+5];
int last[N*50+5];
int f[N*50+5];
bool mark[N*50+5];
map<string,int>mp;
string str[N+5]; struct AC
{
int cnt,res;
void init()
{
res=cnt=0;
memset(ch,0,sizeof(ch));
memset(val,0,sizeof(val));
memset(mark,false,sizeof(mark));
}
int idx(char c)
{
return c-'a';
}
void insert(string str,int v)
{
int u=0;
int n=str.size();
for(int i=0;i<n;++i){
int c=idx(str[i]);
if(!ch[u][c]) ch[u][c]=++cnt;
u=ch[u][c];
}
val[u]=v;
}
void getFail()
{
queue<int>q;
f[0]=0;
for(int i=0;i<26;++i){
int u=ch[0][i];
if(!u) continue;
f[u]=0;
q.push(u);
last[u]=0;
}
while(!q.empty())
{
int r=q.front();
q.pop();
for(int i=0;i<26;++i){
int u=ch[r][i];
if(!u){
ch[r][i]=ch[f[r]][i];
}else{
q.push(u);
int v=f[r];
while(v&&!ch[v][i]) v=f[v];
f[u]=ch[v][i];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
}
void ac(string str)
{
int n=str.size();
int j=0;
for(int i=0;i<n;++i){
int c=idx(str[i]);
j=ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
}
void print(int u)
{
if(u){
if(!mark[val[u]]) res+=mp[str[val[u]]];
mark[val[u]]=true;
print(last[u]);
}
}
int getResult()
{
return res;
}
}ac; char s[N*100+5]; int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
mp.clear();
scanf("%d",&n);
ac.init();
for(int i=1;i<=n;++i){
cin>>str[i];
++mp[str[i]];
ac.insert(str[i],i);
}
scanf("%s",s);
ac.getFail();
ac.ac(s);
printf("%d\n",ac.getResult());
}
return 0;
}

  

HDU-2222 Keywords Search(AC自动机--模板题)的更多相关文章

  1. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  2. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  3. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  4. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  5. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

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

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  7. 【HDU 2222】Keywords Search AC自动机模板题

    参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...

  8. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  9. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

  10. Keywords Search(AC自动机模板)

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

随机推荐

  1. powerdesinger中建立一个表后,出现Existence of index的警告

    可以不检查 Existence of index 这项,也就没有这个警告错误了!意思是说没有给表建立索引,而一个表一般至少要有一个索引,这是一个警告,不用的话对执行没有影响~ 转载:http://bl ...

  2. Jmockit使用

    引用单元测试中mock的使用及mock神器jmockit实践中的java单元测试中各种Mock框架对比,就能明白JMockit有多么强大: JMockit是基于JavaSE5中的java.lang.i ...

  3. Centos7下搭建LAMP平台环境 (转载)

     1.启用Apache(httpd) Centos7默认已经安装httpd服务,只是没有启动.如果你需要全新安装,可以yum install -y httpd 启动服务:systemctl start ...

  4. 利用SVN进行任意文件对比

    都知道SVN可以比较已经上传的文件的内容,看到两个文件有什么不同的地方. 但是有时候并不想上传想要比较的文件,能不能利用SVN这样一个功能去比较别的两个文件呢? 琢磨来琢磨去, 发现只要在资源管理器里 ...

  5. DES MAC PIN HEX

    /* void DesEncrypt( UCHAR * auchInput,UCHAR * auchKey,UCHAR * auchOutput=NULL); Function: DesEncrypt ...

  6. POJ 2184 01背包+负数处理

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10200   Accepted: 3977 D ...

  7. scp不可用:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED

    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! After doing ssh are you seeing this error.No proble ...

  8. 使用Jsoup 抓取页面的数据

    需要使用的是jsoup-1.7.3.jar包   如果需要看文档我下载请借一步到官网:http://jsoup.org/ 这里贴一下我用到的 Java工程的测试代码 package com.javen ...

  9. 用python定时文章发布wordpress

    用python定时文章发布wordpress: 流程: 采集 - 筛选文章 - wordpress文章发布. wordpress文章发布代码:python利用模块xmlrpclib发布文章非常便捷,省 ...

  10. BCG界面库下的Windows8 UI界面样式www.webui8.com

    BCG界面库下的Windows8 UI界面样式(Metro风格)控件主要有以下一些功能: 规则的大块磁贴 支持完整键盘导航 Tile组 标题(Caption) 标题按钮(Caption buttons ...