You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.1 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, m, mod;
const int N = 1e6 + 10;
int ne[N]; // kmp的ne数组针对模式串s
char s[N]; // 模式串s
int f[N][22]; // dp数组
//原始版本就是设计密码那道题的代码可以过4个点剩余6个点TLE
int main() {
cin >> n >> m >> mod;
cin >> s + 1;
// kmp
for (int i = 2, j = 0; i <= m; i++) {
while (j && s[j + 1] != s[i]) j = ne[j];
if (s[j + 1] == s[i]) j++;
ne[i] = j;
}
//普通dp
f[0][0] = 1;
for (int k = 0; k < n; k++)
for (int i = 0; i < m; i++)
for (char c = '0'; c <= '9'; c++) {
int j = i;
while (j && s[j + 1] != c) j = ne[j];
if (s[j + 1] == c) j++;
//在kmp过程中进行判断,不能命中m个长度需要避让
if (j < m) f[k + 1][j] = (f[k + 1][j] + f[k][i]) % mod;
}
int res = 0;
for (int i = 0; i < m; i++) res = (res + f[n][i]) % mod;
printf("%d\n", res);
return 0;
}