#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } const int MOD = 1e9 + 7, NONE = 1e9; struct modint { int x = 0; modint operator+(modint o) const { return {(x + o.x) % MOD}; } modint operator*(modint o) const { return {ll(x) * o.x % MOD}; } modint & operator+=(modint o) { return *this = *this + o; } }; void __dbg(modint m) { cout << m.x; } template<typename T> void upsize(T &t, int sz) { if ((int)t.size() < sz) t.resize(sz); } struct LConnector { modint in{0}; vector<modint> openmul, closemul; LConnector(const string &s, const vi& no, const vi& nc) { const int n = (int)s.size(); vector<vector<modint>> dp(n); if (s[0] == 'L') { upsize(dp[0], 2); dp[0][1] += modint{1}; } else if (no[0] != NONE) { upsize(dp[no[0]], 2); dp[no[0]][1] += modint{1}; } else { upsize(openmul, 2); openmul[1] += modint{1}; return; } for (int i = 0; i < n; ++i) { for (int lev = 0; lev < (int)dp[i].size(); ++lev) { if (!lev) in += dp[i][lev]; vector<modint> &noref = no[i] == NONE ? openmul : dp[no[i]]; vector<modint> &ncref = nc[i] == NONE ? closemul : dp[nc[i]]; upsize(noref, lev+2); noref[lev+1] += dp[i][lev]; if (!lev) continue; upsize(ncref, lev); ncref[lev-1] += dp[i][lev]; } } debug(s, in, openmul, closemul); } }; const int MXH = 610; struct RConnector { vector<modint> openmul, closemul; RConnector(const string &s, const vi& no, const vi& nc) { const int n = (int)s.size(); vector<vector<modint>> dp(n, vector<modint>(MXH)); for (int i = n-1; i >= 0; --i) { for (int lev = 0; lev < MXH; ++lev) { if (lev == 0 && s[i] == 'P') dp[i][lev] += modint{1}; if (no[i] != NONE && lev+1 < MXH) dp[i][lev] += dp[no[i]][lev+1]; if (nc[i] != NONE && lev) dp[i][lev] += dp[nc[i]][lev-1]; } } if (no[0] != NONE) openmul = move(dp[no[0]]); if (nc[0] != NONE) closemul = move(dp[nc[0]]); if (s[0] == 'L') openmul = move(dp[0]); else closemul = move(dp[0]); debug(s, openmul, closemul); } }; modint connect(const LConnector &l, const RConnector &r) { modint ret = l.in; for (int i = 0; i < (int)l.openmul.size() && i < (int)r.openmul.size(); ++i) { ret += l.openmul[i] * r.openmul[i]; } for (int i = 0; i < (int)l.closemul.size() && i < (int)r.closemul.size(); ++i) { ret += l.closemul[i] * r.closemul[i]; } return ret; } void solve() { int n; cin >> n; // n=600; // string bd = string(600, 'L'); vector<string> v(n); for (string &s : v) cin >> s; // for (string &s : v) { // random_shuffle(all(bd)); // s = bd; // } vector<LConnector> lv; vector<RConnector> rv; for (const string &s : v) { int k = (int)s.size(); vi no(k, NONE), nc(k, NONE); debug(s); for (int i = k - 2; i >= 0; --i) { no[i] = no[i+1]; nc[i] = nc[i+1]; if (s[i+1] == 'L') no[i] = i+1; else nc[i] = i+1; } debug(s, no, nc); lv.emplace_back(s, no, nc); rv.emplace_back(s, no, nc); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { modint m = connect(lv[i], rv[j]); cout << m.x << ' '; } cout << '\n'; } } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); 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 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 | #include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define repp(i, n, m) for (int i = (n); i < (m); ++i) #define repr(i, n) for (int i = (n) - 1; i >= 0; --i) #define reppr(i, n, m) for (int i = (m) - 1; i >= (n); --i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <typename T, typename V> void mini(T& a, V b) {if (b < a) a = b; } template <typename T, typename V> void maxi(T& a, V b) {if (b > a) a = b; } const int MOD = 1e9 + 7, NONE = 1e9; struct modint { int x = 0; modint operator+(modint o) const { return {(x + o.x) % MOD}; } modint operator*(modint o) const { return {ll(x) * o.x % MOD}; } modint & operator+=(modint o) { return *this = *this + o; } }; void __dbg(modint m) { cout << m.x; } template<typename T> void upsize(T &t, int sz) { if ((int)t.size() < sz) t.resize(sz); } struct LConnector { modint in{0}; vector<modint> openmul, closemul; LConnector(const string &s, const vi& no, const vi& nc) { const int n = (int)s.size(); vector<vector<modint>> dp(n); if (s[0] == 'L') { upsize(dp[0], 2); dp[0][1] += modint{1}; } else if (no[0] != NONE) { upsize(dp[no[0]], 2); dp[no[0]][1] += modint{1}; } else { upsize(openmul, 2); openmul[1] += modint{1}; return; } for (int i = 0; i < n; ++i) { for (int lev = 0; lev < (int)dp[i].size(); ++lev) { if (!lev) in += dp[i][lev]; vector<modint> &noref = no[i] == NONE ? openmul : dp[no[i]]; vector<modint> &ncref = nc[i] == NONE ? closemul : dp[nc[i]]; upsize(noref, lev+2); noref[lev+1] += dp[i][lev]; if (!lev) continue; upsize(ncref, lev); ncref[lev-1] += dp[i][lev]; } } debug(s, in, openmul, closemul); } }; const int MXH = 610; struct RConnector { vector<modint> openmul, closemul; RConnector(const string &s, const vi& no, const vi& nc) { const int n = (int)s.size(); vector<vector<modint>> dp(n, vector<modint>(MXH)); for (int i = n-1; i >= 0; --i) { for (int lev = 0; lev < MXH; ++lev) { if (lev == 0 && s[i] == 'P') dp[i][lev] += modint{1}; if (no[i] != NONE && lev+1 < MXH) dp[i][lev] += dp[no[i]][lev+1]; if (nc[i] != NONE && lev) dp[i][lev] += dp[nc[i]][lev-1]; } } if (no[0] != NONE) openmul = move(dp[no[0]]); if (nc[0] != NONE) closemul = move(dp[nc[0]]); if (s[0] == 'L') openmul = move(dp[0]); else closemul = move(dp[0]); debug(s, openmul, closemul); } }; modint connect(const LConnector &l, const RConnector &r) { modint ret = l.in; for (int i = 0; i < (int)l.openmul.size() && i < (int)r.openmul.size(); ++i) { ret += l.openmul[i] * r.openmul[i]; } for (int i = 0; i < (int)l.closemul.size() && i < (int)r.closemul.size(); ++i) { ret += l.closemul[i] * r.closemul[i]; } return ret; } void solve() { int n; cin >> n; // n=600; // string bd = string(600, 'L'); vector<string> v(n); for (string &s : v) cin >> s; // for (string &s : v) { // random_shuffle(all(bd)); // s = bd; // } vector<LConnector> lv; vector<RConnector> rv; for (const string &s : v) { int k = (int)s.size(); vi no(k, NONE), nc(k, NONE); debug(s); for (int i = k - 2; i >= 0; --i) { no[i] = no[i+1]; nc[i] = nc[i+1]; if (s[i+1] == 'L') no[i] = i+1; else nc[i] = i+1; } debug(s, no, nc); lv.emplace_back(s, no, nc); rv.emplace_back(s, no, nc); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { modint m = connect(lv[i], rv[j]); cout << m.x << ' '; } cout << '\n'; } } int main() { #ifndef DEBUG ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif int z = 1; // scanf("%d", &z); while (z--) solve(); return 0; } |