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. 前端技术之_CSS详解第三天

    前端技术之_CSS详解第三天 二.权重问题深入 2.1 同一个标签,携带了多个类名,有冲突: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...

  2. java学习日记-基础-列出2~100内的素数

    素数的概念:一个整数如果只能整除1和它本身,那么这个整数就是一个素数 方法一:素数是除去能被2整除.3整除.5整除.7整除的整数,但包含2,3,5,7 public class Sushu { pub ...

  3. 基于jQuery的AJAX实现三级联动菜单

    最近学习jQuery,所以就写了一个关于中国省市县/区的三级联动菜单,权当相互学习,相互促进,特此记录. 下面是嵌套js的html文件: <!DOCTYPE html> <html ...

  4. Hadoop 实现 TF-IDF 计算

    学习Hadoop 实现TF-IDF 算法,使用的是CDH5.13.1 VM版本,Hadoop用的是2.6.0的jar包,Maven中增加如下即可 <dependency> <grou ...

  5. C# Dispose模式详细分析

    C#Dispose模式 目的: 为了及时释放宝贵的非托管资源和托管资源,并且保证资源在被gc回收的时候可以正确释放资源,同时兼顾执行效率 必须遵循的事实: 1 托管资源释放: 由另一线程的gc进行释放 ...

  6. builder设计模式(摘录ITeye文章lintomny)

    对于Builder模式很简单,但是一直想不明白为什么要这么设计,为什么要向builder要Product而不是向知道建造过程的Director要.刚才google到一篇文章,总算清楚了.在这里转贴一下 ...

  7. printf输出格式介绍(转)

    格式代码 A ABC ABCDEFGH %S A ABC ABCDEFGH %5S ####A ##ABC ABCDEFGH %.5S A ABC ABCDE %5.5S ####A ##ABC AB ...

  8. python之Flask实现登录功能

    网站少不了要和数据库打交道,归根到底都是一些增删改查操作,这里做一个简单的用户登录功能来学习一下Flask如何操作MySQL. 用到的一些知识点:Flask-SQLAlchemy.Flask-Logi ...

  9. IOC 的理解与解释

    IOC 是什么? Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不 ...

  10. mybatis一对一映射配置详解

    听说mybatis一对一有三种写法,今天我试了一下. 数据库表准备 为了偷懒,我直接就拿用户权限菜单里的菜单表和菜单与权限的中间表做实现,他们原来是多对多的关系,这边我假设这两张表是一对一. 表  g ...