题目描述

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][],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的更多相关文章

  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. BZOJ day2

    十六题...(好难啊) 1051105910881191119214321876195119682242243824562463276128184720

  2. 【NOIP模拟赛】飞(fly) 数论+树状数组

    树状数组一个被发明以来广为流行的数据结构,基于数组,核心是lowerbit()操作.他向前lowerbit()操作为前缀,向后lowerbit()操作为上辖,我们运用树状数组都是使一个由O(1)变为O ...

  3. 使用setTimeout延时10ms执行onunloadcancel

    在做Web开发时,我们经常用到页面关闭事件onbeforeunload,可以给用户一个选择放弃关闭的机会,就比如这个博客编辑器.如果用户选择了离开,那么onunload事件自然会触发:但若用户选择了取 ...

  4. [MySQL] explain执行计划解读

    Explain语法 EXPLAIN SELECT …… 变体: 1. EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可得 ...

  5. Boke光纤交换机的snmp配置

    今天我配置了一下Boke光纤交换机e-300的snmp trap的配置 敲击help命令你会发现配置snmp的命令共有四个 snmpconfig                      Config ...

  6. C/C++常考面试题(二)

    网上看到的面经,说是dynamic_cast的实现,和RTTI的相关,这才发现原来对这个概念这么模糊,所以作了这个总结. C/C++常考面试题(二) RTTI(Runtime Type Informa ...

  7. HDU 5881--Tea 思维规律

    感谢http://blog.csdn.net/black_miracle/article/details/52567718 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子 ...

  8. Linux : 多线程下载工具: axel

    wget 应该是最常用的下载工具了,但是其不支持多线程下载. axel 安装 epel 源有 axel 的二进制包,可以使用 yum 安装. yum install epel-release yum ...

  9. [bzoj2038][2009国家集训队]小Z的袜子(hose)——莫队算法

    Brief Description 给定一个序列,您需要处理m个询问,每个询问形如[l,r],您需要回答在区间[l,r]中任意选取两个数相同的概率. Algorithm Design 莫队算法入门题目 ...

  10. poj 2312 Battle City(优先队列+bfs)

    题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...