落谷P3041 [USACO12JAN]Video Game G
多模式匹配问题,先建 AC 自动机。
套路性的搞个 DP:
- \(f[i][j]\) 表示前 \(i\) 个字符,当前在 \(AC\) 自动机上的节点是 \(j\) 能匹配到的最多分。
- 初始化 \(f[0][0] = 0\),其余为负无穷
- 答案 \(\max\{f[K][i]\}\)
考虑一条边 \(u \Rightarrow v\),加入后缀的贡献,即 \(f[i][v] = \max(f[i - 1][u] + val(v))\)
其中 \(val(v)\) 表示 \(v\) 这个点上出现的模式串个数,即其 fail 树的他到根的链上的是模式串末尾的个数。
复杂度 \(O(45NK)\)。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int S = 20, L = 305, M = 1005;
int n, K, tr[L][3], fail[L];
int cnt[L], q[L], idx, f[M][L];
char s[S];
void insert() {
int p = 0;
for (int i = 1; s[i]; i++) {
int ch = s[i] - 'A';
if (!tr[p][ch]) tr[p][ch] = ++idx;
p = tr[p][ch];
}
cnt[p]++;
}
void build() {
int hh = 0, tt = -1;
for (int i = 0; i < 3; i++)
if (tr[0][i]) q[++tt] = tr[0][i];
while (hh <= tt) {
int u = q[hh++];
for (int i = 0; i < 3; i++) {
int v = tr[u][i];
if (v) {
fail[v] = tr[fail[u]][i];
cnt[v] += cnt[fail[v]];
q[++tt] = v;
} else tr[u][i] = tr[fail[u]][i];
}
}
}
int main() {
memset(f, 0xcf, sizeof f);
scanf("%d%d", &n, &K);
for (int i = 1; i <= n; i++) {
scanf("%s", s + 1);
insert();
}
build();
f[0][0] = 0;
for (int i = 0; i < K; i++) {
for (int u = 0; u <= idx; u++) {
if (f[i][u] < 0) continue;
for (int j = 0; j < 3; j++) {
int v = tr[u][j];
f[i + 1][v] = max(f[i + 1][v], f[i][u] + cnt[v]);
}
}
}
int ans = 0;
for (int i = 0; i <= idx; i++) ans = max(ans, f[K][i]);
printf("%d\n", ans);
}
落谷P3041 [USACO12JAN]Video Game G的更多相关文章
- 洛谷 P3041 [USACO12JAN] Video Game Combos
题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...
- 【题解】[USACO12JAN]Video Game G
第一道\(AC\)自动机\(+DP.\) 首先,一个自动机上\(DP\)的套路是设\(dp[i][j]\)表示长度为\(i\)匹配到\(j\)节点的最优得分. 那么,由于我们已经建好了\(Trie\) ...
- 洛谷P3041 视频游戏的连击Video Game Combos [USACO12JAN] AC自动机+dp
正解:AC自动机+dp 解题报告: 传送门! 算是个比较套路的AC自动机+dp趴,,, 显然就普普通通地设状态,普普通通地转移,大概就f[i][j]:长度为i匹配到j 唯一注意的是,要加上所有子串的贡 ...
- 【洛谷 P3041】 [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机,dp)
题目链接 手写一下AC自动机(我可没说我之前不是手写的) Trie上dp,每个点的贡献加上所有是他后缀的串的贡献,也就是这个点到根的fail链的和. #include <cstdio> # ...
- P3041 [USACO12JAN]视频游戏的连击Video Game Combos
思路 简单的AC自动机上dp,暴力跳fail向子节点直接转移即可 代码 #include <cstdio> #include <algorithm> #include < ...
- 不失一般性和快捷性地判定决策单调(洛谷P1912 [NOI2009]诗人小G)(动态规划,决策单调性,单调队列)
洛谷题目传送门 闲话 看完洛谷larryzhong巨佬的题解,蒟蒻一脸懵逼 如果哪年NOI(放心我这样的蒟蒻是去不了的)又来个决策单调性优化DP,那蒟蒻是不是会看都看不出来直接爆\(0\)?! 还是要 ...
- 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance
P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...
- 洛谷 P3040 [USACO12JAN]贝尔分享Bale Share
P3040 [USACO12JAN]贝尔分享Bale Share 题目描述 Farmer John has just received a new shipment of N (1 <= N & ...
- 「区间DP」「洛谷PP3146 」[USACO16OPEN]248 G
[USACO16OPEN]248 G 题目: 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...
随机推荐
- 信号-linux
https://www.linuxjournal.com/article/3985 每个信号在 signal.h 头文件中通过宏进行定义,实际是在 signal.h 中定义,对于编号以及信号名的映射关 ...
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)'))) - skipping
C:\Users>pip listPackage Version------------ -------behave 1.2.6configparser 3.7.4ddt 1.2.1parse ...
- Spring Cloud Gateway原理
1.使用 compile 'org.springframework.cloud:spring-cloud-starter-gateway' 2.包结构 actuate中定义了一个叫GatewayCon ...
- 面试腾讯,字节跳动首先要掌握的Java多线程,一次帮你全掌握!
一.程序,进程,线程联系和区别 其实程序是一段静态的代码,它是应用程序执行的脚本.进程就是程序动态的执行过程,它具有动态性,并发性,独立性.线程是进程调度和执行的单位. 进程:每个进程都有独立的代码和 ...
- ABBYY FineReader中的其他格式
ABBYY FineReade是一款功能强大的PDF编辑转换器,在内置任务窗口,您可以将 PDF 或图片转换成常见的格式(*.pptx. *.odt. *.html.*.epub.*.fb2.*.rt ...
- 2个快速制作完成一幅思维导图的iMindMap思维导图用法
随着思维导图的流行,与其相关的思维导图制作软件如雨后春笋,纷纷进入我们的视野中,更让人难以选择.那想要入门的萌新该如何开始这个新的旅途呢? 各式各样的思维导图制作软件当中,有一个软件得到了大家一致的好 ...
- 设置searchDisplayController的searchResultsTableView的UITableViewStyle为grouped
[self.searchDisplayController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped] forKey:@&quo ...
- 2017年第八届蓝桥杯【C++省赛B组】B、C、D、H 题解
可能因为我使用暴力思维比较少,这场感觉难度不低. B. 等差素数列 #暴力 #枚举 题意 类似:\(7,37,67,97,127,157\) 这样完全由素数组成的等差数列,叫等差素数数列. 上边的数列 ...
- Snap Build Your Own Block修炼之道-添加自定义类别
Snap Build Your Own Block自我修炼方法:1.所有的面向对象,其实是对面向过程的抽象过程而已: 2.面对别人的开源项目时,需要找准源头(即项目运行的起点,当然有的是没有的哈,没有 ...
- Apache HTTPD 换行解析漏洞--CVE-2017-15715
CVE-2017-15715 一.漏洞描述 Apache HTTPD是一款HTTP服务器,它可以通过mod_php来运行PHP网页.其2.4.0~2.4.29版本中存在一个解析漏洞,在解析PHP时,1 ...