Files
python/TangDou/AcWing_TiGao/T5/JuZhengChengFa/1305_BF_DP.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

39 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}