#include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif /* * Opis: Struktura do działań modulo * Czas: O(1), dzielenie O(\log mod) * Użycie: Ustaw modulo w ostatniej linii. Jeśli modulo nie jest const, usuń pierwszy wiersz i zadeklaruj mod */ template<int mod> struct modular { int val; modular() { val = 0; } modular(const LL& v) { val = int((-mod <= v && v < mod) ? (int) v : v % mod); if(val < 0) val += mod; } int to_int() { return val; } friend ostream& operator<<(ostream &os, const modular &a) { #ifdef DEBUG constexpr int mx = 1024; for(int y = 1; y <= mx; ++y) if(a * y <= mx) return os << (a * y).val << '/' << y; else if(mod - a * y <= mx) return os << '-' << (mod - a * y).val << '/' << y; #endif return os << a.val; } friend istream& operator>>(istream &is, modular &a) { return is >> a.val; } friend bool operator==(const modular &a, const modular &b) { return a.val == b.val; } friend bool operator!=(const modular &a, const modular &b) { return !(a == b); } friend bool operator<(const modular &a, const modular &b) { return a.val < b.val; } friend bool operator<=(const modular &a, const modular &b) { return a.val <= b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular &m) { if((val += m.val) >= mod) val -= mod; return *this; } modular& operator-=(const modular &m) { if((val -= m.val) < 0) val += mod; return *this; } modular& operator*=(const modular &m) { val = int((LL) val * m.val % mod); return *this; } friend modular qpow(modular a, LL n) { if(n == 0) return 1; if(n % 2 == 1) return qpow(a, n - 1) * a; return qpow(a * a, n / 2); } friend modular inv(const modular &a) { assert(a != 0); return qpow(a, mod - 2); } modular& operator/=(const modular &m) { return (*this) *= inv(m); } modular operator++(int) { modular res = (*this); (*this) += 1; return res; } friend modular operator+(modular a, const modular &b) { return a += b; } friend modular operator-(modular a, const modular &b) { return a -= b; } friend modular operator*(modular a, const modular &b) { return a *= b; } friend modular operator/(modular a, const modular &b) { return a /= b; } }; using mint = modular<int(1e9 + 7)>; int main() { cin.tie(0)->sync_with_stdio(0); int n, m = 0; cin >> n; vector<vector<int>> t(n); REP (i, n) { string s; cin >> s; t[i].resize(ssize(s) + 1); REP (j, ssize(s)) { if (s[j] == 'L') t[i][j + 1] = 0; else t[i][j + 1] = 1; } m = max(m, ssize(s)); } vector<vector<mint>> pref_lf(n), pref_rg(n), suff_lf(n), suff_rg(n); vector odp(n, vector<mint>(n)); vector<mint> all(n); REP (i, n) { int d = ssize(t[i]) - 1; vector nxt(d + 2, vector (2, d + 1)); for (int j = d - 1; j >= 0; --j) { REP (k, 2) nxt[j][k] = nxt[j + 1][k]; nxt[j][t[i][j + 1]] = j + 1; } vector dp(d + 2, vector<mint>(m + 2)); dp[0][0] = 1; pref_lf[i].resize(m + 2); pref_rg[i].resize(m + 2); REP (j, d + 1) { if (j) all[i] += dp[j][0]; REP (k, m + 1) { if (nxt[j][0] == d + 1) { pref_lf[i][k + 1] += dp[j][k]; } else { dp[nxt[j][0]][k + 1] += dp[j][k]; } if (k) { if (nxt[j][1] == d + 1) { pref_rg[i][k - 1] += dp[j][k]; } else { dp[nxt[j][1]][k - 1] += dp[j][k]; } } } } vector pd(d + 2, vector<mint>(m + 2)); for (int j = d; j; --j) { pd[j][0] = 1; REP (k, m + 1) { pd[j][k] += pd[nxt[j][0]][k + 1]; if (k) pd[j][k] += pd[nxt[j][1]][k - 1]; } } suff_lf[i] = pd[nxt[0][0]]; suff_rg[i] = pd[nxt[0][1]]; } REP (i, n) debug(i, pref_lf[i]); REP (i, n) debug(i, suff_lf[i]); REP (i, n) debug(i, pref_rg[i]); REP (i, n) debug(i, suff_rg[i]); REP (i, n) { REP (j, n) { odp[i][j] += all[i]; REP (k, m + 2) { odp[i][j] += pref_lf[i][k] * suff_lf[j][k] + pref_rg[i][k] * suff_rg[j][k]; } } } REP (i, n) { REP (j, n) { if (j) cout << ' '; cout << odp[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 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 | #include<bits/stdc++.h> using namespace std; using LL=long long; #define FOR(i,l,r) for(int i=(l);i<=(r);++i) #define REP(i,n) FOR(i,0,(n)-1) #define ssize(x) int(x.size()) template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';} template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';} #ifdef DEBUG #define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x) #else #define debug(...) {} #endif /* * Opis: Struktura do działań modulo * Czas: O(1), dzielenie O(\log mod) * Użycie: Ustaw modulo w ostatniej linii. Jeśli modulo nie jest const, usuń pierwszy wiersz i zadeklaruj mod */ template<int mod> struct modular { int val; modular() { val = 0; } modular(const LL& v) { val = int((-mod <= v && v < mod) ? (int) v : v % mod); if(val < 0) val += mod; } int to_int() { return val; } friend ostream& operator<<(ostream &os, const modular &a) { #ifdef DEBUG constexpr int mx = 1024; for(int y = 1; y <= mx; ++y) if(a * y <= mx) return os << (a * y).val << '/' << y; else if(mod - a * y <= mx) return os << '-' << (mod - a * y).val << '/' << y; #endif return os << a.val; } friend istream& operator>>(istream &is, modular &a) { return is >> a.val; } friend bool operator==(const modular &a, const modular &b) { return a.val == b.val; } friend bool operator!=(const modular &a, const modular &b) { return !(a == b); } friend bool operator<(const modular &a, const modular &b) { return a.val < b.val; } friend bool operator<=(const modular &a, const modular &b) { return a.val <= b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular &m) { if((val += m.val) >= mod) val -= mod; return *this; } modular& operator-=(const modular &m) { if((val -= m.val) < 0) val += mod; return *this; } modular& operator*=(const modular &m) { val = int((LL) val * m.val % mod); return *this; } friend modular qpow(modular a, LL n) { if(n == 0) return 1; if(n % 2 == 1) return qpow(a, n - 1) * a; return qpow(a * a, n / 2); } friend modular inv(const modular &a) { assert(a != 0); return qpow(a, mod - 2); } modular& operator/=(const modular &m) { return (*this) *= inv(m); } modular operator++(int) { modular res = (*this); (*this) += 1; return res; } friend modular operator+(modular a, const modular &b) { return a += b; } friend modular operator-(modular a, const modular &b) { return a -= b; } friend modular operator*(modular a, const modular &b) { return a *= b; } friend modular operator/(modular a, const modular &b) { return a /= b; } }; using mint = modular<int(1e9 + 7)>; int main() { cin.tie(0)->sync_with_stdio(0); int n, m = 0; cin >> n; vector<vector<int>> t(n); REP (i, n) { string s; cin >> s; t[i].resize(ssize(s) + 1); REP (j, ssize(s)) { if (s[j] == 'L') t[i][j + 1] = 0; else t[i][j + 1] = 1; } m = max(m, ssize(s)); } vector<vector<mint>> pref_lf(n), pref_rg(n), suff_lf(n), suff_rg(n); vector odp(n, vector<mint>(n)); vector<mint> all(n); REP (i, n) { int d = ssize(t[i]) - 1; vector nxt(d + 2, vector (2, d + 1)); for (int j = d - 1; j >= 0; --j) { REP (k, 2) nxt[j][k] = nxt[j + 1][k]; nxt[j][t[i][j + 1]] = j + 1; } vector dp(d + 2, vector<mint>(m + 2)); dp[0][0] = 1; pref_lf[i].resize(m + 2); pref_rg[i].resize(m + 2); REP (j, d + 1) { if (j) all[i] += dp[j][0]; REP (k, m + 1) { if (nxt[j][0] == d + 1) { pref_lf[i][k + 1] += dp[j][k]; } else { dp[nxt[j][0]][k + 1] += dp[j][k]; } if (k) { if (nxt[j][1] == d + 1) { pref_rg[i][k - 1] += dp[j][k]; } else { dp[nxt[j][1]][k - 1] += dp[j][k]; } } } } vector pd(d + 2, vector<mint>(m + 2)); for (int j = d; j; --j) { pd[j][0] = 1; REP (k, m + 1) { pd[j][k] += pd[nxt[j][0]][k + 1]; if (k) pd[j][k] += pd[nxt[j][1]][k - 1]; } } suff_lf[i] = pd[nxt[0][0]]; suff_rg[i] = pd[nxt[0][1]]; } REP (i, n) debug(i, pref_lf[i]); REP (i, n) debug(i, suff_lf[i]); REP (i, n) debug(i, pref_rg[i]); REP (i, n) debug(i, suff_rg[i]); REP (i, n) { REP (j, n) { odp[i][j] += all[i]; REP (k, m + 2) { odp[i][j] += pref_lf[i][k] * suff_lf[j][k] + pref_rg[i][k] * suff_rg[j][k]; } } } REP (i, n) { REP (j, n) { if (j) cout << ' '; cout << odp[i][j]; } cout << '\n'; } } |