题目描述

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.

输入输出样例

输入样例#1: 
3 7
ABA
CB
ABACB
输出样例#1: 
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][3],n,m,k,ans=0;
int root=0,tot=0,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=0;i<len;i++){int c=id(s[i]);if(!ch[now][c]) ch[now][c]=++tot;now=ch[now][c];}val[now]=1;
}inline void get_fail(){queue<int> q;for(int i=0;i<3;i++) if(ch[0][i]){q.push(ch[0][i]);}int r,v,x;while(!q.empty()){x=q.front(),q.pop();for(int i=0;i<3;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[0][0]=0;int to;for(int i=0;i<k;i++)for(int j=0;j<=tot;j++)for(int u=0;u<3;u++){to=ch[j][u];g[i+1][to]=max(g[i+1][to],g[i][j]+val[to]);}for(int i=0;i<=tot;i++) ans=max(ans,g[k][i]);
}int main(){scanf("%d%d",&n,&k);for(int i=1;i<=n;i++){scanf("%s",s);ins();}get_fail();dp();printf("%d\n",ans);return 0;
}

转载于:https://www.cnblogs.com/JYYHH/p/8280569.html

洛谷 P3041 [USACO12JAN] Video Game Combos相关推荐

  1. P3041 [USACO12JAN]Video Game Combos【AC自动机+DP】

    时空限制 1000ms / 128MB 题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', an ...

  2. 洛谷 P3041 视频游戏的连击Video Game Combos(AC自动机+拓扑排序+数位DP)

    洛谷 P3041 视频游戏的连击Video Game Combos 难度一般,不过这个数位DP其实应该叫做记忆化搜索 题意:玩游戏时可以通过按键组合打出combo技能:然后是已知N个combo的按键方 ...

  3. 洛谷P1561 [USACO12JAN]爬山Mountain Climbing 贪心 数学

    洛谷P1561 [USACO12JAN]爬山Mountain Climbing 贪心 数学 1.我们可以发现最终的答案 = max( 上山时间总和 + 最快下山时间,下山时间总和 +最快上山时间 ) ...

  4. 【洛谷 P3041】 [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机,dp)

    题目链接 手写一下AC自动机(我可没说我之前不是手写的) Trie上dp,每个点的贡献加上所有是他后缀的串的贡献,也就是这个点到根的fail链的和. #include <cstdio> # ...

  5. [USACO12JAN]Video Game Combos

    AC自动机建立fail树后树上DP # include <stdio.h> # include <stdlib.h> # include <iostream> # ...

  6. luogu P3041 [USACO12JAN]视频游戏的连击Video Game Combos

    P3041 [USACO12JAN]视频游戏的连击Video Game Combos 题目大意: 给出n个字符串st[1-n],求一个长度为K的字符串,每匹配到st中的字符串就+1分,问最多能加几分 ...

  7. 洛谷、牛客网、AcWing 刷题(python版)

    牛客网python专项练习整理(一) https://blog.csdn.net/weixin_41913008/article/details/87203468 牛客网剑指offer--python ...

  8. 洛谷题解——P1873:砍树

    视频讲解可以直接点击这个 B 站链接,https://www.bilibili.com/video/BV1jk4y1k7hq/. 题目相关 题目链接 洛谷,https://www.luogu.com. ...

  9. 洛谷题解——P1024:一元三次方程求解

    视频讲解可以直接点击这个 B 站链接,https://www.bilibili.com/video/BV1qT4y13717/. 题目相关 题目链接 洛谷,https://www.luogu.com. ...

最新文章

  1. 为什么分布式一定要有redis?
  2. 均值极差图控制上下限_SPC之I-MR控制图
  3. python死磕八之迭代器与生成器
  4. 倒数58天 -- 分治法 -- 使用循环求方程的一个解
  5. 批处理脚本手动双击可以执行,但计划任务中执行失败
  6. 偶然翻开旧日记本,发现了好多的情诗(三)!!!!
  7. (zt)svn 随服务器启动
  8. [Hive]-Table
  9. Qt QSsh 使用 windows Qt实现ssh客户端
  10. 2021财经直播系统 H5网页直播 大区直播间源码
  11. 三菱q系列plc连接电脑步骤_SERVER和三菱Q系列PLC通讯设置步骤
  12. 使用Qt对Excel复选框等进行阅读、修改
  13. 想成为游戏原画师需要哪些系统的学习?
  14. 悉尼大学INFO1110/COMP9001课业解析
  15. c语言中的下标变量是什么,c语言引用数组元素时其数组下标的允许的数据类型是什么...
  16. 华为手机主界面的返回键怎么调出来_主按钮怎么变回来 华为手机的返回键怎么设置?...
  17. 微信撤回软件安卓版_微信拍一拍撤回软件下载
  18. 第三回 利器,我的DHCP (转)
  19. 线代 | 线性代数的本质 本质 本质 nature
  20. 德鲁克日志读后感之八十八

热门文章

  1. 转贴:网友叶落扬天利用火鸟字幕合并器来学习美剧中的英语的心得
  2. 参加科学教师与计算机培训总结报告,信息技术校本培训总结
  3. 空间计量实践操作-MATLAB代码①
  4. .net导入Excel 并显示进度条
  5. 服务器并发量之突破C10K的问题
  6. 劳务派遣信息管理系统软件是什么,教你了解劳务派遣管理系统!
  7. 微信小程序关于内层view设置margin-top无效的解决方案
  8. (四)unity3D脚本的执行顺序
  9. 单细胞基因可视化之UMAP图修饰
  10. 提取图片中的文字如何实现?两种方法均可操作!