//GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 610, mod = 1e9 + 7; int n; string s[nax]; int arr[nax][2]; int dpL[nax][nax]; int dpR[nax][nax]; int op[nax][nax][2]; int cl[nax][nax][2]; int me[nax]; ll ans[nax][nax]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; //n = 600; for (int i = 0; i < n; ++i) { cin >> s[i]; /* for (int j = 0; j < 600; ++j) { s[i] += ((j & 1) ? "L" : "P"); } */ } for (int i = 0; i < n; ++i) { int l = (int)s[i].size() + 1, p = (int)s[i].size() + 1; for (int j = (int)s[i].size(); j >= 1; --j) { arr[j][0] = l; arr[j][1] = p; if (s[i][j - 1] == 'L') l = j; else p = j; } arr[0][0] = l; arr[0][1] = p; for (int j = 0; j <= (int)s[i].size(); ++j) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { dpL[j][sum] = 0; } } dpL[0][0] = 1; for (int j = 0; j <= (int)s[i].size(); ++j) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { if (arr[j][0] == (int)s[i].size() + 1) { op[i][sum][0] += dpL[j][sum]; if (op[i][sum][0] >= mod) op[i][sum][0] -= mod; } else if (sum < (int)s[i].size()) { dpL[arr[j][0]][sum + 1] += dpL[j][sum]; if (dpL[arr[j][0]][sum + 1] >= mod) dpL[arr[j][0]][sum + 1] -= mod; } if (arr[j][1] == (int)s[i].size() + 1) { op[i][sum][1] += dpL[j][sum]; if (op[i][sum][1] >= mod) op[i][sum][1] -= mod; } else if (sum > 0) { dpL[arr[j][1]][sum - 1] += dpL[j][sum]; if (dpL[arr[j][1]][sum - 1] >= mod) dpL[arr[j][1]][sum - 1] -= mod; } } me[i] = (me[i] + dpL[j][0]) % mod; } for (int j = 0; j <= (int)s[i].size(); ++j) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { dpR[j][sum] = 0; } } for (int j = (int)s[i].size(); j >= 1; --j) { int dx = 1; if (s[i][j - 1] == 'P') { dpR[j][1] = 1; dx = -1; } for (int sum = 0; sum <= (int)s[i].size(); ++sum) { if (arr[j][0] != (int)s[i].size() + 1 && sum + dx >= 0 && sum + dx <= (int)s[i].size()) { dpR[j][sum] += dpR[arr[j][0]][sum + dx]; if (dpR[j][sum] >= mod) dpR[j][sum] -= mod; } if (arr[j][1] != (int)s[i].size() + 1 && sum + dx >= 0 && sum + dx <= (int)s[i].size()) { dpR[j][sum] += dpR[arr[j][1]][sum + dx]; if (dpR[j][sum] >= mod) dpR[j][sum] -= mod; } } } if (arr[0][0] != (int)s[i].size() + 1) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { cl[i][sum][0] = dpR[arr[0][0]][sum]; } } if (arr[0][1] != (int)s[i].size() + 1) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { cl[i][sum][1] = dpR[arr[0][1]][sum]; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { ans[i][j] = me[i]; for (int sum = 0; sum <= (int)s[i].size(); ++sum) { ans[i][j] = (ans[i][j] + (ll)op[i][sum][0] * cl[j][sum][0] + (ll)op[i][sum][1] * cl[j][sum][1]) % mod; } ans[i][j]--; if (ans[i][j] < 0) ans[i][j] += mod; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << ans[i][j] << " "; } cout << "\n"; } }
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 | //GRT_2018 #include <bits/stdc++.h> #define PB push_back #define ST first #define ND second //mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); using namespace std; using ll = long long; using pi = pair<int,int>; using vi = vector<int>; const int nax = 610, mod = 1e9 + 7; int n; string s[nax]; int arr[nax][2]; int dpL[nax][nax]; int dpR[nax][nax]; int op[nax][nax][2]; int cl[nax][nax][2]; int me[nax]; ll ans[nax][nax]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; //n = 600; for (int i = 0; i < n; ++i) { cin >> s[i]; /* for (int j = 0; j < 600; ++j) { s[i] += ((j & 1) ? "L" : "P"); } */ } for (int i = 0; i < n; ++i) { int l = (int)s[i].size() + 1, p = (int)s[i].size() + 1; for (int j = (int)s[i].size(); j >= 1; --j) { arr[j][0] = l; arr[j][1] = p; if (s[i][j - 1] == 'L') l = j; else p = j; } arr[0][0] = l; arr[0][1] = p; for (int j = 0; j <= (int)s[i].size(); ++j) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { dpL[j][sum] = 0; } } dpL[0][0] = 1; for (int j = 0; j <= (int)s[i].size(); ++j) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { if (arr[j][0] == (int)s[i].size() + 1) { op[i][sum][0] += dpL[j][sum]; if (op[i][sum][0] >= mod) op[i][sum][0] -= mod; } else if (sum < (int)s[i].size()) { dpL[arr[j][0]][sum + 1] += dpL[j][sum]; if (dpL[arr[j][0]][sum + 1] >= mod) dpL[arr[j][0]][sum + 1] -= mod; } if (arr[j][1] == (int)s[i].size() + 1) { op[i][sum][1] += dpL[j][sum]; if (op[i][sum][1] >= mod) op[i][sum][1] -= mod; } else if (sum > 0) { dpL[arr[j][1]][sum - 1] += dpL[j][sum]; if (dpL[arr[j][1]][sum - 1] >= mod) dpL[arr[j][1]][sum - 1] -= mod; } } me[i] = (me[i] + dpL[j][0]) % mod; } for (int j = 0; j <= (int)s[i].size(); ++j) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { dpR[j][sum] = 0; } } for (int j = (int)s[i].size(); j >= 1; --j) { int dx = 1; if (s[i][j - 1] == 'P') { dpR[j][1] = 1; dx = -1; } for (int sum = 0; sum <= (int)s[i].size(); ++sum) { if (arr[j][0] != (int)s[i].size() + 1 && sum + dx >= 0 && sum + dx <= (int)s[i].size()) { dpR[j][sum] += dpR[arr[j][0]][sum + dx]; if (dpR[j][sum] >= mod) dpR[j][sum] -= mod; } if (arr[j][1] != (int)s[i].size() + 1 && sum + dx >= 0 && sum + dx <= (int)s[i].size()) { dpR[j][sum] += dpR[arr[j][1]][sum + dx]; if (dpR[j][sum] >= mod) dpR[j][sum] -= mod; } } } if (arr[0][0] != (int)s[i].size() + 1) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { cl[i][sum][0] = dpR[arr[0][0]][sum]; } } if (arr[0][1] != (int)s[i].size() + 1) { for (int sum = 0; sum <= (int)s[i].size(); ++sum) { cl[i][sum][1] = dpR[arr[0][1]][sum]; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { ans[i][j] = me[i]; for (int sum = 0; sum <= (int)s[i].size(); ++sum) { ans[i][j] = (ans[i][j] + (ll)op[i][sum][0] * cl[j][sum][0] + (ll)op[i][sum][1] * cl[j][sum][1]) % mod; } ans[i][j]--; if (ans[i][j] < 0) ans[i][j] += mod; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << ans[i][j] << " "; } cout << "\n"; } } |