AC自动机建立fail树后树上DP

# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# include <algorithm>
# include <queue>
# define IL inline
# define RG register
# define ll long long
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std; IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
return x * z;
} const int MAXN(1010);
struct AC{
int fail, ch[3], num;
} tree[MAXN];
int n, cnt, k, f[MAXN][MAXN], ans;
char t[MAXN];
queue <int> Q; IL void Insert(){
RG int len = strlen(t), x = 0;
for(RG int i = 0; i < len; i++){
if(!tree[x].ch[t[i] - 'A']) tree[x].ch[t[i] - 'A'] = ++cnt;
x = tree[x].ch[t[i] - 'A'];
}
tree[x].num++;
} IL void Bfs(){
while(!Q.empty()) Q.pop();
for(RG int i = 0; i < 3; i++)
if(tree[0].ch[i]) Q.push(tree[0].ch[i]);
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
for(RG int i = 0; i < 3; i++)
if(tree[u].ch[i]){
tree[tree[u].ch[i]].fail = tree[tree[u].fail].ch[i];
Q.push(tree[u].ch[i]);
}
else tree[u].ch[i] = tree[tree[u].fail].ch[i];
tree[u].num += tree[tree[u].fail].num;
}
} int main(){
n = Read(); k = Read();
for(RG int i = 1; i <= n; i++){
scanf(" %s", t);
Insert();
}
Bfs();
Fill(f, -127); f[0][0] = 0;
for(RG int K = 1; K <= k; K++){
for(RG int i = 0; i <= cnt; i++)
for(RG int j = 0; j < 3; j++)
f[K][tree[i].ch[j]]=max(f[K][tree[i].ch[j]],f[K - 1][i] + tree[tree[i].ch[j]].num);
}
for(RG int i = 0; i <= cnt; i++)
ans = max(ans, f[k][i]);
printf("%d\n", ans);
return 0;
}

[USACO12JAN]Video Game Combos的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

  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(AC自动机,dp)

    题目链接 手写一下AC自动机(我可没说我之前不是手写的) Trie上dp,每个点的贡献加上所有是他后缀的串的贡献,也就是这个点到根的fail链的和. #include <cstdio> # ...

随机推荐

  1. git服务器配置http请求

    使用apache 配置http协议的git库 在CentOS上基于Apache http服务搭建git远程仓库(一) 基于http方式的git服务器搭建 搭建http协议的git服务器 Linux g ...

  2. LVS结合keepalived配置测试

     LVS/DR + keepalived配置 注意:前面虽然我们已经配置过一些操作,但是下面我们使用keepaliave操作和之前的操作是有些冲突的,所以若是之前配置过DR,请首先做如下操作:   三 ...

  3. [记]WIndow/Linux 获取本机(全部)IPv4、IPv6、MAC地址方法 (C/C++)

    Linux 获取本机IP.MAC地址用法大全 //#include <sys/types.h> #include <ifaddrs.h> #include <sys/io ...

  4. js使用defineProperty的一些坑

    var p2={ }; Object.defineProperty(p2,"gs",{ get:function () { return this.gs; }, set:funct ...

  5. Maven项目热部署,修改代码后不用重启tomcat服务器

    只需要在pom.xml文件中添加 <build> <finalName>MySSM</finalName> <!-- 指定部署的服务器类型 --> &l ...

  6. Spring boot 整合redis单机版

       一.安装redis 这个不多说,网上有各种系统安装redis的操作, redis安装 二.创建sprigboot项目 这个也不多说,不会的前面有相关教程. 三.添加maven坐标 四.编写spr ...

  7. 1.2 Python开发环境

    1.2.1 百家争鸣的繁荣景象 工欲善其事,必先利其器.学习编程也是同样的道理,熟悉开发环境应该是学习一门编程语言的第一步. IDLE是Python的官方标准开发环境,从官网www.python.or ...

  8. String、StringBuffer、与StringBuilder的区别

    转载自博客园,原文链接:String.StringBuffer.与StringBuilder的区别 相信大家都知道StringBuffer.StringBuilder,但是这两个的用法都差不多,到底有 ...

  9. GIT 查看 删除 添加远程库

    查看远程库 $ git remote -vorigin https://github.com/autoliuweijie/MachineLearning.git (fetch)origin https ...

  10. jmeter 脚本规范

    总结了一下公司正在用 jmeter 脚本规范. 使用 jmeter 进行接口级测试, 随着接口增多以及业务逻辑越来越复杂, 导致 jmeter 脚本的维护会更加困难.针对实际使用中发现的问题进行一些规 ...