HDU-2222 Keywords Search(AC自动机--模板题)
题目大意:统计一共出现了多少次模板串。
题目分析: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自动机--模板题)的更多相关文章
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- 【HDU 2222】Keywords Search AC自动机模板题
参考iwtwiioi的模板写出来的.上午gty讲的并没有听懂,只好自己慢慢对着模板理解. 在HDU上为什么相同的程序提交有时T有时A!!! 奉上sth神犇的模板(不是这道题): var ch:char ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- Ubuntu anzhuang
zhongwenshurubuzhidaozenmeqiehuan anhzuang Flash apt-get install flashplugin-installer
- centos7中 npm install express 时Error: Cannot find module 'express'错误
费了很大劲最后在网上找到,在自己的工程目录下再次执行npm install express搞定.
- 能源项目xml文件 -- springMVC-servlet.xml -- default-lazy-init
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- yii2-basic后台管理功能开发之一:引入模板
前景:哎没想到用php的yii2框架写了官网……自己选择的框架,跪着也要码完,当年用yii2-basic搭建了官网,原是想偷懒的,没想到还要写后台.... 需求:实现一个可编辑页面图片及新闻内容的后台 ...
- 了解Sql Server的执行计划
前一篇总结了Sql Server Profiler,它主要用来监控数据库,并跟踪生成的sql语句.但是只拿到生成的sql语句没有什么用,我们可以利用这些sql语句,然后结合执行计划来分析sql语句的性 ...
- Server.UrlEncode()方法 空格转换成了+而非%20
在ASP.NET MVC 的Control类里提供了该方法.该方法可以很方便的对字符串进行url编码,但小猪今天却发现其将空格编码后变成了“+”而非JavaScript采用的encodeURIComp ...
- UOJ Test Round 1
第一题: 题目大意: 给出N个字符串,字符串的前面部分都是字母且都是一样的,后面部分是数字,按照后面的数字排序.N<=10000 解题过程: 1.第一题是真良心,一开始的做法是把后面的数字分离出 ...
- Windows 7中无法访问FTP的解决方法
解决: netsh advfirewall set global StatefulFTP disable
- Android 4.2以上的手机USB调试设置
今天遇到一个问题,我手上有两部手机一部是红米.一部是中兴的青漾QY N986,两部手机的Android系统都是4.2.1的,连接到电脑测试,找了半天没有找到设置开发者选项,后来在网上找了半天,才发现g ...
- 获取本地soapUI项目路径
def projectDir = ${projectDir}