题目描述

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自动机白学了。

  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define maxn 1005
  4. using namespace std;
  5. int ch[maxn][],n,m,k,ans=;
  6. int root=,tot=,val[maxn];
  7. int f[maxn],g[maxn][maxn];
  8. char s[maxn];
  9.  
  10. inline int id(char c){
  11. return c-'A';
  12. }
  13.  
  14. inline void ins(){
  15. int len=strlen(s),now=root;
  16. for(int i=;i<len;i++){
  17. int c=id(s[i]);
  18. if(!ch[now][c]) ch[now][c]=++tot;
  19. now=ch[now][c];
  20. }
  21. val[now]=;
  22. }
  23.  
  24. inline void get_fail(){
  25. queue<int> q;
  26. for(int i=;i<;i++) if(ch[][i]){
  27. q.push(ch[][i]);
  28. }
  29.  
  30. int r,v,x;
  31. while(!q.empty()){
  32. x=q.front(),q.pop();
  33. for(int i=;i<;i++){
  34. r=ch[x][i];
  35. if(!r){
  36. ch[x][i]=ch[f[x]][i];
  37. continue;
  38. }
  39.  
  40. q.push(r);
  41. f[r]=ch[f[x]][i];
  42. val[r]+=val[f[r]];
  43. }
  44. }
  45. }
  46.  
  47. inline void dp(){
  48. memset(g,-0x3f,sizeof(g));
  49. g[][]=;
  50. int to;
  51. for(int i=;i<k;i++)
  52. for(int j=;j<=tot;j++)
  53. for(int u=;u<;u++){
  54. to=ch[j][u];
  55. g[i+][to]=max(g[i+][to],g[i][j]+val[to]);
  56. }
  57.  
  58. for(int i=;i<=tot;i++) ans=max(ans,g[k][i]);
  59. }
  60.  
  61. int main(){
  62. scanf("%d%d",&n,&k);
  63. for(int i=;i<=n;i++){
  64. scanf("%s",s);
  65. ins();
  66. }
  67.  
  68. get_fail();
  69. dp();
  70.  
  71. printf("%d\n",ans);
  72. return ;
  73. }

洛谷 P3041 [USACO12JAN] Video Game Combos的更多相关文章

  1. 落谷P3041 [USACO12JAN]Video Game G

    题目链接 多模式匹配问题,先建 AC 自动机. 套路性的搞个 DP: \(f[i][j]\) 表示前 \(i\) 个字符,当前在 \(AC\) 自动机上的节点是 \(j\) 能匹配到的最多分. 初始化 ...

  2. 洛谷P3041 视频游戏的连击Video Game Combos [USACO12JAN] AC自动机+dp

    正解:AC自动机+dp 解题报告: 传送门! 算是个比较套路的AC自动机+dp趴,,, 显然就普普通通地设状态,普普通通地转移,大概就f[i][j]:长度为i匹配到j 唯一注意的是,要加上所有子串的贡 ...

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

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

  4. 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  5. 洛谷 P3040 [USACO12JAN]贝尔分享Bale Share

    P3040 [USACO12JAN]贝尔分享Bale Share 题目描述 Farmer John has just received a new shipment of N (1 <= N & ...

  6. [USACO12JAN]Video Game Combos

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

  7. 洛谷 P1561 [USACO12JAN]爬山Mountain Climbing

    传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最 ...

  8. 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing

    https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce ...

  9. 洛谷 P2614 计算器弹琴

    P2614 计算器弹琴 题目描述 总所周知,计算器可以拿来干很多它本不应该干的事情,比如写作文.(参看洛谷P2549) 小A发现了一个计算器的另一个隐藏功能——弹琴. http://www.bilib ...

随机推荐

  1. JS格式化时间(支持小程序,兼容IOS)

    })-(\d{})-(\d{})T(\d{}):(\d{}):(\d{})/ /** * @function format time * @param val, format * @return {s ...

  2. IE浏览器被固定启动时访问某网页的处理方法

    一.问题的提出 有些windows的GHOST系统在镜像成使用系统后或者正规安装的windows系统在安装金山毒霸.360杀毒软件后,IE浏览器一打开就自动打开某个指定的网站. 二.问题的分析 1.I ...

  3. [hdu 4417]树状数组+离散化+离线处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 把数字离散化,一个查询拆成两个查询,每次查询一个前缀的和.主要问题是这个数组是静态的,如果带修改 ...

  4. [hdu 1067]bfs+hash

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1067 queue里面果然不能放vector,还是自己写的struct比较省内存…… #include& ...

  5. [bzoj 1143]最长反链二分图最大匹配

    Dilworth定理:偏序集能划分成的最少的全序集的个数与最大反链的元素个数相等. 证明:http://www.cnblogs.com/itlqs/p/6636222.html 题目让求的是最大反链的 ...

  6. Linux内存 性能调优

    内存是影响Linux性能的主要因素之一,内存资源的充足与否直接影响应用系统的使用性能. free命令:监控Linux内存使用状况. 由上图可知,空闲内存是free+buffers+cached=155 ...

  7. Python之json编码

    一.json JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本信息的语法 1.json轻量级:语法规则 JSON 语法是 ...

  8. 图论:Gale-Shapley算法

    Gale-Shapley算法又叫做延迟认可算法,它可以解决这么一个问题 一共有N位男士和N位女士 每位男士对每位女士都有一个好感度,让他们结合成为N对夫妻,要求男士优先表白,最后问结合情况 第一轮,每 ...

  9. [BZOJ1982][POJ1740][Spoj 2021]Moving Pebbles|解题报告

    这道题的题意BZ和POJ上的都不大清楚... 大概就是给出n堆石子,以及初始每堆石子的个数 两个玩家交替操作,每个操作可以任意在一堆中取任意多的石子 然后再从这堆里拿若干个石子放到某个当前还存在的堆里 ...

  10. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式——后缀数组

    Brief Description 给定一个字符串,求至少出现k次的最长重复子串. Algorithm Design 先二分答案,然后将后缀分成若干组.判断有没有一个组的后缀个数不小于k.如果有,那么 ...