BZOJ 2580: [Usaco2012 Jan]Video Game

2580: [Usaco2012 Jan]Video Game

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 142  Solved: 96
[Submit][Status][Discuss]

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

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

分析:

我们定义状态f[i][j]为第i个字符匹配到第j个节点的最多匹配个数,然后求出AC自动机转移即可...

我们建出Trie图,然后转移方程就是f[i][nxt[j][k]]=max(f[i][nxt[j][k]],f[i-1][j]+num[nxt[j][k]])...

num[i]代表匹配到i号节点可以产生的匹配串个数,包括以i为结尾的串和fail指针产生的串...

代码:

#include
#include
#include
#include
//by NeighThorn
using namespace std;

const int maxn=15+5,maxm=1000+5;

int n,m,tot,head=0,tail=0,q[maxm],f[maxm][maxn*maxn];

char s[maxn];

struct trie{
	int cnt,fail,nxt[3];
}tr[maxm];

inline void insert(char *s){
	int len=strlen(s),p=0;
	for(int i=0;i

  


By NeighThorn

转载于:https://www.cnblogs.com/neighthorn/p/6404985.html

你可能感兴趣的:(BZOJ 2580: [Usaco2012 Jan]Video Game)