题目描述
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?
给你个模式串(每个长度≤15,1≤N≤20),串中只含有“ABC”三种字母。求一长度为K(1≤K≤1000)的字符串,使得匹配数最大(重复匹配计多次),输出最大值。
输入
* 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.
输出
* Line 1: A single integer, the maximum number of points Bessie can obtain.
样例输入
3 7 ABA CB ABACB
样例输出
4
提示
The optimal sequence of buttons in this case is ABACBCB, which gives 4 points--1 from ABA, 1 from ABACB, and 2 from CB.
dp思想非常明显吧。看到多模式串匹配,AC自动机非常明显吧。结合一下,就做好了!
dp[i][j]表示长度为i时末节点编号为j的最大匹配数。
状态转移方程也比较好想。f[i+1][AC[j].son[t]]=max(f[i+1][AC[j].son[t]],f[i][j]+AC[AC[j].son[t]].val);
ans=max(dp[k][j])。
代码中的AC[i].val表示加入这个节点会增加多少个匹配数。
Code:
#include<bits/stdc++.h>
#define N 1005
using namespace std;
struct node
{
int fail,son[5],val;
}AC[N];
int n,k,num,ans,Q[N],f[N][N];
char s[N];
void insert(char *s)
{
int len=strlen(s);
int now=0;
for(int i=0;i<len;i++)
{
if(!AC[now].son[s[i]-'A'])AC[now].son[s[i]-'A']=++num;
now=AC[now].son[s[i]-'A'];
}
AC[now].val++;
}
void getfail()
{
int head=1,tail=0;
AC[0].fail=0;
for(int i=0;i<3;i++)
{
int u=AC[0].son[i];
if(u!=0)
{
Q[++tail]=u;
AC[u].fail=0;
}
}
while(head<=tail)
{
int now=Q[head];
AC[now].val+=AC[AC[now].fail].val;
for(int i=0;i<3;i++)
{
int u=AC[now].son[i];
if(u!=0)
{
AC[u].fail=AC[AC[now].fail].son[i];
Q[++tail]=u;
}else AC[now].son[i]=AC[AC[now].fail].son[i];
}
head++;
}
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
insert(s);
}
getfail();
memset(f,-0x3f,sizeof(f));
f[0][0]=0;
ans=0;
for(int i=0;i<k;i++)
for(int j=0;j<=num;j++)
for(int t=0;t<3;t++)
f[i+1][AC[j].son[t]]=max(f[i+1][AC[j].son[t]],f[i][j]+AC[AC[j].son[t]].val);
for(int i=0;i<=num;i++)
ans=max(ans,f[k][i]);
printf("%d\n",ans);
return 0;
}