#include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int mod = 1000 * 1000 * 1000 + 7; const int maxN = 600; const int maxH = 600; const int maxDim = 2 * (maxH + 1) + 1; const int L1 = 0; const int L2 = maxH; const int R1 = maxH + 1; const int R2 = 2 * maxH + 1; const int ORI = 2 * maxH + 2; int dpl[maxN][maxDim]; int dpr[maxN][maxDim]; string s[maxN]; int compute(int l, int r) { int res = 0; rep(i, maxDim) res = (res + 1ll * dpl[l][i] * dpr[r][i]) % mod; return res; } int aux[2][maxDim]; void prepl(int a) { rep(i, maxDim) aux[1][i] = 0; aux[1][ORI] = 1; rep(i, sz(s[a])) { int c1 = (i & 1); int c2 = (c1 ^ 1); aux[c1][ORI] = aux[c2][ORI]; if (s[a][i] == 'L') { fwd(j, R1, R2 + 1) aux[c1][j] = aux[c2][j]; fwd(j, L1 + 1, L2 + 1) { aux[c1][j] = aux[c2][j - 1] + aux[c2][j + maxH]; if (aux[c1][j] >= mod) aux[c1][j] -= mod; } aux[c1][L1] = 0; aux[c1][L1 + 1] = aux[c1][L1 + 1] + aux[c2][ORI]; if (aux[c1][L1 + 1] >= mod) aux[c1][L1 + 1] -= mod; } else { fwd(j, L1, L2 + 1) aux[c1][j] = aux[c2][j]; fwd(j, R1, R2) { aux[c1][j] = aux[c2][j + 1] + aux[c2][j - maxH]; if (aux[c1][j] >= mod) aux[c1][j] -= mod; } aux[c1][R2] = 0; } } rep(i, maxDim) dpl[a][i] = aux[(sz(s[a]) - 1) & 1][i]; } void prepr(int a) { reverse(all(s[a])); rep(i, maxDim) aux[1][i] = 0; aux[1][R1] = 1; rep(i, sz(s[a])) { int c1 = (i & 1); int c2 = (c1 ^ 1); aux[c1][ORI] = aux[c2][ORI]; if (s[a][i] == 'L') { fwd(j, L1, L2) aux[c1][j] = aux[c2][j + 1]; aux[c1][L2] = 0; fwd(j, R1, R2) { aux[c1][j] = aux[c2][j] + aux[c2][j - maxH]; if (aux[c1][j] >= mod) aux[c1][j] -= mod; } aux[c1][ORI] += aux[c2][L1 + 1]; if (aux[c1][ORI] >= mod) aux[c1][ORI] -= mod; aux[c1][R2] = aux[c2][R2]; } else { fwd(j, R1 + 1, R2 + 1) aux[c1][j] = aux[c2][j - 1]; aux[c1][R1] = 0; fwd(j, L1 + 1, L2 + 1) { aux[c1][j] = aux[c2][j] + aux[c2][j + maxH]; if (aux[c1][j] >= mod) aux[c1][j] -= mod; } aux[c1][L1] = aux[c2][L1]; } } rep(i, maxDim) dpr[a][i] = aux[(sz(s[a]) - 1) & 1][i]; } int32_t main() { fastio; int n; cin >> n; rep(i, n) { cin >> s[i]; prepl(i); prepr(i); } rep(i, n) { rep(j, n) cout << compute(i, j) << ' '; 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 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 | #include <chrono> #include <cassert> #include <string> #include <array> #include <deque> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> #include <iterator> #include <algorithm> #include <cmath> #include <complex> #include <numeric> #include <random> #include <ios> #include <iostream> #include <bitset> using namespace std; #define fwd(i, a, b) for (int i = (a); i < (b); ++ i) #define rep(i, n) for (int i = 0; i < (n); ++ i) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)x.size()) #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ll long long #ifdef LOCAL template<typename Stream, typename T1, typename T2> Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";} template<typename Stream, typename T> Stream &operator << (Stream& out, T v) { out << "{"; int i = 0; for (auto x : v) out << x << ((++ i) != sz(v) ? ", " : ""); return out << "}"; } template<typename... Args> void dump(Args... x) {((cerr << x << ", "), ...) << '\n';} #define debug(x...) cerr << "[" #x "]: ", dump(x) #else #define debug(...) 0 #endif const int mod = 1000 * 1000 * 1000 + 7; const int maxN = 600; const int maxH = 600; const int maxDim = 2 * (maxH + 1) + 1; const int L1 = 0; const int L2 = maxH; const int R1 = maxH + 1; const int R2 = 2 * maxH + 1; const int ORI = 2 * maxH + 2; int dpl[maxN][maxDim]; int dpr[maxN][maxDim]; string s[maxN]; int compute(int l, int r) { int res = 0; rep(i, maxDim) res = (res + 1ll * dpl[l][i] * dpr[r][i]) % mod; return res; } int aux[2][maxDim]; void prepl(int a) { rep(i, maxDim) aux[1][i] = 0; aux[1][ORI] = 1; rep(i, sz(s[a])) { int c1 = (i & 1); int c2 = (c1 ^ 1); aux[c1][ORI] = aux[c2][ORI]; if (s[a][i] == 'L') { fwd(j, R1, R2 + 1) aux[c1][j] = aux[c2][j]; fwd(j, L1 + 1, L2 + 1) { aux[c1][j] = aux[c2][j - 1] + aux[c2][j + maxH]; if (aux[c1][j] >= mod) aux[c1][j] -= mod; } aux[c1][L1] = 0; aux[c1][L1 + 1] = aux[c1][L1 + 1] + aux[c2][ORI]; if (aux[c1][L1 + 1] >= mod) aux[c1][L1 + 1] -= mod; } else { fwd(j, L1, L2 + 1) aux[c1][j] = aux[c2][j]; fwd(j, R1, R2) { aux[c1][j] = aux[c2][j + 1] + aux[c2][j - maxH]; if (aux[c1][j] >= mod) aux[c1][j] -= mod; } aux[c1][R2] = 0; } } rep(i, maxDim) dpl[a][i] = aux[(sz(s[a]) - 1) & 1][i]; } void prepr(int a) { reverse(all(s[a])); rep(i, maxDim) aux[1][i] = 0; aux[1][R1] = 1; rep(i, sz(s[a])) { int c1 = (i & 1); int c2 = (c1 ^ 1); aux[c1][ORI] = aux[c2][ORI]; if (s[a][i] == 'L') { fwd(j, L1, L2) aux[c1][j] = aux[c2][j + 1]; aux[c1][L2] = 0; fwd(j, R1, R2) { aux[c1][j] = aux[c2][j] + aux[c2][j - maxH]; if (aux[c1][j] >= mod) aux[c1][j] -= mod; } aux[c1][ORI] += aux[c2][L1 + 1]; if (aux[c1][ORI] >= mod) aux[c1][ORI] -= mod; aux[c1][R2] = aux[c2][R2]; } else { fwd(j, R1 + 1, R2 + 1) aux[c1][j] = aux[c2][j - 1]; aux[c1][R1] = 0; fwd(j, L1 + 1, L2 + 1) { aux[c1][j] = aux[c2][j] + aux[c2][j + maxH]; if (aux[c1][j] >= mod) aux[c1][j] -= mod; } aux[c1][L1] = aux[c2][L1]; } } rep(i, maxDim) dpr[a][i] = aux[(sz(s[a]) - 1) & 1][i]; } int32_t main() { fastio; int n; cin >> n; rep(i, n) { cin >> s[i]; prepl(i); prepr(i); } rep(i, n) { rep(j, n) cout << compute(i, j) << ' '; cout << '\n'; } return 0; } |