Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int MAXN = 5e4 + 10;
- const int MAXM = 51;
- const int MOD = 1e9 + 7;
- string dict[MAXM];
- bool censured[MAXM][MAXM];
- int selfix[MAXN][MAXM];
- bool word[MAXN][MAXM];
- long long ans[MAXM];
- long long dplr[MAXN][MAXM], dprl[MAXN][MAXM];
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int n, m, k;
- cin >> n >> m >> k;
- string v;
- cin >> v;
- for (int i = 1; i <= m; i++) {
- cin >> dict[i];
- selfix[0][i] = 0;
- for (int j = 1; j < dict[i].size(); j++) {
- int curr = selfix[j - 1][i] + 1;
- while (dict[i][curr - 1] != dict[i][j]) {
- if (curr <= 1) {
- curr = 0;
- break;
- }
- curr = selfix[curr - 2][i] + 1;
- }
- selfix[j][i] = curr;
- }
- int curr = 0;
- for (int j = 0; j < n; j++) {
- while (v[j] != dict[i][curr]) {
- if (curr <= 0) {
- curr = -1;
- break;
- }
- curr = selfix[curr - 1][i];
- }
- curr++;
- if (curr == dict[i].size()) {
- word[j - curr + 1][i] = true;
- // cout << j - curr + 1 << '\n';
- curr = selfix[curr - 1][i];
- }
- }
- }
- for (int i = 0; i < k; i++) {
- int a, b;
- cin >> a >> b;
- censured[a][b] = true;
- }
- for (int i = 1; i <= m; i++) {
- if (word[0][i]) {
- dplr[dict[i].size()][i]++;
- }
- }
- for (int j = 1; j < n; j++) {
- for (int i = 1; i <= m; i++) {
- for (int h = 1; h <= m; h++) {
- if (!censured[i][h] && word[j][h] && j + dict[h].size() <= n) {
- dplr[j + dict[h].size()][h] += dplr[j][i];
- if (dplr[j + dict[h].size()][h] >= MOD) dplr[j + dict[h].size()][h] -= MOD;
- }
- }
- }
- }
- for (int i = 1; i <= m; i++) {
- if (word[n - dict[i].size()][i]) {
- dprl[n - dict[i].size()][i]++;
- }
- }
- for (int j = n - 1; j >= 0; j--) {
- for (int i = 1; i <= m; i++) {
- for (int h = 1; h <= m; h++) {
- if (!censured[i][h] && j >= dict[i].size() && word[j - dict[i].size()][i]) {
- dprl[j - dict[i].size()][i] += dprl[j][h];
- if (dprl[j - dict[i].size()][i] >= MOD) dprl[j - dict[i].size()][i] -= MOD;
- }
- }
- }
- }
- for (int j = 0; j < n; j++) {
- for (int i = 1; i <= m; i++) {
- if (word[j][i]) {
- long long suml = 0, sumr = 0;
- for (int h = 1; h <= m; h++) {
- if (!censured[h][i] && j - 1 >= 0) {
- suml += dplr[j][h];
- if (suml >= MOD) suml -= MOD;
- }
- if (!censured[i][h] && j + dict[i].size() < n) {
- sumr += dprl[j + dict[i].size()][h];
- if (sumr >= MOD) sumr -= MOD;
- }
- }
- if (j == 0) {
- ans[i] += sumr;
- if (ans[i] >= MOD) ans[i] -= MOD;
- } else if (j + dict[i].size() == n) {
- ans[i] += suml;
- if (ans[i] >= MOD) ans[i] -= MOD;
- } else {
- ans[i] += suml * sumr % MOD;
- if (ans[i] >= MOD) ans[i] -= MOD;
- }
- }
- }
- }
- for (int i = 1; i <= m; i++) {
- cout << ans[i] << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement