luogu#P3041. [USACO12JAN] Video Game G

    ID: 7071 Type: RemoteJudge 1000ms 125MiB Tried: 2 Accepted: 2 Difficulty: 8 Uploaded By: Tags>AC 自动机动态规划dpUSACO2012O2优化

[USACO12JAN] Video Game G

Problem Description

Bessie is playing a game with only three skill keys A, B, C available, and these keys can form nn specific combos. The ii-th combo is represented by a string sis_i.

Bessie will input a string tt of length kk, and each time a combo appears in tt, Bessie scores one point. An occurrence of sis_i in tt means that sis_i is a contiguous substring of tt starting at some position. If sis_i is a contiguous substring starting at multiple positions in tt, it is counted multiple times.

If Bessie inputs exactly kk characters, what is the maximum score she can get?

Input Format

The first line contains two integers, the number of combos nn and the number of characters Bessie inputs kk.

Then nn lines follow, each containing a string sis_i representing a combo.

Output Format

Output a single integer representing the answer.

3 7 
ABA 
CB 
ABACB 

4 

Hint

Sample 1 Explanation.

If Bessie inputs ABACBCB, then ABA appears once, ABACB appears once, and CB appears twice, for a total of 44 points. This can be proven optimal.

Constraints

For all testdata, it is guaranteed that:

  • 1n201 \le n \le 20, 1k1031 \le k \le 10^3.
  • 1si151 \le |s_i| \le 15. Here si|s_i| denotes the length of string sis_i.
  • All strings contain only the uppercase letters A, B, C.

Translated by ChatGPT 5