[Usaco2012 Jan]Video Game
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
首先对所有的得分串建立AC自动机,然后考虑dp,设\(f[i][j]\)表示当前长度为\(i\),匹配到AC自动机上节点\(j\)的得分,转移直接枚举\(j\)之后连的字符即可
然后建fail指针的时候把终止标识符累加起来,这样之后就可以\(O(1)\)询问了
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1;char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=3e2,M=1e3;
struct S1{
int trie[N+10][3],fail[N+10],End[N+10],tot,root;
void insert(char *s){
int len=strlen(s),p=root;
for (int i=0;i<len;i++){
if (!trie[p][s[i]-'A']) trie[p][s[i]-'A']=++tot;
p=trie[p][s[i]-'A'];
}
End[p]++;
}
void make_fail(){
static int h[N+10];
int head=1,tail=0;
for (int i=0;i<3;i++) if (trie[root][i]) h[++tail]=trie[root][i];
for (;head<=tail;head++){
int Now=h[head];
End[Now]+=End[fail[Now]];//累计标识符
for (int i=0;i<3;i++){
if (trie[Now][i]){
int son=trie[Now][i];
fail[son]=trie[fail[Now]][i];
h[++tail]=son;
}else trie[Now][i]=trie[fail[Now]][i];
}
}
}
}AC;//Aho-Corasick automation
int f[M+10][N+10];
int main(){
int n=read(),K=read();
for (int i=1;i<=n;i++){
static char s[20];
scanf("%s",s);
AC.insert(s);
}
AC.make_fail();
memset(f,255,sizeof(f));
f[0][0]=0;
for (int i=0;i<K;i++){
for (int j=0;j<=AC.tot;j++){
if (!~f[i][j]) continue;
for (int k=0;k<3;k++){
int tmp=AC.trie[j][k];
f[i+1][tmp]=max(f[i+1][tmp],f[i][j]+AC.End[tmp]);
}
}
}
int Ans=0;
for (int i=0;i<=AC.tot;i++) Ans=max(Ans,f[K][i]);
printf("%d\n",Ans);
return 0;
}
[Usaco2012 Jan]Video Game的更多相关文章
- 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 ...
- BZOJ2580: [Usaco2012 Jan]Video Game(AC自动机)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 159 Solved: 110[Submit][Status][Discuss] Descriptio ...
- 【AC自动机+DP】USACO2012 JAN GOLD_Video Game Combos
[题目大意] 给你个模式串(每个长度≤15,1≤N≤20),串中只含有三种字母.求一长度为K(1≤K≤1000)的字符串,使得匹配数最大(重复匹配计多次),输出最大值. [解题思路] W老师给的题,然 ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- DeepCoder: A Deep Neural Network Based Video Compression
郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Abstract: 在深度学习的最新进展的启发下,我们提出了一种基于卷积神经网络(CNN)的视频压缩框架DeepCoder.我们分别对预测 ...
- NC24017 [USACO 2016 Jan S]Angry Cows
NC24017 [USACO 2016 Jan S]Angry Cows 题目 题目描述 Bessie the cow has designed what she thinks will be the ...
- video.js
1.github地址 2.常用API: class : video-js: video-js应用视频所需的风格.js功能,比如全屏和字幕. vjs-default-skin: vjs-default- ...
随机推荐
- HashMap底层原理以及与ConCurrentHashMap区别
HashMap基于hashing原理,我们通过put()和get()方法储存和获取对象.当我们将键值对传递给put()方法时,它调用键对象的hashCode()方法来计算hashcode,让后找到bu ...
- php利用cookie防止重复提交解决办法
原理:如果数据通过了上边的两次验证,说明数据是合法有效的数据,这时候我们把提交的数据串接为一个字符串,并用MD5加密后得到一个MD5的值. 接着我们把这个值通过Cookie放进客户端,当用户下一次提交 ...
- Java多态性详解 (父类引用子类对象)
面向对象编程有三个特征,即封装.继承和多态. 封装隐藏了类的内部实现机制,从而可以在不影响使用者的前提下改变类的内部结构,同时保护了数据. 继承是为了重用父类代码,同时为实现多态性作准备.那么什么是多 ...
- 开源企业IM免费企业即时通讯ENTBOOST V2014.177版本号正式公布
版权声明:本文为博主原创文章,欢迎转载,转载请尽量保持原文章完整,谢谢! https://blog.csdn.net/yanghz/article/details/30529469 ENTBOOST, ...
- 附录: mysql show processlist中的State的意义
附录: mysql show processlist中的State的意义 Checking table 正在检查数据表(这是自动的). Closing tables 正在将表中修改的数据刷新到磁盘中, ...
- POJ 3279 Dungeon Master
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21242 Accepted: 8265 D ...
- antV G6流程图在Vue中的使用
最近我司项目中需要加入流程图制作功能,于是乎百度各种找可视化绘制拓扑图的轮子,大部分都是国外的,看文档太吃力,不过好在最终让我发现了AntV G6流程图图表库,最新版为2.0,不过编辑器在2.0版本还 ...
- 细说align 的作用及用法
.align 就是用来对齐的,究竟怎么对齐,有啥情况?下面分析一下 基本情况讲解 (一) $vim align1.s 在新建的文件编辑以下代码: 1 2 3 4 5 6 _start: b reset ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- codeforces 690D1 D1. The Wall (easy)(dfs)
题目链接: D1. The Wall (easy) time limit per test 0.5 seconds memory limit per test 256 megabytes input ...