【USACO12JAN】视频游戏的连击Video Game Combos
题目描述
Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only valid buttons. Bessie may press the buttons in any order she likes; however, there are only N distinct combos possible (1 <= N <= 20). Combo i is represented as a string S_i which has a length between 1 and 15 and contains only the letters 'A', 'B', and 'C'.
Whenever Bessie presses a combination of letters that matches with a combo, she gets one point for the combo. Combos may overlap with each other or even finish at the same time! For example if N = 3 and the three possible combos are "ABA", "CB", and "ABACB", and Bessie presses "ABACB", she will end with 3 points. Bessie may score points for a single combo more than once.
Bessie of course wants to earn points as quickly as possible. If she presses exactly K buttons (1 <= K <= 1,000), what is the maximum number of points she can earn?
贝西在玩一款游戏,该游戏只有三个技能键 “A”“B”“C”可用,但这些键可用形成N种(1 <= N<= 20)特定的组合技。第i个组合技用一个长度为1到15的字符串S_i表示。
当贝西输入的一个字符序列和一个组合技匹配的时候,他将获得1分。特殊的,他输入的一个字符序列有可能同时和若干个组合技匹配,比如N=3时,3种组合技分别为"ABA", "CB", 和"ABACB",若贝西输入"ABACB",他将获得3分。
若贝西输入恰好K (1 <= K <= 1,000)个字符,他最多能获得多少分?
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and K.
- Lines 2..N+1: Line i+1 contains only the string S_i, representing combo i.
输出格式:
- Line 1: A single integer, the maximum number of points Bessie can obtain.
输入输出样例
3 7
ABA
CB
ABACB
4
说明
The optimal sequence of buttons in this case is ABACBCB, which gives 4 points--1 from ABA, 1 from ABACB, and 2 from CB.
题解:
记cnt[i]为节点i沿着fail一直走下去可以获得的积分,那么
f[i][j]为走了i步到节点j的最大积分 注意初始化...
f[i][a[j].next[k]]=max(f[i][a[j].next[k]],f[i-1][j]+a[a[j].next[k]].cnt);
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int INF=-2e8;
struct node
{
int next[];
int cnt;
}a[];
int f[][];
int root=,num=,fail[];
char s[];
void Clear()
{
a[num].cnt=;
for(int i=;i<;i++)a[num].next[i]=;
}
void add()
{
scanf("%s",s);
int p=root;
for(int i=,ls=strlen(s);i<ls;i++)
{
if(a[p].next[s[i]-'A'])p=a[p].next[s[i]-'A'];
else
{
a[p].next[s[i]-'A']=++num;
Clear();
p=num;
}
}
a[p].cnt++;
}
void getfail()
{
queue<int>q;
q.push(root);
int u,p,v;
while(!q.empty())
{
u=q.front();q.pop();
for(int i=;i<;i++)
{
if(!a[u].next[i])
{
if(a[fail[u]].next[i])a[u].next[i]=a[fail[u]].next[i];
continue;
}
p=fail[u];
while(p)
{
if(a[p].next[i])break;
p=fail[p];
}
if(a[p].next[i] && a[p].next[i]!=a[u].next[i])fail[a[u].next[i]]=a[p].next[i];
v=a[u].next[i];
a[v].cnt+=a[fail[v]].cnt;
q.push(a[u].next[i]);
}
}
}
int main()
{
//freopen("pp.in","r",stdin);
int n,k,ans=;
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
add();
getfail();
for(int i=;i<=k;i++)
for(int j=;j<=num;j++)
f[i][j]=INF;
f[][]=;
for(int i=;i<=k;i++)
{
for(int j=;j<=num;j++)
{
for(int k=;k<;k++)
{
f[i][a[j].next[k]]=max(f[i][a[j].next[k]],f[i-][j]+a[a[j].next[k]].cnt);
}
}
}
for(int i=;i<=num;i++)if(f[k][i]>ans)ans=f[k][i];
printf("%d\n",ans);
return ;
}
【USACO12JAN】视频游戏的连击Video Game Combos的更多相关文章
- [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机+DP)
Description 贝西正在打格斗游戏.游戏里只有三个按键,分别是“A”.“B”和“C”.游戏中有 N 种连击 模式,第 i 种连击模式以字符串 Si 表示,只要贝西的按键中出现了这个字符串,就算 ...
- [Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos
题面 sol 设\(f_{i,j}\)表示填了前\(i\)个字母,在\(AC\)自动机上跑到了节点\(j\)的最大得分.因为匹配需要暴跳\(fail\)所以预先把\(fail\)指针上面的匹配数传下来 ...
- 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> # ...
- 洛谷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 ...
- P2967 [USACO09DEC]视频游戏的麻烦Video Game Troubles
冲刺阶段的首篇题解! 题目链接:P2967 [USACO09DEC]视频游戏的麻烦Video Game Troubles: 题目概述: 总共N个游戏平台,金额上限V元,给出每个游戏平台的价钱和其上游戏 ...
- 视频游戏的连击 [USACO12JAN](AC自动机+动态规划)
传送门 默认大家都学过trie与AC自动机. 先求出fail,对于每个节点维护一个sum,sum[u]待表从根到u所形成的字符串能拿到几分.显然sum[u]=sum[fail] + (u是几个字符串的 ...
随机推荐
- appcompat v21: 让 Android 5.0 前的设备支持 Material Design
1. 十大Material Design开源项目 2. appcompat v21: 让 Android 5.0 前的设备支持 Material Design 主题 AppCompat已经支持最新的调 ...
- shell中冒号 : 用途说明
我们知道,在Linux系统中,冒号(:)常用来做路径的分隔符(PATH),数据字段的分隔符(/etc/passwd)等.其实,冒号(:)在Bash中也是一个内建命令,它啥也不做,是个空命令.只起到占一 ...
- Java面试题合集(二)
接下来几篇文章准备系统整理一下有关Java的面试题,分为基础篇,javaweb篇,框架篇,数据库篇,多线程篇,并发篇,算法篇等等,陆续更新中.其他方面如前端后端等等的面试题也在整理中,都会有的. 注: ...
- kafka安装使用和遇到的坑
下载安装 参考:https://segmentfault.com/a/1190000012730949 https://kafka.apache.org/quickstart 关闭服务 关闭zoo ...
- Swagger: 一个restful接口文档在线生成+功能测试软件
一.什么是 Swagger? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 ...
- express学习(二)—— Post()类型和中间件
1.数据:GET.POST 2.中间件:使用.写.链式操作 GET-无需中间件 req.query POST-需要"body-parser" server.use(bodyPars ...
- 谈谈自己的理解:python中闭包,闭包的实质
闭包这个概念好难理解,身边朋友们好多都稀里糊涂的,稀里糊涂的林老冷希望写下这篇文章能够对稀里糊涂的伙伴们有一些帮助~ 请大家跟我理解一下,如果在一个函数的内部定义了另一个函数,外部的我们叫他外函数,内 ...
- 改变textField的placeholder的颜色和位置
重写UItextField的这个方法,用其他的textField继承自这个父类 - (void) drawPlaceholderInRect:(CGRect)rect { [[UIColor blue ...
- CSS属性操作/下
CSS属性操作/下 1.伪类 anchor伪类 跟<a>/</a>有关:专用于控制链接的显示效果 a:link(没有接触过的链接),用于定义了链接的常规状态. a:hover( ...
- NetSNMP开源代码学习——小试牛刀
原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 题外话:技术越是古董级的东西,越是值得学习. 一. 配置 参考: http://www.cnbl ...