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 ...
随机推荐
- 北京Uber优步司机奖励政策(4月8日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 在ubuntu trusty下安装python的rasterio库
就这些吧.. apt-get update -y apt-get install -y software-properties-common add-apt-repository ppa:ubuntu ...
- 05-JVM对象探秘
一.对象的内存布局 以Hotspot虚拟机为例,对象在内存中的结构可以分为三部分:对象头(header).实例数据(instance data).对齐填充(padding). 1.1. ...
- 用IDEA编写spark的WordCount
我习惯用Maven项目 所以用IDEA新建一个Maven项目 下面是pom文件 我粘上来吧 <?xml version="1.0" encoding="UTF-8& ...
- 骰子涂色 (Cube painting,UVa 253)
题目描述:算法竞赛入门习题4-4 题目思路:1.旋转其中一个骰子进行匹配 2.进行遍历,如果匹配,就进行相对面的匹配 3.三个对立面都匹配即是一样等价的 //没有按照原题的输入输出 #include ...
- hdu刷题2
hdu1021 给n,看费波纳列数能否被3整除 算是找规律吧,以后碰到这种题就打打表找找规律吧 #include <stdio.h> int main(void) { int n; whi ...
- 一键部署pxe环境
系统:Centos6.5 环境:VMware Workstation12 #!/bin/bash # Please prepare CentOS ISO image first # root pass ...
- Python3 Tkinter-Message
1.创建 from tkinter import * root=Tk() Message(root,text='hello Message').pack() root.mainloop() 2.属性 ...
- Martian Addition
In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are ...
- 软件测试基础-Homework1
The error was in my graduate work which was about game development.I broadcast some messages to the ...