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的更多相关文章

  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. BZOJ2580: [Usaco2012 Jan]Video Game(AC自动机)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 159  Solved: 110[Submit][Status][Discuss] Descriptio ...

  4. 【AC自动机+DP】USACO2012 JAN GOLD_Video Game Combos

    [题目大意] 给你个模式串(每个长度≤15,1≤N≤20),串中只含有三种字母.求一长度为K(1≤K≤1000)的字符串,使得匹配数最大(重复匹配计多次),输出最大值. [解题思路] W老师给的题,然 ...

  5. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  6. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  7. DeepCoder: A Deep Neural Network Based Video Compression

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! Abstract: 在深度学习的最新进展的启发下,我们提出了一种基于卷积神经网络(CNN)的视频压缩框架DeepCoder.我们分别对预测 ...

  8. 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 ...

  9. video.js

    1.github地址 2.常用API: class : video-js: video-js应用视频所需的风格.js功能,比如全屏和字幕. vjs-default-skin: vjs-default- ...

随机推荐

  1. appium-java-api

    AppiumDriver getAppStrings() 默认系统语言对应的Strings.xml文件内的数据. driver.getAppStrings(String language) 查找某一个 ...

  2. Codeforces 8VC Venture Cup 2016 - Elimination Round F. Group Projects 差分DP*****

    F. Group Projects   There are n students in a class working on group projects. The students will div ...

  3. luogu3373 【模板】线段树2

    题目大意: 已知一个数列,你需要进行下面三种操作:1.将某区间每一个数乘上x2.将某区间每一个数加上x3.求出某区间每一个数的和 本线段树的标记是个二元组:add和mul,其代表将一个线段中的每一个点 ...

  4. 使用std::mutex取代QMutex

    为了保证对某个资源的操作是原子性的(对资源读写时,只有当前的操作结束,才允许另外线程对其操作,这里有个理解误区,资源操作原子性不是说,当前某个线程获得了某个资源使用权,然后线程执行时间完毕,要切换线程 ...

  5. 谈谈Paxos一致性算法和一致性这个名词

    转自:http://www.cnblogs.com/esingchan/p/3917718.html 维基的简介:Paxos算法是莱斯利·兰伯特(Leslie Lamport,就是 LaTeX 中的& ...

  6. Spark高级

    Spark源码分析: https://yq.aliyun.com/articles/28400?utm_campaign=wenzhang&utm_medium=article&utm ...

  7. js中return的作用

    1.终止函数的继续运行. 当遇到if…… else是.若出现return,就会出现终止运行,不会继续做出判断 <html> <head> <title>return ...

  8. Linux下配置rsync服务器

    一.简介 rsync是一个远程数据同步工具,可以快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都 ...

  9. SocketErrorCode:10022

    在编写.net的网络服务器时,我使用了裸socket来实现.在windows上,或者在linux上通过.net core来跑时都没有什么问题,但是通过mono运行调用socket.Bind()时却总是 ...

  10. Opencv:10个步骤检测出图片中条形码

    1. 原图像大小调整,提高运算效率 2. 转化为灰度图 3. 高斯平滑滤波 4.求得水平和垂直方向灰度图像的梯度差,使用Sobel算子 5.均值滤波,消除高频噪声 6.二值化 7.闭运算,填充条形码间 ...