【洛谷 P3041】 [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机,dp)
题目链接
手写一下AC自动机(我可没说我之前不是手写的)
Trie上dp,每个点的贡献加上所有是他后缀的串的贡献,也就是这个点到根的fail链的和。
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXK = 1010;
const int MAXN = 1010;
struct ACA{
int next[5], num, fail;
}AC[MAXN];
int f[MAXK][MAXN];
int n, k, cnt, ans;
char a[30];
queue <int> q;
void insert(){
int len = strlen(a + 1), p = 0, w;
for(int i = 1; i <= len; ++i){
w = a[i] - 'A';
if(!AC[p].next[w])
AC[p].next[w] = ++cnt;
p = AC[p].next[w];
}
++AC[p].num;
}
void build(){
int now;
for(int i = 0; i < 3; ++i)
if(AC[0].next[i])
q.push(AC[0].next[i]);
while(q.size()){
now = q.front(); q.pop();
for(int i = 0; i < 3; ++i)
if(AC[now].next[i]){
AC[AC[now].next[i]].fail = AC[AC[now].fail].next[i];
q.push(AC[now].next[i]);
}
else AC[now].next[i] = AC[AC[now].fail].next[i];
AC[now].num += AC[AC[now].fail].num;
}
}
void dp(){
for(int i = 0; i <= k; ++i)
for(int j = 1; j <= cnt; ++j)
f[i][j] = -2147483647;
for(int i = 1; i <= k; ++i)
for(int j = 0; j <= cnt; ++j){
for(int k = 0; k < 3; ++k)
f[i][AC[j].next[k]] = max(f[i][AC[j].next[k]], f[i - 1][j] + AC[AC[j].next[k]].num);
}
for(int i = 1; i <= cnt; ++i)
ans = max(ans, f[k][i]);
}
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; ++i){
scanf("%s", a + 1);
insert();
}
build();
dp();
printf("%d\n", ans);
return 0;
}
【洛谷 P3041】 [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机,dp)的更多相关文章
- [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机+DP)
Description 贝西正在打格斗游戏.游戏里只有三个按键,分别是“A”.“B”和“C”.游戏中有 N 种连击 模式,第 i 种连击模式以字符串 Si 表示,只要贝西的按键中出现了这个字符串,就算 ...
- P3041 [USACO12JAN]视频游戏的连击Video Game Combos
思路 简单的AC自动机上dp,暴力跳fail向子节点直接转移即可 代码 #include <cstdio> #include <algorithm> #include < ...
- [Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos
题面 sol 设\(f_{i,j}\)表示填了前\(i\)个字母,在\(AC\)自动机上跑到了节点\(j\)的最大得分.因为匹配需要暴跳\(fail\)所以预先把\(fail\)指针上面的匹配数传下来 ...
- 【题解】[USACO12JAN]视频游戏的连击Video Game Combos
好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...
- [洛谷3041]视频游戏的连击Video Game Combos
题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...
- 洛谷P3041 视频游戏的连击Video Game Combos [USACO12JAN] AC自动机+dp
正解:AC自动机+dp 解题报告: 传送门! 算是个比较套路的AC自动机+dp趴,,, 显然就普普通通地设状态,普普通通地转移,大概就f[i][j]:长度为i匹配到j 唯一注意的是,要加上所有子串的贡 ...
- 【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][luogu3041] Video Game Combos [AC自动机+dp]
题面 传送门 思路 首先,有一个非常显然的思路就是dp: 设$dp[i][j]$表示前i个字符,最后一个为j 然后发现这个东西有后效性 改!设$dp[i][j]$代表前i个字符,最后15个的状态为j( ...
- 洛谷 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 ...
随机推荐
- 思科ACL阻止勒索病毒
如何在思科的3700系列交换机上配置ACL阻止勒索病毒的传播? 勒索病毒主要是微软的锅,通过TCP/UDP的135.137.138.139.445端口攻陷用户的计算机加密用户的文件达到勒索比特币的目的 ...
- hive中同源多重insert写法
多重insert: with tmp_a as ( select name from tmp_test3 ) from tmp_a insert overwrite table tmp_test1 s ...
- springboot 整合Elasticsearch
Elasticsearch Elasticsearch 是一个分布式.可扩展.实时的搜索与数据分析引擎. 它能从项目一开始就赋予你的数据以搜索.分析和探索的能力,可用于实现全文搜索和实时数据统计. 在 ...
- xshell修改配色方案为白色
- LODOP很简短的问答(纯文字)
打印机选择打印机,参考样例5,7http://www.c-lodop.com/LodopDemo.html打印机的一些优先级,参考http://www.c-lodop.com/blogs/Blog00 ...
- express下使用ES6
//环境切换配置 package.json scripts:{ "service": "NODE_ENV=production PORT=3000 npm start&q ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- 在使用FPGA来控制DDR3/DDR2 IP 的时候两个错误的解决办法
对于熟悉Intel FPGA的老(gong)司(cheng)机(shi)来说,外部存储器的控制早已是轻车熟路,但是对于新手,DDR3/DDR2 的IP使用也许并没有那么简单,不过没关系,骏龙的培训网站 ...
- 学习记录-java基础部分(一)
学习记录-java基础部分(一) 参考:GitHub上的知名项目:javaGuide : https://github.com/Snailclimb/JavaGuide/blob/master/doc ...
- python tkinter中的事件绑定
一个Tkinter主要跑在mainloop进程里.Events可能来自多个地方,比如按键,鼠标,或是系统事件. Tkinter提供了丰富的方法来处理这些事件.对于每一个控件Widget,你都可以为其绑 ...