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. hdu 1520 树形DP基础

    http://acm.hdu.edu.cn/showproblem.php?pid=1520 父节点和子节点不能同时选. http://blog.csdn.net/woshi250hua/articl ...

  2. linux shell基础编程2

    while循环 语法1: while [ 条件 ] do 命令序列 done 语法2: while read -r line do 命令序列 done (切记while和左中括号一定要有空格) 例子 ...

  3. cf375D. Tree and Queries(莫队)

    题意 题目链接 给出一棵 n 个结点的树,每个结点有一个颜色 c i . 询问 q 次,每次询问以 v 结点为根的子树中,出现次数 ≥k 的颜色有多少种.树的根节点是1. Sol 想到了主席树和启发式 ...

  4. CompletionService的poll方法

    1.poll():马上返回完成的任务,若没有,则返回null 2.poll(long timeout, TimeUnit unit): 等待timeout时间,如果大于最短任务完成时间,则获取任务结果 ...

  5. Android应用开发基础之一:数据存储和界面展现(一)

    Android项目的目录结构 Activity:应用被打开时显示的界面 src:项目代码 R.java:项目中所有资源文件的资源id Android.jar:Android的jar包,导入此包方可使用 ...

  6. 在 Windows Vista、Windows 7 和 Windows Server 2008 上设置 SharePoint 2010 开发环境

    适用范围: SharePoint Foundation 2010 | SharePoint Server 2010 本文内容 步骤 1:选择和预配置操作系统 步骤 2:安装 SharePoint 20 ...

  7. 设计模式之简单工厂模式(Simple Factory)

    原文地址:http://www.cnblogs.com/BeyondAnyTime/archive/2012/07/06/2579100.html 今天呢,要学习的设计模式是“简单工厂模式”,这是一个 ...

  8. IOS 播放视频(MPMoviePlayerController、MPMoviePlayerViewController)

    ● iOS提供了叫 做MPMoviePlayerController.MPMoviePlayerViewController的两个 类,可以用来轻松播放视频 ➢ YouTobe就是用MPMoviePl ...

  9. Android(java)学习笔记44:Map集合的遍历之键值对对象找键和值

    1. Map集合的遍历之 键值对对象找 键和值: package cn.itcast_01; import java.util.HashMap; import java.util.Map; impor ...

  10. UVA1184 Air Raid

    嘟嘟嘟 最小路径覆盖板子题. 建二分图,然后跑Dinic(因为我不会匈牙利),然后ans = n - maxflow(). 主要是发一下用链前存图的写法.(好像比vector短一点) #include ...