-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequired_Substring.cpp
More file actions
62 lines (54 loc) · 1.42 KB
/
Required_Substring.cpp
File metadata and controls
62 lines (54 loc) · 1.42 KB
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
#include <bits/stdc++.h>
using namespace std;
// First you make it work, then you can always make it beautiful
#define int long long
const int mod = 1e9 + 7;
int modpowr(int a, int b) {
int res = 1;
while(b) {
if(b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
// dp[i][j] = number of ways to build a string of length n
// given that we are currently at position i
// and have matched j characters of the pattern s as a suffix.
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
int m = s.size();
vector<int> lps(m);
for(int i=1;i<m;i++) {
int prev = lps[i-1];
while(prev > 0 && s[i] != s[prev]) {
prev = lps[prev - 1];
}
lps[i] = prev + (s[i] == s[prev]);
}
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
for(int i=0;i<=n;i++) {
dp[i][m] = modpowr(26, n - i);
}
for(int i=n-1;i>=0;i--) {
for(int j=0;j<m;j++) {
for(int k=0;k<26;k++) {
int idx = j;
while(idx > 0 && k != s[idx] - 'A') {
idx = lps[idx - 1];
}
if(k == s[idx] - 'A') idx++;
(dp[i][j] += dp[i+1][idx]) %= mod;
}
}
}
int ans = dp[0][0];
cout << ans << "\n";
return 0;
}