BZOJ 2580: [Usaco2012 Jan]Video Game
2580: [Usaco2012 Jan]Video Game
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 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<algorithm> #include<iostream> #include<cstring> #include<cstdio> //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<len;i++){ if(!tr[p].nxt[s[i]-'A']) tr[p].nxt[s[i]-'A']=++tot; p=tr[p].nxt[s[i]-'A'];tr[p].fail=-1; } tr[p].cnt++; } inline void buildACM(void){ head=0,tail=0;q[0]=0; while(head<=tail){ int id=q[head++],p=-1; for(int i=0;i<3;i++){ if(tr[id].nxt[i]){ if(id){ p=tr[id].fail; while(p!=-1){ if(tr[p].nxt[i]){ tr[tr[id].nxt[i]].fail=tr[p].nxt[i]; break; } p=tr[p].fail; } if(p==-1) tr[tr[id].nxt[i]].fail=0; } else tr[tr[id].nxt[i]].fail=0; tr[tr[id].nxt[i]].cnt+=tr[tr[tr[id].nxt[i]].fail].cnt; q[++tail]=tr[id].nxt[i]; } else if(id) tr[id].nxt[i]=tr[tr[id].fail].nxt[i]; } } } inline void DP(void){ memset(f,-1,sizeof(f)); f[0][0]=0; for(int i=1;i<=m;i++) for(int j=0;j<=tot;j++) if(f[i-1][j]!=-1) for(int k=0;k<3;k++) if(tr[j].nxt[k]) f[i][tr[j].nxt[k]]=max(f[i][tr[j].nxt[k]],f[i-1][j]+tr[tr[j].nxt[k]].cnt); } signed main(void){ scanf("%d%d",&n,&m);tr[0].fail=-1; for(int i=1;i<=n;i++) scanf("%s",s),insert(s); buildACM();DP();int ans=0; for(int i=0;i<=tot;i++) ans=max(ans,f[m][i]); printf("%d\n",ans); return 0; }
By NeighThorn