【题解】AC自动机题解合集
最近貌似大家都在搞字符串?很长一段时间都没有写博客了……还是补一补坑吧。
感觉AC自动机真的非常优美了,通过在trie树上建立fail指针可以轻松解决多模匹配的问题。实际上在AC自动机上的匹配可以看做是拿着一个串在上面跑,在固定一个左端点的时候尽量地向右匹配。如果发现实在是匹配不下去了,就向右挪动左端点实现新的匹配(跳转fail指针)。基本上根据这一条理解,就可以解决大部分的问题了。
AC自动机裸考的不多,除了匹配之外一个较常见的搭配就是和DP结合在一起。但本质上依然是在匹配串,只要根据fail指针的指向去转移dp状态即可。
1.[HNOI2006] 最短母串问题
非常明确的指向:n <= 12。一眼状压,我们建立状态 \(f[u][S]\) 表示在匹配到AC自动机上的状态 \(u\) 的时候,已经匹配上的串为 \(S\) 集合时的方案数。也许会有疑问:那么怎么保证步数最短&能够输出字典序最小的解?注意AC自动机上相邻状态的转移意味着添加了一个字符,这样我们可以方便地BFS转移。优先转移小的字符可以保证字典序最小,发现答案后直接输出即可。
#include <bits/stdc++.h>
using namespace std;
#define maxn 605
#define maxc 55
#define maxm 5000
int n, tot, cnt, Ans[maxn];
int ch[maxn][], fail[maxn];
int mark[maxn], bits[];
char s[maxc]; struct node
{
int b;
short a, c;
node(short _a = , int _b = , short _c = -)
{ a = _a, b = _b, c = _c; }
}g[maxn][maxm], ans; queue <node> q; void Ins(int x)
{
int L = strlen(s + ), p = ;
for(int i = ; i <= L; i ++)
{
int u = s[i] - 'A';
if(!ch[p][u]) ch[p][u] = ++ tot;
p = ch[p][u];
}
mark[p] = (mark[p] | bits[x - ]);
} void Build()
{
queue <int> q;
for(int i = ; i < ; i ++)
if(ch[][i]) q.push(ch[][i]);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i = ; i < ; i ++)
{
if(ch[u][i])
{
fail[ch[u][i]] = ch[fail[u]][i];
mark[ch[u][i]] |= mark[fail[ch[u][i]]];
q.push(ch[u][i]);
}
else ch[u][i] = ch[fail[u]][i];
}
}
} void DP()
{
q.push(node(, )); g[][].c = -;
while(!q.empty())
{
node now = q.front(); q.pop();
int u = now.a, S = now.b;
if(S == bits[n] - ) { ans = node(u, S, g[u][S].c); break; }
for(int i = ; i < ; i ++)
{
int v = ch[u][i], s = S | mark[v];
if(g[v][s].c == -)
{
g[v][s] = node(u, S, i);
q.push(node(v, s));
}
}
}
} int main()
{
scanf("%d", &n);
bits[] = ; for(int i = ; i < ; i ++) bits[i] = bits[i - ] << ;
for(int i = ; i <= n; i ++)
{
scanf("%s", s + );
Ins(i);
}
Build(); DP();
for(; g[ans.a][ans.b].c != -; ans = g[ans.a][ans.b])
Ans[++ cnt] = g[ans.a][ans.b].c;
for(int i = cnt; i >= ; i --) printf("%c", Ans[i] + 'A');
return ;
}
2.[JSOI2009] 密码
emmmm……如果没有输出方案一说,和上题完全就是一样的做法但是我们要输出方案呀?想想如果想要在AC自动机上去爆搜也保证复杂度的话,大概借助一个dp数组表示从当前状态往后转移是否可能出现合法解就好了吧?所以状态的设立定为从当前状态走到目的状态的方案数。记忆化搜索大法好!(但是好像没有人这么写?明明这样写真的又无脑又简单呀……)
#include <bits/stdc++.h>
using namespace std;
#define maxn 100000
#define int long long
int n, m, tot, bits[], f[][][];
int cnt, mark[maxn], fail[maxn], ch[maxn][];
char s[maxn]; int read()
{
int x = , k = ;
char c; c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} void Ins(int x)
{
int t = strlen(s + ), p = ;
for(int i = ; i <= t; i ++)
{
int u = s[i] - 'a';
if(!ch[p][u]) ch[p][u] = ++ tot;
p = ch[p][u];
}
mark[p] |= bits[x - ];
} void Build()
{
queue <int> q;
for(int i = ; i < ; i ++)
if(ch[][i]) q.push(ch[][i]);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i = ; i < ; i ++)
{
int v = ch[u][i];
if(v)
{
fail[v] = ch[fail[u]][i];
mark[v] |= mark[fail[v]];
q.push(v);
}
else ch[u][i] = ch[fail[u]][i];
}
}
} void Up(int &x, int y) { x = (x + y); }
int DP(int x, int y, int z)
{
if(z == n && y == bits[m] - ) return f[x][y][z] = ;
else if(z == n) return ;
if(f[x][y][z] != -) return f[x][y][z];
else f[x][y][z] = ;
for(int c = ; c < ; c ++)
{
int v = ch[x][c];
Up(f[x][y][z], DP(v, y | mark[v], z + ));
}
return f[x][y][z];
} void dfs(int x, int y, int z)
{
if(z == n)
{
for(int i = ; i <= cnt; i ++) printf("%c", s[i]);
puts(""); return;
}
for(int c = ; c < ; c ++)
{
int v = ch[x][c];
if(f[v][y | mark[v]][z + ] > )
{
s[++ cnt] = c + 'a';
dfs(v, y | mark[v], z + );
cnt --;
}
}
} signed main()
{
n = read(), m = read();
memset(f, -, sizeof(f));
bits[] = ; for(int i = ; i < ; i ++) bits[i] = bits[i - ] << ;
for(int i = ; i <= m; i ++)
{
scanf("%s", s + );
Ins(i);
}
Build(); DP(, , );
printf("%lld\n", f[][][]);
if(f[][][] > ) return ;
dfs(, , );
return ;
}
3.[BJOI2017] 魔法咒语
这题首先观察一下数据范围,发现一定是两种做法的题(并没有统一的数据范围)。前面的直接暴力建立状态 \(f[i][j]\) 表示第 \(i\) 个字符匹配到了AC自动机上的 \(j\) 状态的方案数。可以枚举用哪一个串转移,只要不会踩到禁忌状态就可以转移。为了降低复杂度,可以预处理一下。至于后面的数据,看到这么大的数据范围显然矩阵。发现长度 <= 2;所以我们可以有:
差不多这样子去构造矩阵。状态和转移方式是不变的,构造矩阵优化dp就好。
#include <bits/stdc++.h>
using namespace std;
#define maxn 6300
#define maxm 205
#define mod 1000000007
int n, m1, m2, ans, tot, f[maxm][maxn];
int len[maxn], ch[maxn][], fail[maxn];
int rec1[maxn][maxn], rec2[maxn][maxn], trans[maxn][maxm];
bool error[maxn];
char s[maxm][maxm]; struct matrix
{
int a[][];
matrix() { memset(a, , sizeof(a)); }
friend matrix operator *(const matrix& a, const matrix& b)
{
matrix c;
memset(c.a, , sizeof(c.a));
int t = tot * ;
for(int i = ; i <= t; i ++)
for(int j = ; j <= t; j ++)
for(int k = ; k <= t; k ++)
c.a[i][j] = (c.a[i][j] + 1ll * a.a[i][k] * b.a[k][j] % mod) % mod;
return c;
}
}M; void Up(int &x, int y) { x = (x + y); if(x >= mod) x -= mod; }
void Ins(int x)
{
int p = ; len[x] = strlen(s[x] + );
for(int i = ; i <= len[x]; i ++)
{
int v = s[x][i] - 'a';
if(!ch[p][v]) ch[p][v] = ++ tot;
p = ch[p][v];
}
error[p] = ;
} int Get(int u, int x)
{
int p = u;
for(int i = ; i <= len[x]; i ++)
{
int v = s[x][i] - 'a';
if(error[p]) return -;
p = ch[p][v];
}
if(error[p]) return -;
return p;
} void Build()
{
queue <int> q;
for(int i = ; i < ; i ++) if(ch[][i]) q.push(ch[][i]);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i = ; i < ; i ++)
{
int v = ch[u][i];
if(v)
{
fail[v] = ch[fail[u]][i];
error[v] |= error[fail[v]];
q.push(v);
}
else ch[u][i] = ch[fail[u]][i];
}
}
for(int j = ; j <= tot; j ++)
for(int i = ; i <= m1; i ++)
{
trans[j][i] = Get(j, i); if(trans[j][i] == -) continue;
if(len[i] == ) rec1[j + ][trans[j][i] + ] ++;
else rec2[j + ][trans[j][i] + ] ++;
}
} void DP1()
{
f[][] = ;
for(int k = ; k <= n; k ++)
for(int i = ; i <= tot; i ++)
{
if(!f[k][i]) continue;
for(int j = ; j <= m1; j ++)
{
int t = trans[i][j];
if(t == -) continue;
else if(k + len[j] <= n) Up(f[k + len[j]][t], f[k][i]);
}
}
for(int i = ; i <= tot; i ++)
if(!error[i]) Up(ans, f[n][i]);
} matrix Qpow(int timer)
{
matrix base;
memset(base.a, , sizeof(base.a));
for(int i = ; i <= * tot; i ++) base.a[i][i] = ;
for(; timer; timer >>= , M = M * M)
if(timer & ) base = base * M;
return base;
} void DP2()
{
tot ++; int t = * tot;
for(int i = tot + ; i <= t; i ++) M.a[i][i - tot] = ;
for(int i = ; i <= tot; i ++)
for(int j = tot + ; j <= t; j ++)
M.a[i][j] = rec2[i][j - tot];
for(int i = tot + ; i <= t; i ++)
for(int j = tot + ; j <= t; j ++)
M.a[i][j] = rec1[i - tot][j - tot]; matrix ret = Qpow(n), S;
memset(S.a, , sizeof(S.a)); S.a[][tot + ] = ;
S = S * ret;
for(int i = tot + ; i <= t; i ++)
if(!error[i - tot - ]) Up(ans, S.a[][i]);
} signed main()
{
scanf("%d%d%d", &m1, &m2, &n);
for(int i = ; i <= m1; i ++) scanf("%s", s[i] + ), len[i] = strlen(s[i] + );
for(int i = ; i <= m2; i ++) scanf("%s", s[m1 + ] + ), Ins(m1 + );
Build();
if(n <= ) DP1(); else DP2();
printf("%d\n", ans);
return ;
}
【题解】AC自动机题解合集的更多相关文章
- [题解+总结]动态规划大合集II
1.前言 大合集总共14道题,出自江哥之手(这就没什么好戏了),做得让人花枝乱颤.虽说大部分是NOIP难度,也有简单的几道题目,但是还是做的很辛苦,有几道题几乎没思路,下面一道道边看边分析一下. 2. ...
- [JSOI2012]玄武密码 题解(AC自动机)
显然是AC自动机对吧 插入单词之后把文章在自动机上跑一遍,到达过的节点打上花火标记 之后检查一下每个单词有几个标记即可 可以把题目中的4个字母映射成abcd方便遍历 一定要记得把文章也映射啊! #in ...
- 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- 【bzoj1030】[JSOI2007]文本生成器 AC自动机+dp
题目描述 JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版.该软件可以随机生成一些文章―――总是生成一篇长度固 ...
- 【bzoj2938】[Poi2000]病毒 AC自动机
题目描述 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找出了所有的病毒代码段,试问,是否 ...
- 【bzoj3940】[Usaco2015 Feb]Censoring AC自动机
题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...
- 【bzoj3530】[Sdoi2014]数数 AC自动机+数位dp
题目描述 我们称一个正整数N是幸运数,当且仅当它的十进制表示中不包含数字串集合S中任意一个元素作为其子串.例如当S=(22,333,0233)时,233是幸运数,2333.20233.3223不是幸运 ...
- 【bzoj1195】[HNOI2006]最短母串 AC自动机+状态压缩+BFS最短路
原文地址:http://www.cnblogs.com/GXZlegend/p/6825226.html 题目描述 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串 ...
- 【bzoj1444】[Jsoi2009]有趣的游戏 AC自动机+矩阵乘法
题目描述 输入 注意 是0<=P 输出 样例输入 样例输出 题解 AC自动机+矩阵乘法 先将所有字符串放到AC自动机中,求出Trie图. 然后构建邻接矩阵:如果x不是某个字符串的末位置,则x连向 ...
随机推荐
- 【MongoDB安装】MongoDB在centos linux平台安装
参考:http://www.runoob.com/mongodb/mongodb-linux-install.html 一..下载安装包 下载方式: 1.登录官网download,然后通过xftp传到 ...
- Struts 2(八):文件上传
第一节 基于Struts 2完成文件上传 Struts 2框架中没有提供文件上传,而是通过Common-FileUpload框架或COS框架来实现的,Struts 2在原有上传框架的基础上进行了进一步 ...
- EF6+MVC5之Oracleo数据库的CodeFirst方式实现
http://www.oracle.com/technetwork/topics/dotnet/whatsnew/index.htm http://www.knowsky.com/887470.htm ...
- maven 手动安装jar包
1.问题 maven有时候在pom文件引入jar包会报错,所以可以通过手动导入jar包的方式导入. 2.解决: 通过maven命令导入jar包, mvn install:install-file -D ...
- [转]操作系统Unix、Windows、Mac OS、Linux的故事
[写得很江湖气,可惜找不到原作者了] 文章转自:http://blog.csdn.net/wenmingchan/article/details/49925379 http://www.jb51.ne ...
- div不设置高度背景颜色或外边框不能显示的解决方法
在使用div+css进行网页布局时,如果外部div有背景颜色或者边框,而不设置其高度,在浏览时出现最外层Div的背景颜色和边框不起作用的问题. 大体结构<div class="oute ...
- day04 list tuple (补)
今日内容: 1. 列表 2. 列表的增删改查 3. 列表的嵌套 4. 元组和元组嵌套 5. range 列表 列表: 能装对象的对象. 有顺序的(按照我们添加的顺序保存) 在代码中使用[]表示列表. ...
- python3【基础】-字符串 常用的方法
字符串一个最重要的特性就是不可修改. name.capitalize() 首字母大写 name.casefold() 大写全部变小写 name.center(50,"-") 输出 ...
- Scrum立会报告+燃尽图(Beta阶段第二周第一次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2409 项目地址:https://coding.net/u/wuyy694 ...
- ORM(object relational Maping)
ORM即对象关系映射,是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述对象和数据库之间映射的元数据,将java程序中的对象自动持久化到关系数据库中.本质上 ...