题目描述

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,不过dp那块还是调了半天。。

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,cnt,ans,k;
int fail[];
int end[];
int ch[][];
int dp[][];
bool vis[][];
char s[];
void build(string s)
{
int len=s.length();
int now=;
for(int i=;i<len;i++)
{
if(!ch[now][s[i]-'A']) ch[now][s[i]-'A']=++cnt;
now=ch[now][s[i]-'A'];
}
end[now]+=;
}
void build_fail()
{
queue<int>q;
for(int i=;i<;i++)
if(ch[][i])
q.push(ch[][i]);
while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=;i<;i++)
{
if(ch[u][i])
{
fail[ch[u][i]]=ch[fail[u]][i];
q.push(ch[u][i]);
}
else ch[u][i]=ch[fail[u]][i];
}
}
}
int get(int now,int val)
{
while(now) val+=end[now],now=fail[now];
return val;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%s",s);
build(s);
}
build_fail();
vis[][]=;
for(int i=;i<k;i++)
for(int j=;j<=cnt;j++)
{
if(!vis[i][j]) continue;
for(int l=;l<;l++)
{
int now=ch[j][l];
dp[i+][now]=max(dp[i+][now],get(now,dp[i][j]));
vis[i+][now]=true;
}
}
for(int i=;i<=cnt;i++) ans=max(ans,dp[k][i]);
printf("%d",ans);
return ;
}

[洛谷3041]视频游戏的连击Video Game Combos的更多相关文章

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

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

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

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

  3. 【USACO12JAN】视频游戏的连击Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

  4. [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机+DP)

    Description 贝西正在打格斗游戏.游戏里只有三个按键,分别是“A”.“B”和“C”.游戏中有 N 种连击 模式,第 i 种连击模式以字符串 Si 表示,只要贝西的按键中出现了这个字符串,就算 ...

  5. [Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos

    题面 sol 设\(f_{i,j}\)表示填了前\(i\)个字母,在\(AC\)自动机上跑到了节点\(j\)的最大得分.因为匹配需要暴跳\(fail\)所以预先把\(fail\)指针上面的匹配数传下来 ...

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

    思路 简单的AC自动机上dp,暴力跳fail向子节点直接转移即可 代码 #include <cstdio> #include <algorithm> #include < ...

  7. 【题解】[USACO12JAN]视频游戏的连击Video Game Combos

    好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...

  8. 洛谷 P2197 nim游戏

    洛谷 P2197 nim游戏 题目描述 甲,乙两个人玩Nim取石子游戏. nim游戏的规则是这样的:地上有n堆石子(每堆石子数量小于10000),每人每次可从任意一堆石子里取出任意多枚石子扔掉,可以取 ...

  9. 洛谷 P1965 转圈游戏

    洛谷 P1965 转圈游戏 传送门 思路 每一轮第 0 号位置上的小伙伴顺时针走到第 m 号位置,第 1 号位置小伙伴走到第 m+1 号位置,--,依此类推,第n − m号位置上的小伙伴走到第 0 号 ...

随机推荐

  1. python uwsgi 部署以及优化

    这篇文章其实两个月之前就应该面世了,但是最近琐事.烦心事太多就一直懒得动笔,拖到现在才写 一.uwsgi.wsgi.fastcgi区别和联系 参见之前的文章 http://www.cnblogs.co ...

  2. c++ 流继承关系

  3. 编写高质量代码--改善python程序的建议(七)

    原文发表在我的博客主页,转载请注明出处! 建议三十四:掌握字符串的基本用法 编程有两件事,一件是处理数值,另一件是处理字符串,在商业应用编程来说,处理字符串的代码超过八成,所以需要重点掌握. 首先有个 ...

  4. 【BZOJ1491】[NOI2007]社交网络 Floyd

    [BZOJ1491][NOI2007]社交网络 Description 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子 ...

  5. spring定时器的cronexpression表达式

    转自:https://www.cnblogs.com/yaowen/p/3779284.html 相关配置: import com.alibaba.dubbo.config.annotation.Se ...

  6. Linux中Oracle的sqlplus下退格和Del键无效的问题解决

    利用rlwrap工具解决方法 1.安装rlwrap和readline库 CentOS下可以用EPEL的yum源直接安装,步骤如下: (1)RHEL/CentOS/SL Linux 6.x 下安装 EP ...

  7. dev grid 常用方法

    绑定数据源 public void Data(){DataTable td = new DataTable();DataRow row = td.NewRow();foreach (GridColum ...

  8. Hibernate 处理事务

    1. Hibernate 的持久化类 1.1 什么是持久化类 持久化类: 就是一个 Java 类(JavaBean),这个 Java类与表建立了映射关系就可以是持久化类; 持久化类 = JavaBea ...

  9. sql语句select group by order by where一般先后顺序

    写的顺序:select ... from... where.... group by... having... order by..执行顺序:from... where...group by... h ...

  10. socketserver模块、MySQL(数据库、数据表的操作)

    一.socketserver实现并发 基于tcp的套接字,关键就是两个循环,一个链接循环,一个通信循环. socketserver模块中分两大类:server类(解决链接问题)和request类(解决 ...