【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是几个字符串的 ...
随机推荐
- 201621123060 《Java程序设计》第五周学习总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承.多态.抽象类与接口 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. 2. 书面作业 作 ...
- APP案例分析
产品 蓝叠安卓模拟器 选择理由 看了一眼桌面,就这个比较有意思.现在很多人喜欢玩手游,经常喜欢开个小号搞事情.这时候身边又没有多余的手机,怎么办?安卓模拟器下一个.手机屏幕太小玩起来没意思怎么 ...
- Beta集合
Beta冲刺day1 Beta冲刺day2 Beta冲刺day3 Beta冲刺day4 Beta冲刺day5 Beta冲刺day6 Beta冲刺day7 测试总结 总结合集 Beta预备
- 怎么去理解JAVA中类与对象的关系
首先要明确,在现实生活中,每一个物体都有自己的基本特征,专业一点也可以说成是属性有些甚至还有一定的行为.例如 汽车的特征:有车门.有轮胎.颜色各一等等,行为:有行驶,开车门,开车灯,等等.有这些属性和 ...
- Hibernate之Hibernate的体系结构
体系结构简图: 这是一张体系结构的简图,其中的hibernate.properties文件的作用相当于配置文件hibernate.cfg.xml XML Mapping对应的就是映射文件 XXXX.h ...
- NYOJ 炫舞家st
#include <iostream>#include <cstring>#include <algorithm>using namespace std; cons ...
- JAVA_SE基础——4.path的临时配置&Classpath的配置
这次,我来写下关于path的临时配置的心的 我来说个有可能的实例:如果你去到别人的电脑 又想写代码 又不想改乱别人的path配置的话 再说别人愿意你在别人的电脑上瞎配吗? 那该怎么办呢? 那没问题 ...
- Python内置函数(47)——vars
英文文档: vars([object]) Return the __dict__ attribute for a module, class, instance, or any other objec ...
- Python内置函数(23)——dict
英文文档: class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg) Return a new di ...
- 新概念英语(1-143)A walk through the woods
Lesson 143 A walk through the woods 林中散步 Listen to the tape then answer this question. What was so f ...