洛谷 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 ...
随机推荐
- BZOJ3223: Tyvj 1729 文艺平衡树 无旋Treap
一开始光知道pushdown却忘了pushup......... #include<cstdio> #include<iostream> #include<cstring ...
- python3创建目录
感觉python3最好用的创建目录函数是os.makedirs,它可以设置在多级目录不存在时自动创建,已经存在也不抛出异常. import os os.makedirs('hello/hello1/h ...
- B. Light It Up 思维题
Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedu ...
- MAC电脑密码破解
[第一个方法] 开机,启动时按cmd+S,进入Single User Mode,出现像DOS一样的提示符#root> 在#root>下输入(注意空格,大小写) fsck -y moun ...
- Tomcat部署时war和war exploded区别及验证
war和war exploded的区别 在使用IDEA开发项目的时候,部署Tomcat的时候通常会出现下边的情况: 是选择war还是war exploded 这里首先看一下他们两个的区别: war模式 ...
- zhudongfangyu.exe进程是360主动防御进程,用来监控电脑系统,防止电脑病毒出现并阻止病毒或木马的安全进程
zhudongfangyu.exe进程是360主动防御进程,用来监控电脑系统,防止电脑病毒出现并阻止病毒或木马的安全进程
- go的websocket实现
websocket分为握手和数据传输阶段,即进行了HTTP握手 + 双工的TCP连接 RFC协议文档在:http://tools.ietf.org/html/rfc6455 握手阶段 握手阶段就是普通 ...
- 算法题之Climbing Stairs(leetcode 70)
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- For循环中不可以嵌套RDD操作
今天犯了一个致命理解错误,Spark中的RDD Map操作只是一个计算式的传递,并不是Action,也就是在for循环中不会产生真正的计算. 因此,如果for循环中出现了RDD的Map类似操作,都会引 ...
- [ Mongodb ] 问题总汇
1. Mongodb备份 [root@localhost ~]# mongodump -h /users 2. Mongodb恢复 [root@localhost ~]# mongorestore - ...