[洛谷3041]视频游戏的连击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.
输入输出样例
说明
The optimal sequence of buttons in this case is ABACBCB, which gives 4 points--1 from ABA, 1 from ABACB, and 2 from CB.
很显然的AC自动机+dp,不过dp那块还是调了半天。。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,cnt,ans,k;
int fail[];
int end[];
int ch[][];
int dp[][];
bool vis[][];
char s[];
void build(string s)
{
int len=s.length();
int now=;
for(int i=;i<len;i++)
{
if(!ch[now][s[i]-'A']) ch[now][s[i]-'A']=++cnt;
now=ch[now][s[i]-'A'];
}
end[now]+=;
}
void build_fail()
{
queue<int>q;
for(int i=;i<;i++)
if(ch[][i])
q.push(ch[][i]);
while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=;i<;i++)
{
if(ch[u][i])
{
fail[ch[u][i]]=ch[fail[u]][i];
q.push(ch[u][i]);
}
else ch[u][i]=ch[fail[u]][i];
}
}
}
int get(int now,int val)
{
while(now) val+=end[now],now=fail[now];
return val;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%s",s);
build(s);
}
build_fail();
vis[][]=;
for(int i=;i<k;i++)
for(int j=;j<=cnt;j++)
{
if(!vis[i][j]) continue;
for(int l=;l<;l++)
{
int now=ch[j][l];
dp[i+][now]=max(dp[i+][now],get(now,dp[i][j]));
vis[i+][now]=true;
}
}
for(int i=;i<=cnt;i++) ans=max(ans,dp[k][i]);
printf("%d",ans);
return ;
}
[洛谷3041]视频游戏的连击Video Game Combos的更多相关文章
- 洛谷P3041 视频游戏的连击Video Game Combos [USACO12JAN] AC自动机+dp
正解:AC自动机+dp 解题报告: 传送门! 算是个比较套路的AC自动机+dp趴,,, 显然就普普通通地设状态,普普通通地转移,大概就f[i][j]:长度为i匹配到j 唯一注意的是,要加上所有子串的贡 ...
- 【洛谷 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 ...
- [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] ...
- 洛谷 P2197 nim游戏
洛谷 P2197 nim游戏 题目描述 甲,乙两个人玩Nim取石子游戏. nim游戏的规则是这样的:地上有n堆石子(每堆石子数量小于10000),每人每次可从任意一堆石子里取出任意多枚石子扔掉,可以取 ...
- 洛谷 P1965 转圈游戏
洛谷 P1965 转圈游戏 传送门 思路 每一轮第 0 号位置上的小伙伴顺时针走到第 m 号位置,第 1 号位置小伙伴走到第 m+1 号位置,--,依此类推,第n − m号位置上的小伙伴走到第 0 号 ...
随机推荐
- 【BZOJ4584】[Apio2016]赛艇 DP
[BZOJ4584][Apio2016]赛艇 Description 在首尔城中,汉江横贯东西.在汉江的北岸,从西向东星星点点地分布着个划艇学校,编号依次为到.每个学校都拥有若干艘划艇.同一所学校的所 ...
- idle命令行按ALT+P重复调出上个语句
idle命令行按ALT+P重复调出上个语句
- Redisson教程
Redisson入门 Author:Ricky Date:2017-04-24 Redisson概述 Redisson是架设在Redis基础上的一个Java驻内存数据网格(In-Memory Dat ...
- Go语言性能测试
对于一些服务来说,性能是极其重要的一环,事关系统的吞吐.访问的延迟,进而影响用户的体验. 写性能测试在Go语言中是很便捷的,go自带的标准工具链就有完善的支持,下面我们来从Go的内部和系统调用方面来详 ...
- Linux文档编辑 vim
- CoffeeScript编译手记
最近折腾bootstrap,看到一个Messager插件挺好的,可这丫的发出来的都是CoffeeScript的DEMO,顿时让我感觉自己已经落后了一百年. 于是各种搜索,各种脑补,原来Coffee最后 ...
- document.cookie = 'wcookie_date=' + wv + ';max-age=60'
js cookie生命周期
- 动态长度中英字符串显示至固定高度td
w 为td中英字符串区域设置为display:block; height=td_height,并指明td width. <!doctype html> <html lang=&quo ...
- js身份证号有效性验证
1.简述 最近做的系统有用到实名验证的,起初对于用户身份证号只是简单地使用正则表达式进行验证, 很多无效的身份证号就成了漏网之鱼. 导致后台存表里很多无效的身份证号,随便输入用户名和身份证号就可以实名 ...
- 为什么JSP的内置对象不需要声明
本文将通过对一个JSP运行过程的剖析,深入JSP运行的内幕,并从全新的视角阐述一些JSP中的技术要点. HelloWorld.jsp 我们以Tomcat 4.1.17服务器为例,来看看最简单的Hell ...