BZOJ_2580_[Usaco2012 Jan]Video Game_AC自动机+DP

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

先对那些combo串建立AC自动机。
每个节点维护出fail树的子树和。
设F[i][j]表示i个字符,现在在j号节点上的最大匹配数
转移就F[i+1][ch[j][c]]=max(F[i+1][ch[j][c]],F[i][j]+siz[ch[j][c]])即可。
 
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 1050
#define M 660
int ch[M][3],fail[M],cnt=1,siz[M],f[N][M],n,m,Q[M],l,r;
char w[22];
void insert() {
int i,p=1,lw=strlen(w+1);
for(i=1;i<=lw;i++) {
int &k=ch[p][w[i]-'A'];
if(!k) k=++cnt; p=k;
}
siz[p]=1;
}
void build() {
int p,i;
for(i=0;i<3;i++) ch[0][i]=1;
Q[r++]=1;
while(l<r) {
p=Q[l++];
for(i=0;i<3;i++) {
if(ch[p][i]) fail[ch[p][i]]=ch[fail[p]][i],Q[r++]=ch[p][i];
else ch[p][i]=ch[fail[p]][i];
}
siz[p]+=siz[fail[p]];
}
}
int main() {
scanf("%d%d",&m,&n);
int i,j,k;
for(i=1;i<=m;i++) {
scanf("%s",w+1);
insert();
}
build();
memset(f,0x80,sizeof(f));
f[0][1]=0;
for(i=1;i<=n;i++) {
for(j=1;j<=cnt;j++) {
for(k=0;k<3;k++) {
f[i][ch[j][k]]=max(f[i][ch[j][k]],f[i-1][j]+siz[ch[j][k]]);
}
}
}
int ans=0;
for(i=1;i<=cnt;i++) ans=max(ans,f[n][i]);
printf("%d\n",ans);
}

BZOJ_2580_[Usaco2012 Jan]Video Game_AC自动机+DP的更多相关文章

  1. BZOJ 2580: [Usaco2012 Jan]Video Game

    2580: [Usaco2012 Jan]Video Game Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 142  Solved: 96[Subm ...

  2. BZOJ2580: [Usaco2012 Jan]Video Game(AC自动机)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 159  Solved: 110[Submit][Status][Discuss] Descriptio ...

  3. [Usaco2012 Jan]Video Game

    Description Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the ...

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

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

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

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

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

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

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

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

  8. hdu 4117 GRE Words AC自动机DP

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

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

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

随机推荐

  1. Android 加载gif图片强大框架(支持预加载、缓存,还支持显示静态图片,一行代码全搞定)

    之前项目中没有涉及到显示gif图片的功能,也没有着重研究过,最近项目中要用到显示gif图片,于是就在网上一顿搜,用过之后发现如下几个缺点. 1.加载大的gif图片会出现oom. 2.没有预加载和缓存功 ...

  2. 初步认识thymeleaf:简单表达式和标签(二)

    1.th:each:循环,<tr th:each="user,userStat:${users}">,userStat是状态变量,有 index,count,size, ...

  3. Collections模块下的Counter

    class Counter(dict) 这个类是dict的子类,对哈希类型的项进行计数,元素被存储为字典的键,他们的计数将作为字典的键值. 主要介绍两个方法: 1.初始化方法:__init__(*ar ...

  4. docker的安装和技巧

    工作了有一段时间,开发环境中需要docker环境,但是docker一直不算很熟,之前一直是利用yum安装,但是yum安装真的很费劲,所以总结了一些经验给大家: 1,利用yum直接安装 官网是直接给了y ...

  5. JAVA 第一张 使用记事本编写代码

    使用记事本开发JAVA程序的步骤

  6. DDGScreenShot—截取图片的任意部分

    写在前面 DDGScreenShot 库提供了截取任意图片的功能, 支持手势截图,当然,输入任意的区域也可以,下面看看具体的代码 代码如下: 方法封装 /** ** 用手势截图(截取图片的任意部分) ...

  7. Yii2数据接口

    写接口之前先确认那你已经安装了Yii2的basic版或者advanced版,如果还没有,赶快去看这篇文章:composer安装Yii2. 现在默认你已经安装了basic版或者advanced版了,并且 ...

  8. html块级元素与行内元素

    1.关于行内元素和快元素的说明: 根据CSS规范的规定,每一个网页元素都有一个display属性,用于确定该元素的类型,每一个元素都有默认的display属性值,比如div元素,它的默认display ...

  9. Flex 右键菜单控制

    //设置监控右键菜单项 private function setUserMenuItem():void{ var contextMenu:ContextMenu = new ContextMenu() ...

  10. rsync 密钥文件错误问题总结

    rsync 可以使用 --password-file 选项指定密钥文件,密钥文件中简单存放 rsync 密码:在第一次使用密钥文件的时候经常遇到文件权限相关问题:这里总结一下,我遇到的问题. 问题描述 ...