题目链接

手写一下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)的更多相关文章

  1. [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机+DP)

    Description 贝西正在打格斗游戏.游戏里只有三个按键,分别是“A”.“B”和“C”.游戏中有 N 种连击 模式,第 i 种连击模式以字符串 Si 表示,只要贝西的按键中出现了这个字符串,就算 ...

  2. P3041 [USACO12JAN]视频游戏的连击Video Game Combos

    思路 简单的AC自动机上dp,暴力跳fail向子节点直接转移即可 代码 #include <cstdio> #include <algorithm> #include < ...

  3. [Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos

    题面 sol 设\(f_{i,j}\)表示填了前\(i\)个字母,在\(AC\)自动机上跑到了节点\(j\)的最大得分.因为匹配需要暴跳\(fail\)所以预先把\(fail\)指针上面的匹配数传下来 ...

  4. 【题解】[USACO12JAN]视频游戏的连击Video Game Combos

    好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...

  5. [洛谷3041]视频游戏的连击Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

  6. 洛谷P3041 视频游戏的连击Video Game Combos [USACO12JAN] AC自动机+dp

    正解:AC自动机+dp 解题报告: 传送门! 算是个比较套路的AC自动机+dp趴,,, 显然就普普通通地设状态,普普通通地转移,大概就f[i][j]:长度为i匹配到j 唯一注意的是,要加上所有子串的贡 ...

  7. 【USACO12JAN】视频游戏的连击Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

  8. [USACO12Jan][luogu3041] Video Game Combos [AC自动机+dp]

    题面 传送门 思路 首先,有一个非常显然的思路就是dp: 设$dp[i][j]$表示前i个字符,最后一个为j 然后发现这个东西有后效性 改!设$dp[i][j]$代表前i个字符,最后15个的状态为j( ...

  9. 洛谷 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 ...

随机推荐

  1. 配置Always On AG

    1.准备测试环境的服务器 在 Always On AG 中如果需要自动 Failover 至少需要集群中有 3 台服务器,但是我只是测试功能,因此只使用了两台服务器.并且本文不涉及任何 Pacemak ...

  2. Flume(一) —— 启动与基本使用

    基础架构 Flume is a distributed, reliable(可靠地), and available service for efficiently(高效地) collecting, a ...

  3. Server 2003 操作系统位数

    安装好电脑系统,如何查看windows 2003/xp/win7是64位还是32位? 方法/步骤 第一种方法:桌面上鼠标右键单击“计算机”(我的电脑) 在弹出的快捷菜单中选择“属性”,如果看到64的字 ...

  4. 范仁义html+css课程---3、图片和超链接

    范仁义html+css课程---3.图片和超链接 一.总结 一句话总结: img标签是图片标签,定义 HTML 页面中的图像 a标签是超链接标签,用于从一个页面链接到另一个页面. 1.img标签要点? ...

  5. ora-8176原因及解决方法

    在oracle undo_retention范围内,且_undo_autotune=false的情况下,一个语句执行的时候仍然发生ora-8176,语句如下: INSERT INTO XXX SELE ...

  6. SymPy解方程的实现

    https://www.cnblogs.com/zgyc/p/6277562.html SymPy完全是用Python写的,并不需要外部的库 原理: 单纯用语言内置的运算与变量解决的是,由值求结果.如 ...

  7. JAVA垃圾回收-可达性分析算法

    在java中是通过引用来和对象进行关联的,也就是说如果要操作对象,必须通过引用来进行.那么很显然一个简单的办法就是通过引用计数来判断一个对象是否可以被回收.不失一般性,如果一个对象没有任何引用与之关联 ...

  8. 泡泡一分钟:Efficient Trajectory Planning for High Speed Flight in Unknown Environments

    张宁  Efficient Trajectory Planning for High Speed Flight in Unknown Environments 高效飞行在未知环境中的有效轨迹规划链接: ...

  9. Ubuntu新建用户并指定目录

    例如我要新建一个nginx用户,并指定目录,允许使用bash登录 sudo useradd -d "/home/nginx" -m -s "/bin/bash" ...

  10. [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...