Time Limit: 10 Sec  Memory Limit: 128 MB
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 = , B = ;
int T, K;
char s[];
int ch[MAXN][], f[][MAXN], fail[MAXN], val[MAXN], tot = , root = ;
void insert(char *s) {
int N = strlen(s + );
int now = root;
for(int i = ; 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 = ; i < B; i++) if(ch[root][i]) q.push(ch[root][i]);
while(!q.empty()) {
int p = q.front(); q.pop();
for(int i = ; 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 = ; i <= K; i++) f[i][] = ;//óDD?×′ì?ê?2??é?ü′?μ?μ?£?òò′?Dèòa?e2??üD?
int ans = ;
for(int i = ; i <= K; i++)
for(int j = ; j <= tot; j++)
for(int k = ; k < B; k++) {
int son = ch[j][k];
if(son) {
f[i][son] = max(f[i][son], f[i - ][j] + val[son]);
printf("%d %d %d %d %d\n", i, j, k, son, f[i][son]);
} }
for(int i = ; 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 = ; i <= T; i++)
scanf("%s", s + ), insert(s);
GetFail();
printf("%d", Dp());
return ;
}

最新文章

  1. 使用hibernate可以优化的地方
  2. BZOJ4013 : [HNOI2015]实验比较
  3. 【php】对PHPExcel一些简单的理解
  4. ArrowDrawable
  5. PAT_1008 数组元素循环右移问题
  6. max key length is 1000 bytes
  7. [转载] Linux的Top命令解析
  8. mkdir npm
  9. 通过官方API结合源码,如何分析程序流程
  10. jquery源码&#39;jQuery.fn.init.prototype&#39;
  11. Python 多线程、多进程 (一)之 源码执行流程、GIL
  12. Objective-C MacOS以管理员权限执行程序
  13. 一起做RGB-D SLAM (2)
  14. English trip -- VC(情景课) 7 B Clothing 服装
  15. C++中的结构体的认识
  16. ASP.NET 关于GridView 表格重复列合并
  17. C# 窗体文件下的 MainForm.cs,MainForm.Designer.cs,MainForm.resx,是什么,干什么
  18. ubuntu桌面安装常用软件&amp;及常见问题
  19. JAVA多线程提高十:同步工具CyclicBarrier与CountDownLatch
  20. fedora 修改home下的中文目录为英文目录

热门文章

  1. oracle中时间戳转为Date类型的数据
  2. POI Excel解析
  3. Android - Rxjava 使用和原理
  4. 解决python3缺少zlib的问题
  5. Linux 网络(连接)相关参数作用
  6. ECharts动态数据加载
  7. Angular js 过滤器 笔记(转自菜鸟教程)
  8. 生成centos7 安装脚本
  9. ubuntu 火狐浏览器中常规方法安装flashplayer的问题
  10. March 11 2017 Week 10 Saturday