Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 159  Solved: 110
[Submit][Status][Discuss]

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.

Source

AC自动机应该不难看出来

按照套路dp,设$f[i][j]$表示枚举到第$i$个位置,现在位于自动机上的第$i$位。

转移的时候枚举下一个位置就好

有两个需要注意的地方

1.Trie树在我们建fail树的时候实际被我们改造成了Trie图,因此每个节点是可能被多次枚举到的,需要对自身取$max$

2.有些深度大于当前枚举长度的点是不可能走到的,因此开始时应把每个点的权值设为$-INF$(root除外)

// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = , B = ;
int T, K;
char s[];
int ch[MAXN][], f[][MAXN], fail[MAXN], val[MAXN], tot = , root = ;
void insert(char *s) {
int N = strlen(s + );
int now = root;
for(int i = ; i <= N; i++) {
int x = s[i] - 'A';
if(!ch[now][x]) ch[now][x] = ++tot;
now = ch[now][x];
}
val[now]++;
}
void GetFail() {
queue<int> q;
for(int i = ; i < B; i++) if(ch[root][i]) q.push(ch[root][i]);
while(!q.empty()) {
int p = q.front(); q.pop();
for(int i = ; i < B; i++) {
if(!ch[p][i]) ch[p][i] = ch[fail[p]][i];
else fail[ch[p][i]] = ch[fail[p]][i], q.push(ch[p][i]);
}
val[p] += val[fail[p]];
}
}
int Dp() {
memset(f, -0x3f, sizeof(f));
for(int i = ; i <= K; i++) f[i][] = ;//óDD?×′ì?ê?2??é?ü′?μ?μ?£?òò′?Dèòa?e2??üD?
int ans = ;
for(int i = ; i <= K; i++)
for(int j = ; j <= tot; j++)
for(int k = ; k < B; k++) {
int son = ch[j][k];
if(son) {
f[i][son] = max(f[i][son], f[i - ][j] + val[son]);
printf("%d %d %d %d %d\n", i, j, k, son, f[i][son]);
} }
for(int i = ; i <= tot; i++)
ans = max(ans, f[K][i]);
return ans;
}
int main() {
#ifdef WIN32
freopen("a.in", "r", stdin);
#endif
scanf("%d %d", &T, &K);
for(int i = ; i <= T; i++)
scanf("%s", s + ), insert(s);
GetFail();
printf("%d", Dp());
return ;
}

BZOJ2580: [Usaco2012 Jan]Video Game(AC自动机)的更多相关文章

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

    BZOJ_2580_[Usaco2012 Jan]Video Game_AC自动机+DP Description Bessie is playing a video game! In the game ...

  2. BZOJ 2580: [Usaco2012 Jan]Video Game

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

  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. BZOJ2580:[USACO]Video Game(AC自动机,DP)

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

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

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

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

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

  7. [USACO12Jan][luogu3041] Video Game Combos [AC自动机+dp]

    题面 传送门 思路 首先,有一个非常显然的思路就是dp: 设$dp[i][j]$表示前i个字符,最后一个为j 然后发现这个东西有后效性 改!设$dp[i][j]$代表前i个字符,最后15个的状态为j( ...

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

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

  9. hdu-Danganronpa(AC自动机)

    Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, t ...

随机推荐

  1. TypeScript 入门笔记

    1.原始数据类型 JavaScript 的类型分为两种:原始数据类型和对象数据类型.原始数据类型包括布尔值.数值.字符串.null.undefined 以及 ES6 中的 Symbol. 前五种数据类 ...

  2. oracle学习篇二:常用SQL

    ------------------------1.简单的SQL查询--------------------------select * from emp;select empno,ename,job ...

  3. CSS Hack兼容

    CSS中有很多标签在不同浏览器中有不同的兼容性问题,问了让网页的功能更好的不同浏览器中显示正常, 需要通过hack的方式来解决浏览器兼容问题. CSS hack问题由来已久,目前我的了解甚少,以下是转 ...

  4. 尝试VS插件

    从试用vs2013开始,ide变得越来越智能,但是vs2013总是会出一些莫名其妙的问题,导致编译不成功,不能跟vs2010共享等等.于是由再次回到vs2010. 现在vs2015的update1更新 ...

  5. 面向对象设计中private,public,protected的访问控制原则及静态代码块的初始化顺序

    第一:private, public, protected访问标号的访问范围. private:只能由          1.该类中的函数          2.其友元函数访问 不能被任何其他访问,该 ...

  6. OpenSUSE 内核编译教程 (kernel 2.6.x)

    http://cn.opensuse.org/OpenSUSE_%E5%86%85%E6%A0%B8%E7%BC%96%E8%AF%91%E6%95%99%E7%A8%8B_(kernel_2.6.x ...

  7. Struts2学习-拦截器2

    1.做一个登陆页面(loginView.jsp,才用Action来访问),2.登陆成功后,可以跳转到系统的首页(index.jsp),3.首页有一个链接(testOtherAction访问其它的功能模 ...

  8. linux内核编译与开发

    一.Linux内核简介linux kernel map: linux 系统体系结构: linux kernel体系结构: arm有7种工作模式,x86也实现了4个不同级别RING0-RING3,RIN ...

  9. May 10th 2017 Week 19th Wednesday

    Imagination is the source of creation. 想象是创作之源. Sometimes, creation and innovation are very simple, ...

  10. LAB2 软件测试 Selenium上机实验 2017

    1.安装SeleniumIDE插件 打开Firefox——>菜单栏——>附加组件——>获取附加组件——>查看更多附加组件——>搜索框输入SeleniumIDE并查找——& ...