Description

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?
给出n个ABC串combo[1..n]和k,现要求生成一个长k的字符串S,问S与word[1..n]的最大匹配数

Input

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.

Output

Line 1: A single integer, the maximum number of points Bessie can obtain.

Sample Input

3 7 ABA CB ABACB

Sample Output

4

HINT

The optimal sequence of buttons in this case is ABACBCB, which gives 4 points--1 from ABA, 1 from ABACB, and 2 from CB.

Solution

用$End[i]$表示$i$结点及其所有的后缀(也就是其$fail$树上所有祖先)的字符串个数。

设$f[i][j]$表示匹配到$i$点,长度为$j$,转移比较显然。

Code

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define N (1009)
using namespace std; int n,k,cnt,ans,f[N][N];
int Son[N][],End[N],Fail[N];
char s[N];
queue<int>q; void Insert(char s[])
{
int now=,len=strlen(s);
for (int i=; i<len; ++i)
{
int x=s[i]-'A';
if (!Son[now][x]) Son[now][x]=++cnt;
now=Son[now][x];
}
++End[now];
} void Build_Fail()
{
for (int i=; i<; ++i)
if (Son[][i]) q.push(Son[][i]);
while (!q.empty())
{
int now=q.front(); q.pop();
End[now]+=End[Fail[now]];
for (int i=; i<; ++i)
{
if (!Son[now][i])
{
Son[now][i]=Son[Fail[now]][i];
continue;
}
Fail[Son[now][i]]=Son[Fail[now]][i];
q.push(Son[now][i]);
}
}
} int main()
{
scanf("%d%d",&n,&k);
for (int i=; i<=n; ++i)
scanf("%s",s), Insert(s);
Build_Fail();
memset(f,-0x7f,sizeof(f));
f[][]=;
for (int i=; i<=k; ++i)
for (int j=; j<=cnt; ++j)
for (int k=; k<; ++k)
f[Son[j][k]][i+]=max(f[Son[j][k]][i+],f[j][i]+End[Son[j][k]]);
for (int i=; i<=cnt; ++i) ans=max(ans,f[i][k]);
printf("%d\n",ans);
}

BZOJ2580:[USACO]Video Game(AC自动机,DP)的更多相关文章

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

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

  2. POJ1625 Censored!(AC自动机+DP)

    题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后 ...

  3. HDU2296 Ring(AC自动机+DP)

    题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个 ...

  4. HDU2457 DNA repair(AC自动机+DP)

    题目一串DNA最少需要修改几个基因使其不包含一些致病DNA片段. 这道题应该是AC自动机+DP的入门题了,有POJ2778基础不难写出来. dp[i][j]表示原DNA前i位(在AC自动机上转移i步) ...

  5. hdu 4117 GRE Words AC自动机DP

    题目:给出n个串,问最多能够选出多少个串,使得前面串是后面串的子串(按照输入顺序) 分析: 其实这题是这题SPOJ 7758. Growing Strings AC自动机DP的进阶版本,主题思想差不多 ...

  6. hdu 2457(ac自动机+dp)

    题意:容易理解... 分析:这是一道比较简单的ac自动机+dp的题了,直接上代码. 代码实现: #include<stdio.h> #include<string.h> #in ...

  7. HDU 2425 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU2296——Ring(AC自动机+DP)

    题意:输入N代表字符串长度,输入M代表喜欢的词语的个数,接下来是M个词语,然后是M个词语每个的价值.求字符串的最大价值.每个单词的价值就是单价*出现次数.单词可以重叠.如果不止一个答案,选择字典序最小 ...

  9. tyvj P1519 博彩游戏(AC自动机+DP滚动数组)

    P1519 博彩游戏 背景 Bob最近迷上了一个博彩游戏…… 描述 这个游戏的规则是这样的:每花一块钱可以得到一个随机数R,花上N块钱就可以得到一个随机序列:有M个序列,如果某个序列是产生的随机序列的 ...

随机推荐

  1. 使用WPF教你一步一步实现连连看(三)

    这次首先对以前的结构进行了调整: 第一步:把MyButton按钮的属性独立成一个类,放在一个单独的MyButton.cs中,把图片的初始化也放到里面. 调整代码如下: public class MyB ...

  2. 选择ORACLE数据库字符集

    如何选择数据库的字符集是一个有争议的话题,字符集本身涉及的范围很广,它与应用程序.客户的本地环境.操作系统.服务器等关系很密切,因此要做出合适的 选择,需要明白这些因素之间的关系.另外对字符集的基本概 ...

  3. eclipse连接VisualSVN Server

    1.下载安装VisualSVN Server 2.修改资源库的网络连接.去掉默认的选中,修改端口,点击ok. 3.新建资源库Test,显示连接的地址http://svnybb/svn/Test/ .之 ...

  4. Only fullscreen activities can request orientation

    问题 当我们把targetSdkVersion升级到27,buildToolsVersion和相关的support library升级到27.0.2后,在Android 8.0(API level 2 ...

  5. Python paramiko ssh 在同一个session里run多个命令

    import threading, paramiko strdata='' fulldata='' class ssh: shell = None client = None transport = ...

  6. CSS 水平居中和垂直居中

    1.水平居中——行内元素 text-align: center; 2.水平居中——定宽块状元素 margin: auto,满足定宽和块状两个条件的元素是可以通过设置“左右margin”值为“auto” ...

  7. bzoj 2406: 矩阵 ——solution

    对于100%的数据满足N,M<=200,0<=L<=R<=1000,0<=Aij<=1000 http://www.lydsy.com/JudgeOnline/pr ...

  8. drupal常用api

    最短的函数 // 语言字串,除了可以获取对应语言外,还可以设置字串变量.可以是!var, @var或 %var,%var就添加元素外层.@var会过滤HTML,!var会原样输出HTML,%var会添 ...

  9. 怎么配置wamp下mysql的编码

    Windows下的Apache+Mysql/MariaDB+Perl/PHP/Python,一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高 ...

  10. LeetCode题解之Find All Duplicates in an Array

    1.题目描述 2.问题分析 将数组中的元素 A[i] 放到 A[ A[i] - 1] 的位置.然后遍历一边数组,如果不满足 A[i] == i+1,则将A[i]添加到 结果中. 3.代码 vector ...