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 ...
随机推荐
- [原创]用python检测LVS real server状态实现HTTP高可用
import httplib import os import time def check_http(i): try: conn=httplib.HTTPConnection(i, 80, time ...
- MySQL高级第三章——查询截取分析
一.查询分析 1.永远小表驱动大表 使用小的数据集驱动大的数据集. //复习 EXISTS 的知识:SELECT ... FROM tb WHERE EXISTS (subquery) 是因为前后数据 ...
- CSS3中的动画
CSS3中的动画包括两种: Transition(过渡) Animation(动画) 这两种方法都可以让元素动起来,功能类似,但是稍有区别: Transition只定义某一个元素的开始状态和结束状态 ...
- 1030: [JSOI2007]文本生成器
1030: [JSOI2007]文本生成器 https://www.lydsy.com/JudgeOnline/problem.php?id=1030 分析: AC自动机+dp. 正难则反,求满足的, ...
- Mac brew安装redis
1.安装redis $ brew install redis Error:Failed to download resource "reds" // 下载reds失败 不过不需要 ...
- coolshell里的一些c++文章
c++数组不支持多态 https://coolshell.cn/articles/9543.htmlwhy gcc in c++ https://airs.com/ian/cxx-slides.pdf ...
- 「日常训练」Uncle Tom's Inherited Land*(HDU-1507)
题意与分析 题意是这样的:给你一个\(N\times M\)的图,其中有一些点不能放置\(1\times 2\)大小的矩形,矩形可以横着放可以竖着放,问剩下的格子中,最多能够放多少个矩形. 注意到是\ ...
- uvaoj1225Digit Counting(暴力)
Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequen ...
- Unity编辑器 - 使用GL绘制控件
Unity编辑器 - 使用GL绘制控件 控件较为复杂时,可能造成界面卡顿,在EditorGUI中也可以灵活使用GL绘制来提升性能. 以绘制线段为例: using UnityEngine; using ...
- gitignore 文件生效办法
.gitignore 可以添加一些不加入git版本控制的文件 比如一些测试文件.因人而异的配置信息等等 .gitignore 文件展示如下 /.idea/target//.classpath /.pr ...