洛谷 P3041 [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.
最简单的AC自动机+DP了。。。。再不会的话AC自动机白学了。
- #include<bits/stdc++.h>
- #define ll long long
- #define maxn 1005
- using namespace std;
- int ch[maxn][],n,m,k,ans=;
- int root=,tot=,val[maxn];
- int f[maxn],g[maxn][maxn];
- char s[maxn];
- inline int id(char c){
- return c-'A';
- }
- inline void ins(){
- int len=strlen(s),now=root;
- for(int i=;i<len;i++){
- int c=id(s[i]);
- if(!ch[now][c]) ch[now][c]=++tot;
- now=ch[now][c];
- }
- val[now]=;
- }
- inline void get_fail(){
- queue<int> q;
- for(int i=;i<;i++) if(ch[][i]){
- q.push(ch[][i]);
- }
- int r,v,x;
- while(!q.empty()){
- x=q.front(),q.pop();
- for(int i=;i<;i++){
- r=ch[x][i];
- if(!r){
- ch[x][i]=ch[f[x]][i];
- continue;
- }
- q.push(r);
- f[r]=ch[f[x]][i];
- val[r]+=val[f[r]];
- }
- }
- }
- inline void dp(){
- memset(g,-0x3f,sizeof(g));
- g[][]=;
- int to;
- for(int i=;i<k;i++)
- for(int j=;j<=tot;j++)
- for(int u=;u<;u++){
- to=ch[j][u];
- g[i+][to]=max(g[i+][to],g[i][j]+val[to]);
- }
- for(int i=;i<=tot;i++) ans=max(ans,g[k][i]);
- }
- int main(){
- scanf("%d%d",&n,&k);
- for(int i=;i<=n;i++){
- scanf("%s",s);
- ins();
- }
- get_fail();
- dp();
- printf("%d\n",ans);
- return ;
- }
洛谷 P3041 [USACO12JAN] Video Game Combos的更多相关文章
- 落谷P3041 [USACO12JAN]Video Game G
题目链接 多模式匹配问题,先建 AC 自动机. 套路性的搞个 DP: \(f[i][j]\) 表示前 \(i\) 个字符,当前在 \(AC\) 自动机上的节点是 \(j\) 能匹配到的最多分. 初始化 ...
- 洛谷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> # ...
- 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance
P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...
- 洛谷 P3040 [USACO12JAN]贝尔分享Bale Share
P3040 [USACO12JAN]贝尔分享Bale Share 题目描述 Farmer John has just received a new shipment of N (1 <= N & ...
- [USACO12JAN]Video Game Combos
AC自动机建立fail树后树上DP # include <stdio.h> # include <stdlib.h> # include <iostream> # ...
- 洛谷 P1561 [USACO12JAN]爬山Mountain Climbing
传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最 ...
- 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing
https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce ...
- 洛谷 P2614 计算器弹琴
P2614 计算器弹琴 题目描述 总所周知,计算器可以拿来干很多它本不应该干的事情,比如写作文.(参看洛谷P2549) 小A发现了一个计算器的另一个隐藏功能——弹琴. http://www.bilib ...
随机推荐
- JS格式化时间(支持小程序,兼容IOS)
})-(\d{})-(\d{})T(\d{}):(\d{}):(\d{})/ /** * @function format time * @param val, format * @return {s ...
- IE浏览器被固定启动时访问某网页的处理方法
一.问题的提出 有些windows的GHOST系统在镜像成使用系统后或者正规安装的windows系统在安装金山毒霸.360杀毒软件后,IE浏览器一打开就自动打开某个指定的网站. 二.问题的分析 1.I ...
- [hdu 4417]树状数组+离散化+离线处理
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 把数字离散化,一个查询拆成两个查询,每次查询一个前缀的和.主要问题是这个数组是静态的,如果带修改 ...
- [hdu 1067]bfs+hash
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1067 queue里面果然不能放vector,还是自己写的struct比较省内存…… #include& ...
- [bzoj 1143]最长反链二分图最大匹配
Dilworth定理:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. 证明:http://www.cnblogs.com/itlqs/p/6636222.html 题目让求的是最大反链的 ...
- Linux内存 性能调优
内存是影响Linux性能的主要因素之一,内存资源的充足与否直接影响应用系统的使用性能. free命令:监控Linux内存使用状况. 由上图可知,空闲内存是free+buffers+cached=155 ...
- Python之json编码
一.json JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本信息的语法 1.json轻量级:语法规则 JSON 语法是 ...
- 图论:Gale-Shapley算法
Gale-Shapley算法又叫做延迟认可算法,它可以解决这么一个问题 一共有N位男士和N位女士 每位男士对每位女士都有一个好感度,让他们结合成为N对夫妻,要求男士优先表白,最后问结合情况 第一轮,每 ...
- [BZOJ1982][POJ1740][Spoj 2021]Moving Pebbles|解题报告
这道题的题意BZ和POJ上的都不大清楚... 大概就是给出n堆石子,以及初始每堆石子的个数 两个玩家交替操作,每个操作可以任意在一堆中取任意多的石子 然后再从这堆里拿若干个石子放到某个当前还存在的堆里 ...
- [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式——后缀数组
Brief Description 给定一个字符串,求至少出现k次的最长重复子串. Algorithm Design 先二分答案,然后将后缀分成若干组.判断有没有一个组的后缀个数不小于k.如果有,那么 ...