BZOJ2580: [Usaco2012 Jan]Video Game(AC自动机)
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 159 Solved: 110
[Submit][Status][Discuss]
Description
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
Sample Output
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自动机)的更多相关文章
- 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 ...
- BZOJ 2580: [Usaco2012 Jan]Video Game
2580: [Usaco2012 Jan]Video Game Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 142 Solved: 96[Subm ...
- [Usaco2012 Jan]Video Game
Description Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the ...
- 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 ...
- 洛谷P3041 视频游戏的连击Video Game Combos [USACO12JAN] AC自动机+dp
正解:AC自动机+dp 解题报告: 传送门! 算是个比较套路的AC自动机+dp趴,,, 显然就普普通通地设状态,普普通通地转移,大概就f[i][j]:长度为i匹配到j 唯一注意的是,要加上所有子串的贡 ...
- [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机+DP)
Description 贝西正在打格斗游戏.游戏里只有三个按键,分别是“A”.“B”和“C”.游戏中有 N 种连击 模式,第 i 种连击模式以字符串 Si 表示,只要贝西的按键中出现了这个字符串,就算 ...
- [USACO12Jan][luogu3041] Video Game Combos [AC自动机+dp]
题面 传送门 思路 首先,有一个非常显然的思路就是dp: 设$dp[i][j]$表示前i个字符,最后一个为j 然后发现这个东西有后效性 改!设$dp[i][j]$代表前i个字符,最后15个的状态为j( ...
- 【洛谷 P3041】 [USACO12JAN]视频游戏的连击Video Game Combos(AC自动机,dp)
题目链接 手写一下AC自动机(我可没说我之前不是手写的) Trie上dp,每个点的贡献加上所有是他后缀的串的贡献,也就是这个点到根的fail链的和. #include <cstdio> # ...
- hdu-Danganronpa(AC自动机)
Problem Description Danganronpa is a video game franchise created and developed by Spike Chunsoft, t ...
随机推荐
- Mac下抓包工具Charles4.0下载及使用
Charles是一款运行在你自己计算机上的web代理工具,可以有效地获取http通信信息,主要用于网页的开发和调试等. 安装 以下为来自史蒂芬周的博客的Charles 4.0 Mac破解版 下载地址: ...
- PHP 文件锁和常用文件函数
文件锁 bool flock ( int handle, int operation [, int &wouldblock] );flock() 操作的 handle 必须是一个已经打开的文件 ...
- Spring注解之Controller中获取请求参数及验证使用
1.处理request的uri部分的参数:@PathVariable. 2.处理request header部分的参数:@RequestHeader,@CookieValue@RequestHeade ...
- mysql 语句学习一 关于系统信息的查询
首先说一下,SQL语句是不区分大小写的. 1.SELECT VERSION(); -- 查询当前版本号 2.SELECT CURRENT_TIME(); -- 查询当前时间 3.S ...
- CSS3中的Flexbox弹性布局(二)
flexbox详解 flexbox的出现是为了解决复杂的web布局,因为这种布局方式很灵活.容器的子元素可以任意方向进行排列.此属性目前处于非正式标准. flex布局模型不同于块和内联模型布局,块和内 ...
- 多线程(一)~基础介绍,简单demo实现
前言: 现在CPU都是多核的,可以同时处理多个进程,比如我笔记本的CPU是i3-370,它就是双核四线程的.那么这个核和线程都是什么呢? 核是针对硬件而言的,即核心,代表的 ...
- SqlServer存储过程示例
/* 步骤1 删除本地及海关单证待分派表.报关单表中的数据 delete from W_DOCUMENTS; delete from W_DOCUMENTS_TEST; delete from W_D ...
- SpringMvc-自定义视图
1.创建视图: 注意:创建视图的时候需要实现View接口的俩个方法 package com.atguigu.springmvc.views; import java.util.Date; import ...
- 1.LVS理论基础
查看我的有道云笔记: http://note.youdao.com/noteshare?id=68e0ca45757943e482ba390d4d49369f&sub=4A2A593CDE2D ...
- March 17 2017 Week 11 Friday
Simplicity is the ultimate sophistication. 简约才是精巧到了极致. Recently I have spent a great number of time ...