|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <cassert> |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +typedef vector<int> vi; |
| 8 | +typedef pair<int,int> ii; |
| 9 | +typedef pair<int,ii> iii; |
| 10 | +typedef vector<ii> vii; |
| 11 | +typedef vector<vii> vvii; |
| 12 | + |
| 13 | +string KMP_str; // The string to search in |
| 14 | +string KMP_pat; // The pattern to search |
| 15 | +vi lps; |
| 16 | +vvii v; |
| 17 | +typedef long long ll; |
| 18 | +//ll mulmodn(ll a, ll b, ll n){ |
| 19 | +// ll res = 0; |
| 20 | +// while(b){ |
| 21 | +// if(b & 1) res= (res+a) %n; |
| 22 | +// a = (a*2)%n; |
| 23 | +// b >>= 1; |
| 24 | +// } |
| 25 | +// return res; |
| 26 | +//} |
| 27 | + |
| 28 | +// KMP Init |
| 29 | +void KMP_init(){ |
| 30 | + int m = KMP_pat.length(); |
| 31 | + lps.resize(m+1,0); // m+1 or m |
| 32 | + lps[0]=-1; |
| 33 | + int i = 0, j = -1; |
| 34 | + while (i < m) { |
| 35 | + while (j >= 0 && KMP_pat[i] != KMP_pat[j]) j = lps[j]; |
| 36 | + i++; j++; |
| 37 | + lps[i] = j; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// Search a pattern in a string |
| 42 | +// Assuming lps is allready initialized with KMP_init |
| 43 | +void KMP_search(int options) { |
| 44 | + int n = KMP_str.length(); |
| 45 | + int m = KMP_pat.length(); |
| 46 | + int i = 0, j = 0; |
| 47 | + while (i < n) { |
| 48 | + while (j >= 0 && KMP_str[i] != KMP_pat[j]) j = lps[j]; |
| 49 | + i++; j++; |
| 50 | + if (j == m) { // Pattern found |
| 51 | + v[i-j].push_back({i-j+m, options}); |
| 52 | +// cout << "The pattern " << KMP_pat << " is found at index " << i-j << "-" << i-j+m << endl; |
| 53 | + j = lps[j]; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +int main() { // Kattis - What Does It Mean? - heritage |
| 59 | + int dict, options; |
| 60 | + cin >> dict; |
| 61 | + cin >> KMP_str; |
| 62 | + v = vvii(KMP_str.size(), vii(0)); // init |
| 63 | + while (dict--) { |
| 64 | + cin >> KMP_pat >> options; |
| 65 | + KMP_init(); |
| 66 | + KMP_search(options); |
| 67 | + } |
| 68 | + vector<ll> res(KMP_str.length()+1, 0); |
| 69 | + res[KMP_str.size()] = 1; |
| 70 | + for (int i = KMP_str.length() - 1; i >= 0 ; --i) { |
| 71 | + for (int j = 0; j < v[i].size(); ++j) { |
| 72 | + assert(v[i][j].first <= res.size()); |
| 73 | +// res[i] += mulmodn(res[v[i][j].first], v[i][j].second, 1000000007); |
| 74 | + res[i] += (res[v[i][j].first] * v[i][j].second) % 1000000007; |
| 75 | + res[i] %= 1000000007; |
| 76 | + } |
| 77 | + } |
| 78 | + cout << res[0]; |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
0 commit comments