// clang-format off #include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(), (x).end() template<class C> void mini(C &a5, C b5) { a5 = min(a5, b5); } template<class C> void maxi(C &a5, C b5) { a5 = max(a5, b5); } #ifdef LOCAL const bool debug = true; #else const bool debug = false; #define cerr if (true) {} else cout #endif // clang-format on #define int long long #define double long double const int INF = 1e18; const int mod = 1e9 + 7; const int nax = 610; const int sax = 610; const int L01 = 1; const int R01 = 0; const int L11 = 1; const int R11 = -1; int backward[nax][sax]; int fforward[nax][sax]; int arr[nax]; int step[nax][2]; inline int to11(int x) { // {0, 1} -> {-1, 1} return x * 2 - 1; } inline int to01(int x) { // {-1, 1} -> {0, 1} return x > 0; } struct My { string s; int b[2][sax]; int f_last[2][sax]; int f_suf[2][sax]; int partial_result; int partial_result2; My(string s_) : s(s_) { clear(); init_next_arr(); init_backward(); init_forward(); // init b for (int i = 0; i < 2; i++) { for (int sum = 0; sum < sax; sum++) { if (i == to01(arr[0])) { b[i][sum] = backward[0][sum]; } else { b[i][sum] = backward[ step[0][i] ][sum]; } } } if ('L' == s[0]) { partial_result2 = backward[0][0]; } else { partial_result2 = backward[step[0][1]][0]; } init_f(0); init_f(1); } int solve(const My &other) const { int res = this->partial_result; for (int i = 0; i < 2; i++) { for (int sum = 0; sum < sax; sum++) { res += this->f_last[i][sum] * other.b[i][sum]; res += this->f_suf[i][sum] * other.b[1-i][sum]; res %= mod; } } return res; } void init_f(int letter01) { int end = (int)this->s.size() - 1; while (end >= 0 && !(to01(arr[end]) == letter01)) { end--; } if (end < 0) { f_last[letter01][0] = 1; return; } int begin = end; while (begin > 0 && arr[begin-1] == arr[end]) { begin--; } for (int sum = 0; sum < sax; sum++) { f_last[letter01][sum] = fforward[end][sum]; } if (end != (int)this->s.size() - 1) return; for (int sum = 0; sum < sax; sum++) { for (int i = begin; i <= end; i++) { f_suf[letter01][sum] += fforward[i][sum]; } f_suf[letter01][sum] %= mod; } } void clear() { for (int i = 0; i < (int)this->s.size() + 5; i++) { for (int j = 0; j < sax; j++) { backward[i][j] = fforward[i][j] = 0; } arr[i] = 0; step[i][0] = step[i][1] = 0; } for (int i = 0; i < 2; i++) { for (int j = 0; j < sax; j++) { b[i][j] = f_last[i][j] = f_suf[i][j] = 0; } } } void init_next_arr() { int size = this->s.size(); step[size-1][0] = size; step[size-1][1] = size; for (int i = 0; i < size; i++) { arr[i] = (this->s[i] == 'L') ? 1 : -1; } for (int i = (int)size-2; i >= 0; i--) { for (int j = 0; j < 2; j++) { step[i][j] = step[i+1][j]; } step[i][to01(arr[i+1])] = i+1; } } void init_backward() { for (int i = (int)this->s.size()-1; i >= 0; i--) { for (int sum = 0; sum < (int)this->s.size()-i+2; sum++) { for (int k = 0; k < 2; k++) { backward[i][sum] += backward[ step[i][k] ][ sum+arr[i] ]; } if (sum == 1 && this->s[i] == 'P') { backward[i][sum]++; } backward[i][sum] %= mod; } } } void init_forward() { // init for the first 'L' if (arr[0] == L11) { fforward[ 0 ][1] = 1; } else { fforward[ step[0][L01] ][1] = 1; } for (int i = 0; i < (int)this->s.size()-1; i++) { for (int sum = 0; sum < i+2; sum++) { for (int k = 0; k < 2; k++) { int cur_step = step[i][k]; int cur_sum = sum + arr[cur_step]; if (cur_sum < 0) continue; int &x = fforward[ cur_step ][ cur_sum ]; x = (x + fforward[i][sum]) % mod; } } } this->partial_result = 0; for (int i = 0; i < (int)this->s.size(); i++) { this->partial_result += fforward[i][0]; this->partial_result %= mod; } } void debug_print() { if (!debug) return; cerr << "debug for " << this->s << "\n"; cerr << "forward\n"; for (int i = 0; i < (int)this->s.size(); i++) { for (int sum = 0; sum < 10; sum++) { cerr << fforward[i][sum] << " "; } cerr << "\n"; } cerr << "backward\n"; for (int i = 0; i < (int)this->s.size(); i++) { for (int sum = 0; sum < 10; sum++) { cerr << backward[i][sum] << " "; } cerr << "\n"; } } }; string dummy(int len=600) { string letter = "L"; if (rand() % 2) letter = "P"; string res = ""; for (int i = 0; i < len; i++) { res += letter; } return res; } vector<My> v; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n=600; string s; cin >> n; for (int i = 1; i <= n; i++) { cin >> s; //s = dummy(); v.push_back({s}); v.back().debug_print(); cerr << "parital " << i << "\t" << v.back().partial_result << "\n"; //auto xd = My(s+s); //xd.debug_print(); //cout << xd.partial_result << "\n"; //cout << xd.partial_result2 << "\n"; //exit(0); } for (const auto &i : v) { for (const auto &j : v) { cout << i.solve(j) << " "; } cout << "\n"; } return 0; } /* * L -> 1; P -> -1 * */ /* 4 LLPLPP PPLP LLP P */
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | // clang-format off #include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(), (x).end() template<class C> void mini(C &a5, C b5) { a5 = min(a5, b5); } template<class C> void maxi(C &a5, C b5) { a5 = max(a5, b5); } #ifdef LOCAL const bool debug = true; #else const bool debug = false; #define cerr if (true) {} else cout #endif // clang-format on #define int long long #define double long double const int INF = 1e18; const int mod = 1e9 + 7; const int nax = 610; const int sax = 610; const int L01 = 1; const int R01 = 0; const int L11 = 1; const int R11 = -1; int backward[nax][sax]; int fforward[nax][sax]; int arr[nax]; int step[nax][2]; inline int to11(int x) { // {0, 1} -> {-1, 1} return x * 2 - 1; } inline int to01(int x) { // {-1, 1} -> {0, 1} return x > 0; } struct My { string s; int b[2][sax]; int f_last[2][sax]; int f_suf[2][sax]; int partial_result; int partial_result2; My(string s_) : s(s_) { clear(); init_next_arr(); init_backward(); init_forward(); // init b for (int i = 0; i < 2; i++) { for (int sum = 0; sum < sax; sum++) { if (i == to01(arr[0])) { b[i][sum] = backward[0][sum]; } else { b[i][sum] = backward[ step[0][i] ][sum]; } } } if ('L' == s[0]) { partial_result2 = backward[0][0]; } else { partial_result2 = backward[step[0][1]][0]; } init_f(0); init_f(1); } int solve(const My &other) const { int res = this->partial_result; for (int i = 0; i < 2; i++) { for (int sum = 0; sum < sax; sum++) { res += this->f_last[i][sum] * other.b[i][sum]; res += this->f_suf[i][sum] * other.b[1-i][sum]; res %= mod; } } return res; } void init_f(int letter01) { int end = (int)this->s.size() - 1; while (end >= 0 && !(to01(arr[end]) == letter01)) { end--; } if (end < 0) { f_last[letter01][0] = 1; return; } int begin = end; while (begin > 0 && arr[begin-1] == arr[end]) { begin--; } for (int sum = 0; sum < sax; sum++) { f_last[letter01][sum] = fforward[end][sum]; } if (end != (int)this->s.size() - 1) return; for (int sum = 0; sum < sax; sum++) { for (int i = begin; i <= end; i++) { f_suf[letter01][sum] += fforward[i][sum]; } f_suf[letter01][sum] %= mod; } } void clear() { for (int i = 0; i < (int)this->s.size() + 5; i++) { for (int j = 0; j < sax; j++) { backward[i][j] = fforward[i][j] = 0; } arr[i] = 0; step[i][0] = step[i][1] = 0; } for (int i = 0; i < 2; i++) { for (int j = 0; j < sax; j++) { b[i][j] = f_last[i][j] = f_suf[i][j] = 0; } } } void init_next_arr() { int size = this->s.size(); step[size-1][0] = size; step[size-1][1] = size; for (int i = 0; i < size; i++) { arr[i] = (this->s[i] == 'L') ? 1 : -1; } for (int i = (int)size-2; i >= 0; i--) { for (int j = 0; j < 2; j++) { step[i][j] = step[i+1][j]; } step[i][to01(arr[i+1])] = i+1; } } void init_backward() { for (int i = (int)this->s.size()-1; i >= 0; i--) { for (int sum = 0; sum < (int)this->s.size()-i+2; sum++) { for (int k = 0; k < 2; k++) { backward[i][sum] += backward[ step[i][k] ][ sum+arr[i] ]; } if (sum == 1 && this->s[i] == 'P') { backward[i][sum]++; } backward[i][sum] %= mod; } } } void init_forward() { // init for the first 'L' if (arr[0] == L11) { fforward[ 0 ][1] = 1; } else { fforward[ step[0][L01] ][1] = 1; } for (int i = 0; i < (int)this->s.size()-1; i++) { for (int sum = 0; sum < i+2; sum++) { for (int k = 0; k < 2; k++) { int cur_step = step[i][k]; int cur_sum = sum + arr[cur_step]; if (cur_sum < 0) continue; int &x = fforward[ cur_step ][ cur_sum ]; x = (x + fforward[i][sum]) % mod; } } } this->partial_result = 0; for (int i = 0; i < (int)this->s.size(); i++) { this->partial_result += fforward[i][0]; this->partial_result %= mod; } } void debug_print() { if (!debug) return; cerr << "debug for " << this->s << "\n"; cerr << "forward\n"; for (int i = 0; i < (int)this->s.size(); i++) { for (int sum = 0; sum < 10; sum++) { cerr << fforward[i][sum] << " "; } cerr << "\n"; } cerr << "backward\n"; for (int i = 0; i < (int)this->s.size(); i++) { for (int sum = 0; sum < 10; sum++) { cerr << backward[i][sum] << " "; } cerr << "\n"; } } }; string dummy(int len=600) { string letter = "L"; if (rand() % 2) letter = "P"; string res = ""; for (int i = 0; i < len; i++) { res += letter; } return res; } vector<My> v; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n=600; string s; cin >> n; for (int i = 1; i <= n; i++) { cin >> s; //s = dummy(); v.push_back({s}); v.back().debug_print(); cerr << "parital " << i << "\t" << v.back().partial_result << "\n"; //auto xd = My(s+s); //xd.debug_print(); //cout << xd.partial_result << "\n"; //cout << xd.partial_result2 << "\n"; //exit(0); } for (const auto &i : v) { for (const auto &j : v) { cout << i.solve(j) << " "; } cout << "\n"; } return 0; } /* * L -> 1; P -> -1 * */ /* 4 LLPLPP PPLP LLP P */ |