AC自动机裸题
HDU 2222 Keywords Search
模板题。对模式串建立AC自动机然后在trie树上找一遍目标串即可。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
inline int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int trie[N][], top, fail[N]; void init(){top=; mem(trie[],);}
void ins(char *s){
int rt, nxt;
for (rt=; *s; rt=nxt, ++s){
nxt=trie[rt][*s-];
if (!nxt) mem(trie[top],), trie[rt][*s-]=nxt=top++;
}
++trie[rt][];
}
void makefail(){
int u, v, bg, ed;
static int q[N];
fail[]=bg=ed=;
FO(i,,) if ((v=trie[][i])) fail[q[ed++]=v]=;
while (bg<ed){
u=q[bg++];
FO(i,,) {
if ((v=trie[u][i])) fail[q[ed++]=v]=trie[fail[u]][i];
else trie[u][i]=trie[fail[u]][i];
}
}
}
int ac(char *s){
static bool vis[N];
int ans=; mem(vis,);
for (int i=; *s; ++s) {
i=trie[i][*s-];
for (int j=i; j&&!vis[j]; j=fail[j]) vis[j]=, ans+=trie[j][];
}
return ans;
}
char str[];
int main ()
{
int T, n;
scanf("%d",&T);
while (T--) {
scanf("%d",&n); init();
while (n--) scanf("%s",str), ins(str);
scanf("%s",str); makefail();
printf("%d\n",ac(str));
}
return ;
}
HDU 2896 病毒侵袭
把AC自动机的节点维护的东西改一改就行了。不过由于目标串很多,每次都fillchar一遍vis数组会超时。使用以前学的黑科技按时间戳更新vis即可AC。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
inline int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int trie[N][], top, fail[N], ans[], q[N];
bool vis[N];
queue<int>Q; void init(){top=; mem(trie[],);}
void ins(char *s, int id){
int rt, nxt;
for (rt=; *s; rt=nxt, ++s){
nxt=trie[rt][*s-];
if (!nxt) mem(trie[top],), trie[rt][*s-]=nxt=top++;
}
trie[rt][]=id;
}
void makefail(){
int u, v, bg, ed;
fail[]=bg=ed=;
FO(i,,) if ((v=trie[][i])) fail[q[ed++]=v]=;
while (bg<ed){
u=q[bg++];
FO(i,,) {
if ((v=trie[u][i])) fail[q[ed++]=v]=trie[fail[u]][i];
else trie[u][i]=trie[fail[u]][i];
}
}
}
void ac(char *s){
int v;
while (!Q.empty()) v=Q.front(), Q.pop(), vis[v]=;
for (int i=; *s; ++s) {
i=trie[i][*s-];
for (int j=i; j&&!vis[j]; j=fail[j]) {
vis[j]=; Q.push(j);
if (trie[j][]) ans[++ans[]]=trie[j][];
}
}
}
char str[];
int main ()
{
int n, m, total=;
scanf("%d",&n); init();
getchar();
FOR(i,,n) gets(str), ins(str,i);
makefail();
scanf("%d",&m);
getchar();
FOR(i,,m) {
gets(str); ans[]=; ac(str);
if (ans[]) ++total;
else continue;
printf("web %d:",i);
sort(ans+,ans+ans[]+);
FOR(j,,ans[]) printf(" %d",ans[j]);
putchar('\n');
}
printf("total: %d\n",total);
return ;
}
HDU 3065 病毒侵袭持续中
统计每个模式串的出现次数,不用vis数组就行了。这题由于模式串的特殊性,只需要建立26叉树即可。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
inline int Scan() {
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... int trie[N][], top, fail[N], num[];
char ss[][]; void init(){top=; mem(trie[],);}
void ins(char *s, int id){
int rt, nxt;
for (rt=; *s; rt=nxt, ++s){
nxt=trie[rt][*s-'A'];
if (!nxt) mem(trie[top],), trie[rt][*s-'A']=nxt=top++;
}
trie[rt][]=id;
}
void makefail(){
int u, v, bg, ed;
static int q[N];
fail[]=bg=ed=;
FO(i,,) if ((v=trie[][i])) fail[q[ed++]=v]=;
while (bg<ed){
u=q[bg++];
FO(i,,) {
if ((v=trie[u][i])) fail[q[ed++]=v]=trie[fail[u]][i];
else trie[u][i]=trie[fail[u]][i];
}
}
}
void ac(char *s){
int v;
for (int i=; *s; ++s) {
if (*s<'A'||*s>'Z') {i=; continue;}
i=trie[i][*s-'A'];
for (int j=i; j; j=fail[j]) if ((v=trie[j][])) ++num[v];
}
}
char str[];
int main ()
{
int n;
while (~scanf("%d",&n)) {
init(); mem(num,);
getchar();
FOR(i,,n) gets(ss[i]), ins(ss[i],i);
makefail();
gets(str);
ac(str);
FOR(i,,n) {
if (!num[i]) continue;
printf("%s: %d\n",ss[i],num[i]);
}
}
return ;
}
AC自动机裸题的更多相关文章
- BZOJ-3940:Censoring(AC自动机裸题)
Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have p ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 3065 AC自动机 裸题
中文题题意不再赘述 注意 失配数组 f 初始化一步到位 #include <stdio.h> #include <string.h> #include <queue&g ...
- HDU 2896 AC自动机 裸题
中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU3695(AC自动机模板题)
题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...
随机推荐
- bos开发日记一
BOS项目 第1天 项目12天安排: 1-2天:项目概述.搭建开发环境.主页设计.持久层和表现层设计 3-6天:项目业务开发(取派员.区域.分区.定区.业务受理)---整个项目分为基础设置.取派.中转 ...
- msys2-x86_64 Mingw64 编译openssl
windows 刚开始编译时提示找不到gcc 添加环境变量export PATH=$PATH:/mingw64/bin$source /etc/profile 将openssl源码复制到C:\msys ...
- 天津Uber优步司机奖励政策(1月11日~1月17日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 每天看一片代码系列(三):codepen上一个音乐播放器的实现
今天我们看的是一个使用纯HTML+CSS+JS实现音乐播放器的例子,效果还是很赞的: codePen地址 HTML部分 首先我们要思考一下,一个播放器主要包含哪些元素.首先要有播放的进度信息,还有播放 ...
- 6 线程threading
1.第1种方式:threading模块 1)单线程执行 #-*- coding:utf-8 -*- import time def main(): print("我错了...") ...
- CF 1051 F. The Shortest Statement
F. The Shortest Statement http://codeforces.com/contest/1051/problem/F 题意: n个点,m条边的无向图,每次询问两点之间的最短路. ...
- 9、Java ConcurrentModificationException异常原因和解决方法
Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...
- 安装虚拟机以及ubuntu
近期要用到linux,所以开始学习.想要在这里记录自己的学习之路 1.安装虚拟机以及装ubuntu系统 虚拟机就相当于一台电脑,电脑里得有系统,我们选择了ubuntu作为操作系统. 安装看下面的博客操 ...
- 使用redux-actions优化actions管理
redux-actions的api很少,有三个createAction(s) handleASction(s) combineActions 主要用到createAction去统一管理actio ...
- Jmeter使用之:高效组织接口自动化用例技巧
Jmeter怎么使用的文章多如牛毛,但怎么组织好测试用例,则几乎很难看到.在本文,我将把Jmeter下怎么组织测试用例的几点心得分享给大家,希望能给你一些帮助或启示. 1.善用“逻辑控制器”中的“简单 ...