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 ...
随机推荐
- Format - DateTime
1. Long Date/Short Date/Long Time/Short Time,可以在系统的“Region and Language”中找到相应设置: 2. ISO Format/Local ...
- Android 环信聊天头像昵称显示解决方案
从消息扩展中获取昵称和头像 昵称和头像的获取:把用户基本的昵称和头像的URL放到消息的扩展中,通过消息传递给接收方,当收到一条消息时,则能通过消息的扩展得到发送者的昵称和头像URL,然后保存到本地数据 ...
- jscode属性排序
根据data中的value 对geCoorMap 进行排序,暂定降序排序. var data = [{name:"name1",value:29},{name:"name ...
- nagios外部命令接口
http://nagios.manubulon.com/traduction/docs14en/extcommands.html https://old.nagios.org/developerinf ...
- 一.配置简单的嵌入式tomcat和jetty
我们写了一个web应用,打成war包后,就需要找一个server来部署.对于我们的实际应用,我们基本没必要自己再写一个嵌入式的server.接下来两篇文章只是以钻研的心态来学习一下嵌入式tomcat和 ...
- .NET部分知识点整理
.Net学习 Visual Studio2018 企业版:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF 专业版:KBJFW-NXHK6-W4WJM-CRMQB-G3CDH 开发工具常用V ...
- laravel + haproxy + https 后生成分页 url 非 https 解决办法
更合适的解决办法:在 AppServiceProvider boot 方法中使用 \URL::forceScheme('https'); 即可. 背景 近日对所有的客户都上线了 https ,本来在 ...
- Linux 配置 ss
Linux 配置 Shadowsocks 标签(空格分隔): ss VPS 1.首先安装 sudo pip install shadowsocks 2.然后在指定位置新建shadowsocks.jso ...
- Oracle 通过undo块查看事务信息
数据库版本:Oracle 11.2.0.3 RAC 实验目的:通过undo块查看Oracle事务信息 实验细节:1 开始一个事务SQL> select * from t1; ID NAME--- ...
- zt dup() 和 dup2()
dup() 和 dup2() 2011-10-07 11:06:31| 分类: Linux学习心得 |字号 订阅 dup和dup2都可用来复制一个现存的文件描述符,使两个文件描述符指向同一个fi ...