#include <iostream> #include <vector> #include <string> using namespace std; const int MOD = 1000000007; struct Drybling { string str; int delta_max1; vector<vector<int>> dp1; int delta_max2; vector<vector<int>> dp2; int fl, fp; vector<int>cross_to_L, cross_to_P; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Drybling>D(n); for (auto &d : D) { // read the string cin >> d.str; int l = d.str.length(); // delta max 1 d.delta_max1 = 0; for (char c : d.str) if (c == 'L') d.delta_max1++; // calculate DP1 d.dp1.resize(l, vector<int>(d.delta_max1 + 1)); bool was_l = false; vector<int>tail_sum_L(d.delta_max1 + 1), tail_sum_P(d.delta_max1 + 1); for (int i = 0; i < l; i++) { for (int delta = 0; delta <= d.delta_max1; delta++) if (d.str[i] == 'L') d.dp1[i][delta] = ((delta > 0) ? (tail_sum_L[delta - 1] + int(delta == 1 && !was_l)) : 0) % MOD; else d.dp1[i][delta] = (delta < d.delta_max1) ? tail_sum_P[delta + 1] : 0; for (int delta = 0; delta <= d.delta_max1; delta++) { if (d.str[i] == 'L') tail_sum_L[delta] = 0; else tail_sum_P[delta] = 0; tail_sum_L[delta] = (tail_sum_L[delta] + d.dp1[i][delta]) % MOD; tail_sum_P[delta] = (tail_sum_P[delta] + d.dp1[i][delta]) % MOD; } if (d.str[i] == 'L') was_l = true; } // delta max 2 d.delta_max2 = 0; for (char c : d.str) if (c == 'P') d.delta_max2++; // calculate DP2 d.dp2.resize(l, vector<int>(d.delta_max2 + 1)); int last_l = -1, last_p = -1; for (int i = l - 1; i >= 0; i--) { for (int delta = 0; delta <= d.delta_max2; delta++) if (d.str[i] == 'L') d.dp2[i][delta] = ((delta < d.delta_max2) ? (((last_l==-1) ? 0 : d.dp2[last_l][delta + 1]) + ((last_p==-1) ? 0 : d.dp2[last_p][delta + 1])) : 0) % MOD; else d.dp2[i][delta] = ((delta > 0) ? (((last_l==-1) ? 0 : d.dp2[last_l][delta - 1]) + ((last_p==-1) ? 0 : d.dp2[last_p][delta - 1]) + int(delta == 1)) : 0) % MOD; if (d.str[i] == 'L') last_l = i; else last_p = i; } // first / last occurences d.fl = d.fp = -1; for (int i = 0; i < l; i++) if (d.str[i] == 'L' && d.fl == -1) d.fl = i; else if (d.str[i] == 'P' && d.fp == -1) d.fp = i; // preprocess crossings int inside_l = 0, inside_p = 0; d.cross_to_L.resize(d.delta_max1 + 1); d.cross_to_P.resize(d.delta_max1 + 1); for (int i = l - 1; i >= 0 && (inside_l == 0 || inside_p == 0); i--) { for (int delta = 0; delta <= d.delta_max1; delta++) { if (inside_l == 0) d.cross_to_L[delta] = (d.cross_to_L[delta] + d.dp1[i][delta]) % MOD; if (inside_p == 0) d.cross_to_P[delta] = (d.cross_to_P[delta] + d.dp1[i][delta]) % MOD; } if (d.str[i] == 'L') inside_l++; else inside_p++; } } for (auto& d1 : D) { for (auto& d2 : D) { int l1 = d1.str.length(); int delta_max = min(d1.delta_max1, d2.delta_max2); long long result = 0; for (int delta = 0; delta <= delta_max; delta++) { if (d2.fl != -1) result += (long long)d1.cross_to_L[delta] * d2.dp2[d2.fl][delta]; if (d2.fp != -1) result += (long long)d1.cross_to_P[delta] * d2.dp2[d2.fp][delta]; result %= MOD; } for (int i = 0; i < l1; i++) result += d1.dp1[i][0]; if (d1.fl == -1 && d2.fl != -1) result += d2.dp2[d2.fl][0]; cout << result % MOD << " "; } cout << "\n"; } return 0; }
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #include <iostream> #include <vector> #include <string> using namespace std; const int MOD = 1000000007; struct Drybling { string str; int delta_max1; vector<vector<int>> dp1; int delta_max2; vector<vector<int>> dp2; int fl, fp; vector<int>cross_to_L, cross_to_P; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Drybling>D(n); for (auto &d : D) { // read the string cin >> d.str; int l = d.str.length(); // delta max 1 d.delta_max1 = 0; for (char c : d.str) if (c == 'L') d.delta_max1++; // calculate DP1 d.dp1.resize(l, vector<int>(d.delta_max1 + 1)); bool was_l = false; vector<int>tail_sum_L(d.delta_max1 + 1), tail_sum_P(d.delta_max1 + 1); for (int i = 0; i < l; i++) { for (int delta = 0; delta <= d.delta_max1; delta++) if (d.str[i] == 'L') d.dp1[i][delta] = ((delta > 0) ? (tail_sum_L[delta - 1] + int(delta == 1 && !was_l)) : 0) % MOD; else d.dp1[i][delta] = (delta < d.delta_max1) ? tail_sum_P[delta + 1] : 0; for (int delta = 0; delta <= d.delta_max1; delta++) { if (d.str[i] == 'L') tail_sum_L[delta] = 0; else tail_sum_P[delta] = 0; tail_sum_L[delta] = (tail_sum_L[delta] + d.dp1[i][delta]) % MOD; tail_sum_P[delta] = (tail_sum_P[delta] + d.dp1[i][delta]) % MOD; } if (d.str[i] == 'L') was_l = true; } // delta max 2 d.delta_max2 = 0; for (char c : d.str) if (c == 'P') d.delta_max2++; // calculate DP2 d.dp2.resize(l, vector<int>(d.delta_max2 + 1)); int last_l = -1, last_p = -1; for (int i = l - 1; i >= 0; i--) { for (int delta = 0; delta <= d.delta_max2; delta++) if (d.str[i] == 'L') d.dp2[i][delta] = ((delta < d.delta_max2) ? (((last_l==-1) ? 0 : d.dp2[last_l][delta + 1]) + ((last_p==-1) ? 0 : d.dp2[last_p][delta + 1])) : 0) % MOD; else d.dp2[i][delta] = ((delta > 0) ? (((last_l==-1) ? 0 : d.dp2[last_l][delta - 1]) + ((last_p==-1) ? 0 : d.dp2[last_p][delta - 1]) + int(delta == 1)) : 0) % MOD; if (d.str[i] == 'L') last_l = i; else last_p = i; } // first / last occurences d.fl = d.fp = -1; for (int i = 0; i < l; i++) if (d.str[i] == 'L' && d.fl == -1) d.fl = i; else if (d.str[i] == 'P' && d.fp == -1) d.fp = i; // preprocess crossings int inside_l = 0, inside_p = 0; d.cross_to_L.resize(d.delta_max1 + 1); d.cross_to_P.resize(d.delta_max1 + 1); for (int i = l - 1; i >= 0 && (inside_l == 0 || inside_p == 0); i--) { for (int delta = 0; delta <= d.delta_max1; delta++) { if (inside_l == 0) d.cross_to_L[delta] = (d.cross_to_L[delta] + d.dp1[i][delta]) % MOD; if (inside_p == 0) d.cross_to_P[delta] = (d.cross_to_P[delta] + d.dp1[i][delta]) % MOD; } if (d.str[i] == 'L') inside_l++; else inside_p++; } } for (auto& d1 : D) { for (auto& d2 : D) { int l1 = d1.str.length(); int delta_max = min(d1.delta_max1, d2.delta_max2); long long result = 0; for (int delta = 0; delta <= delta_max; delta++) { if (d2.fl != -1) result += (long long)d1.cross_to_L[delta] * d2.dp2[d2.fl][delta]; if (d2.fp != -1) result += (long long)d1.cross_to_P[delta] * d2.dp2[d2.fp][delta]; result %= MOD; } for (int i = 0; i < l1; i++) result += d1.dp1[i][0]; if (d1.fl == -1 && d2.fl != -1) result += d2.dp2[d2.fl][0]; cout << result % MOD << " "; } cout << "\n"; } return 0; } |