AcWing 1052. 设计密码
题目链接:
https://www.acwing.com/problem/content/description/1054/
题解:
kmp + dp
f[i][j]: 在第i个字母已经选取的情况下,状态为j的所有方案的数量,这里的j表示kmp中已经匹配的模板串的数量
状态机的状态解释很难想到,建议背过
AC代码:
import java.util.*;
public class Main {
static int N = 55;
static int[][] f = new int[N][N];
static int[] ne = new int[N];
static int MOD = (int) 1e9 + 7;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
String p = " " + sc.nextLine();
int m = p.length() - 1;
for (int i = 2, j = 0; i <= m; i ++) {
while (j != 0 && p.charAt(i) != p.charAt(j + 1)) j = ne[j];
if (p.charAt(i) == p.charAt(j + 1)) j ++;
ne[i] = j;
}
f[0][0] = 1;
for (int i = 1; i <= n; i ++) {
for (int j = 0; j < m; j ++) {
for (char k = 'a'; k <= 'z'; k ++) {
int u = j;
while (u != 0 && k != p.charAt(u + 1)) u = ne[u];
if (k == p.charAt(u + 1)) u ++;
if (u < m) f[i][u] = (f[i][u] + f[i - 1][j]) % MOD;
}
}
}
int res = 0;
for (int i = 0; i < m; i ++) res = (res + f[n][i]) % MOD;
System.out.println(res);
}
}