-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathRequired Substring.cpp
65 lines (56 loc) · 1.37 KB
/
Required Substring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 1005, maxM = 505;
const ll MOD = 1e9+7;
int N, M, best[26][maxM];
char S[maxN];
ll ans, dp[maxM][maxN];
ll pow26(ll b){
ll a = 26;
ll res = 1;
while(b){
if(b&1) res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
bool good(vector<char> pre, int k){
int SZ = (int) pre.size();
for(int i = 0; i < SZ-k; i++)
if(pre[i+k] != S[i])
return false;
return true;
}
int main(){
scanf("%d %s", &N, S);
M = (int) strlen(S);
if(M > N){
printf("0\n");
return 0;
}
for(int r = 0; r < M; r++){
for(int c = 0; c < 26; c++){
vector<char> pre;
for(int i = 0; i < r; i++)
pre.push_back(S[i]);
pre.push_back((char) (c+'A'));
for(int k = 0; k < r+1; k++){
if(good(pre, k)){
best[c][r] = r-k+1;
break;
}
}
}
}
dp[0][0] = 1;
for(int i = 1; i <= N; i++)
for(int j = 0; j < M; j++)
for(int c = 0; c < 26; c++)
dp[best[c][j]][i] = (dp[best[c][j]][i] + dp[j][i-1]) % MOD;
ans = pow26(N);
for(int i = 0; i < M; i++)
ans = (ans - dp[i][N] + MOD) % MOD;
printf("%lld\n", ans);
}