AC自动机建立fail树后树上DP

# include <stdio.h>
# include <stdlib.h>
# include <iostream>
# include <string.h>
# include <algorithm>
# include <queue>
# define IL inline
# define RG register
# define ll long long
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std; IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
return x * z;
} const int MAXN(1010);
struct AC{
int fail, ch[3], num;
} tree[MAXN];
int n, cnt, k, f[MAXN][MAXN], ans;
char t[MAXN];
queue <int> Q; IL void Insert(){
RG int len = strlen(t), x = 0;
for(RG int i = 0; i < len; i++){
if(!tree[x].ch[t[i] - 'A']) tree[x].ch[t[i] - 'A'] = ++cnt;
x = tree[x].ch[t[i] - 'A'];
}
tree[x].num++;
} IL void Bfs(){
while(!Q.empty()) Q.pop();
for(RG int i = 0; i < 3; i++)
if(tree[0].ch[i]) Q.push(tree[0].ch[i]);
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
for(RG int i = 0; i < 3; i++)
if(tree[u].ch[i]){
tree[tree[u].ch[i]].fail = tree[tree[u].fail].ch[i];
Q.push(tree[u].ch[i]);
}
else tree[u].ch[i] = tree[tree[u].fail].ch[i];
tree[u].num += tree[tree[u].fail].num;
}
} int main(){
n = Read(); k = Read();
for(RG int i = 1; i <= n; i++){
scanf(" %s", t);
Insert();
}
Bfs();
Fill(f, -127); f[0][0] = 0;
for(RG int K = 1; K <= k; K++){
for(RG int i = 0; i <= cnt; i++)
for(RG int j = 0; j < 3; j++)
f[K][tree[i].ch[j]]=max(f[K][tree[i].ch[j]],f[K - 1][i] + tree[tree[i].ch[j]].num);
}
for(RG int i = 0; i <= cnt; i++)
ans = max(ans, f[k][i]);
printf("%d\n", ans);
return 0;
}

[USACO12JAN]Video Game Combos的更多相关文章

  1. 洛谷 P3041 [USACO12JAN] Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

  2. 【USACO12JAN】视频游戏的连击Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

  3. [Luogu3041][USACO12JAN]视频游戏的连击Video Game Combos

    题面 sol 设\(f_{i,j}\)表示填了前\(i\)个字母,在\(AC\)自动机上跑到了节点\(j\)的最大得分.因为匹配需要暴跳\(fail\)所以预先把\(fail\)指针上面的匹配数传下来 ...

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

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

  5. P3041 [USACO12JAN]视频游戏的连击Video Game Combos

    思路 简单的AC自动机上dp,暴力跳fail向子节点直接转移即可 代码 #include <cstdio> #include <algorithm> #include < ...

  6. 【题解】[USACO12JAN]视频游戏的连击Video Game Combos

    好久没有写博客了,好惭愧啊……虽然这是一道弱题但还是写一下吧. 这道题目的思路应该说是很容易形成:字符串+最大值?自然联想到学过的AC自动机与DP.对于给定的字符串建立出AC自动机,dp状态dp[i] ...

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

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

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

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

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

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

随机推荐

  1. ajax常用操作

    load的方法的使用(现在已不常用) <!doctype html><html lang="en"><head> <meta charse ...

  2. 一个客户端一键安装环境和服务的shell脚本

    #!/bin/bash basepath=$(cd `dirname $0`; pwd)SHELL_DIR="${basepath}/shell"PACKAGE_DIR=" ...

  3. ipset批量配置iptables

    简介: ipset是iptables的扩展,允许你创建匹配整个地址sets(地址集合)的规则.而不像普通的iptables链是线性的存储和过滤,ip集合存储在带索引的数据结构中,这种集合比较大也可以进 ...

  4. window 下生成NodeJs(v8.9.3) 的 VS2015 解决方案node.sln

    window 下生成NodeJs(v8.9.3) 的 VS2015 解决方案node.sln 使用步骤 也可以参照 github: https://github.com/nodejs/node/blo ...

  5. bzoj2151 种树 双向链表+堆

    2151: 种树 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1151  Solved: 613[Submit][Status][Discuss] ...

  6. Linux 格式化和挂载数据盘

    如果您已经为 ECS 实例配了数据盘,您需要先格式化数据盘并挂载文件系统后才能正常使用数据盘. 注意: 磁盘分区和格式化是高风险行为,请慎重操作.本文档描述如何处理一个新买的数据盘,如果您的数据盘上有 ...

  7. 关于springMVC中component-scan的问题以及springmvc.xml整理

    关于springMVC中component-scan的问题以及springmvc.xml整理 一.component-scan问题和解决办法         最近在学习使用springMVC+myba ...

  8. 3.2 while 循环

    Python 编程中 while 语句用于循环执行程序,即在条件满足的情况下,循环执行某段代码.所以就需要在循环的代码块中设计一种使代码块循环执行一定次数后是while语句的条件不满足,从而中止whi ...

  9. C++ 中vector的使用方法(转)

    原地址:http://blog.csdn.net/duan19920101/article/details/50617190/ 在c++中,vector是一个十分有用的容器. 作用:它能够像容器一样存 ...

  10. 文件无法复制的原因-IT33

    Win7系统复制数据至其他硬盘或者是移动存储设备是,有时会发生无法复制文件过大的情况.这里先大致介绍一下硬盘文件系统分为NFTS格式和FAT32格式这两种,其中FAT32仅支持单次移动4G以下容量的数 ...