Submit: 159 Solved: 110
[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
AC自动机应该不难看出来
按照套路dp,设$f[i][j]$表示枚举到第$i$个位置,现在位于自动机上的第$i$位。
转移的时候枚举下一个位置就好
有两个需要注意的地方
1.Trie树在我们建fail树的时候实际被我们改造成了Trie图,因此每个节点是可能被多次枚举到的,需要对自身取$max$
2.有些深度大于当前枚举长度的点是不可能走到的,因此开始时应把每个点的权值设为$-INF$(root除外)
// luogu-judger-enable-o2 // luogu-judger-enable-o2 #include<cstdio> #include<cstring> #include<queue> using namespace std; const int MAXN = 301, B = 3; int T, K; char s[17]; int ch[MAXN][4], f[1001][MAXN], fail[MAXN], val[MAXN], tot = 0, root = 0; void insert(char *s) { int N = strlen(s + 1); int now = root; for(int i = 1; i <= N; i++) { int x = s[i] - 'A'; if(!ch[now][x]) ch[now][x] = ++tot; now = ch[now][x]; } val[now]++; } void GetFail() { queue<int> q; for(int i = 0; i < B; i++) if(ch[root][i]) q.push(ch[root][i]); while(!q.empty()) { int p = q.front(); q.pop(); for(int i = 0; i < B; i++) { if(!ch[p][i]) ch[p][i] = ch[fail[p]][i]; else fail[ch[p][i]] = ch[fail[p]][i], q.push(ch[p][i]); } val[p] += val[fail[p]]; } } int Dp() { memset(f, -0x3f, sizeof(f)); for(int i = 0; i <= K; i++) f[i][0] = 0;//óDD?×′ì?ê?2??é?ü′?μ?μ?£?òò′?Dèòa?e2??üD? int ans = 0; for(int i = 1; i <= K; i++) for(int j = 0; j <= tot; j++) for(int k = 0; k < B; k++) { int son = ch[j][k]; if(son) { f[i][son] = max(f[i][son], f[i - 1][j] + val[son]); printf("%d %d %d %d %d ", i, j, k, son, f[i][son]); } } for(int i = 0; i <= tot; i++) ans = max(ans, f[K][i]); return ans; } int main() { #ifdef WIN32 freopen("a.in", "r", stdin); #endif scanf("%d %d", &T, &K); for(int i = 1; i <= T; i++) scanf("%s", s + 1), insert(s); GetFail(); printf("%d", Dp()); return 0; }