[Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos
sol
设\(f_{i,j}\)表示填了前\(i\)个字母,在\(AC\)自动机上跑到了节点\(j\)的最大得分。因为匹配需要暴跳\(fail\)所以预先把\(fail\)指针上面的匹配数传下来,这样就只要计算当前节点的贡献就可以了。
code
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N = 1005;
int n,k,tot,ch[3][N],fail[N],cnt[N],dp[N][N],ans;
char s[N];
queue<int>Q;
void Insert()
{
scanf("%s",s);int l=strlen(s);
int x=0;
for (int i=0;i<l;i++)
{
if (!ch[s[i]-'A'][x]) ch[s[i]-'A'][x]=++tot;
x=ch[s[i]-'A'][x];
}
cnt[x]++;
}
void Get_Fail()
{
for (int i=0;i<3;i++) if (ch[i][0]) Q.push(ch[i][0]);
while (!Q.empty())
{
int u=Q.front();Q.pop();
for (int i=0;i<3;i++)
if (ch[i][u]) fail[ch[i][u]]=ch[i][fail[u]],Q.push(ch[i][u]);
else ch[i][u]=ch[i][fail[u]];
cnt[u]+=cnt[fail[u]];
}
}
void DP()
{
memset(dp,-127,sizeof(dp));dp[0][0]=0;
for (int i=1;i<=k;i++)
for (int j=0;j<=tot;j++)
for (int l=0;l<3;l++)
dp[i][ch[l][j]]=max(dp[i][ch[l][j]],dp[i-1][j]+cnt[ch[l][j]]);
}
int main()
{
scanf("%d %d",&n,&k);
for (int i=1;i<=n;i++) Insert();
Get_Fail();
DP();
ans=dp[k][0];
for (int i=1;i<=tot;i++) ans=max(ans,dp[k][i]);
printf("%d\n",ans);
return 0;
}
[Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos的更多相关文章
- [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 < ...
- 【题解】[USACO12JAN]视频游戏的连击Video Game Combos
好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...
- 【洛谷 P3041】 [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机,dp)
题目链接 手写一下AC自动机(我可没说我之前不是手写的) Trie上dp,每个点的贡献加上所有是他后缀的串的贡献,也就是这个点到根的fail链的和. #include <cstdio> # ...
- 【USACO12JAN】视频游戏的连击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 唯一注意的是,要加上所有子串的贡 ...
- [洛谷3041]视频游戏的连击Video Game Combos
题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...
- 【Luogu3041】视频游戏的连击(AC自动机,动态规划)
题面链接 题解 首先构建出AC自动机 然后在AC自动机上面跑DP 转移很显然从Trie树的节点跳到他的儿子节点 但是要注意一个问题, 在计算的时候,每一个节点加入后能够 造成的贡献 要加上他的子串的贡 ...
- P2967 [USACO09DEC]视频游戏的麻烦Video Game Troubles
冲刺阶段的首篇题解! 题目链接:P2967 [USACO09DEC]视频游戏的麻烦Video Game Troubles: 题目概述: 总共N个游戏平台,金额上限V元,给出每个游戏平台的价钱和其上游戏 ...
随机推荐
- CSS3动画中的animation-timing-function效果演示
CSS3动画(animation)属性有如下几个: 属性 值 说明 animation-name name 指定元素要使用的keyframes名称 animation-duration time(ms ...
- PhpStorm使用之 —— Xdebug断点调试
PhpStorm使用之 -- Xdebug断点调试 在<XAMPP的配置与使用>中已经阐述了Xdebug插件的配置,Xdebug配置完成后,只需要在IDE工具中进行相关设置,便可启动Xde ...
- 一个客户端一键安装环境和服务的shell脚本
#!/bin/bash basepath=$(cd `dirname $0`; pwd)SHELL_DIR="${basepath}/shell"PACKAGE_DIR=" ...
- Ubuntu下利用Apache转发模块实现反向代理
Apache的反向代理主要利用转发模块,proxy和proxy_http 先配置 Apache 支持proxy 和 proxy_http 在Ubuntu系统下,Apache的配置文件在目录/etc/a ...
- 简单http文件服务器
日常工作中,主要是通过ssh终端(putty等)远程开发,经常涉及到传输文件,因为本地系统为Win10,所以没办法利用强大的linux脚本来进行文件传输.之前用过python的SimpleHttp模块 ...
- PHP opcache扩展安装
下面是我在PHP 5.4下的安装方法: https://pecl.php.net/get/zendopcache-7.0.5.tgz tar xzf zendopcache-7.0.5.tgz cd ...
- Linux Centos下编译安装Redis
需要安装 tcl 8.5 wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz //直接下载 sudo tar xzvf tcl8 ...
- 和scikit-learn打个招呼
1.先装对应的库.不能偷懒,都得装,不然飞不起来. pip install scikit-learn pip install numpy pip install scipy 2.测试如下代码. imp ...
- Phpstrom操作git
1.PHPstrom操作git[上传] 2.提交代码到仓库 2. 3.使用git bash上传代码仓库的代码到远程服务器 代开git bash进入到项目所在的目录,输入命令$ git push .上 ...
- 3_使用指针对象(Using Object Pointer)
类的成员函数有两种调用方式,一种是由对象调用,另一种是由对象指针调用.其中,对象调用的方式为objectName.memberFunctionName(parameters),而对象指针调用的方式为o ...