#include <bits/stdc++.h> using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef long double ldbl; typedef pair<int, int> pii; typedef pair<uint, uint> puu; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<uint> vu; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<puu> vpuu; typedef vector<pll> vpll; typedef vector<pull> vpull; typedef vector<string> vstr; typedef vector<double> vdbl; typedef vector<ldbl> vldbl; #define pb push_back #define ppb pop_back #define pfr push_front #define ppfr pop_front #define emp emplace #define empb emplace_back #define be begin #define rbe rbegin #define all(x) (x).be(), (x).end() #define rall(x) (x).rbe(), (x).rend() #define fir first #define sec second #define mkp make_pair #define brif(cond) if (cond) break #define ctif(cond) if (cond) continue #define retif(cond) if (cond) return void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);} template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;} template<typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a/b; a %= b; swap(a, b); x0 -= q*x1; swap(x0, x1); y0 -= q*y1; swap(y0, y1); } x = x0; y = y0; return a; } int ctz(uint x) {return __builtin_ctz(x);} int ctzll(ull x) {return __builtin_ctzll(x);} int clz(uint x) {return __builtin_clz(x);} int clzll(ull x) {return __builtin_clzll(x);} int popcnt(uint x) {return __builtin_popcount(x);} int popcntll(ull x) {return __builtin_popcountll(x);} int bsr(uint x) {return 31^clz(x);} int bsrll(ull x) {return 63^clzll(x);} #define MOD 1000000007 uint add(uint a, uint b) { return a+b < MOD ? a+b : a+b-MOD; } uint sub(uint a, uint b) { return a < b ? a+MOD-b : a-b; } uint mul(uint a, uint b) { return (ull)a*b%MOD; } uint sqr(uint a) { return mul(a, a); } uint modpow(uint b, uint e) { uint r = 1; for (; e; e >>= 1) { if (e&1) r = mul(r, b); b = sqr(b); } return r; } uint modpowll(uint b, ull e) { uint r = 1; for (; e; e >>= 1) { if (e&1) r = mul(r, b); b = sqr(b); } return r; } uint modinv(uint n) { return modpow(n, MOD-2); } uint moddiv(uint n, uint d) { return mul(n, modinv(d)); } void uadd(uint &a, uint b) { a = add(a, b); } void usub(uint &a, uint b) { a = sub(a, b); } void umul(uint &a, uint b) { a = mul(a, b); } #define MX 608 struct A { void read(); int len, cl, cp; uint tot; char s[MX]; int nxtl[MX], nxtp[MX]; uint prel[MX], prep[MX]; uint sufl[MX], sufp[MX]; }; A a[MX]; uint dp[MX][MX]; void clear_dp(int m) { for (int i = 0; i <= m; ++i) fill_n(dp[i], m+1, 0); } void A::read() { ull sum = 0; cin >> s; len = strlen(s); int il = len, ip = len; for (int i = len-1; i >= 0; --i) { if (s[i] == 'L') { il = i; ++cl; } else { ip = i; ++cp; } nxtl[i] = il; nxtp[i] = ip; } /// pref: clear_dp(len); dp[0][0] = 1; for (int i = 0; i < len; ++i) { int bmx = min(i, cl); if (nxtl[i] < len) { int dst = nxtl[i]+1; for (int b = 0; b <= bmx; ++b) uadd(dp[dst][b+1], dp[i][b]); } else { for (int b = 0; b <= bmx; ++b) uadd(prel[b], dp[i][b]); } if (nxtp[i] < len) { int dst = nxtp[i]+1; for (int b = 1; b <= bmx; ++b) uadd(dp[dst][b-1], dp[i][b]); } else { for (int b = 0; b <= bmx; ++b) uadd(prep[b], dp[i][b]); } sum += dp[i+1][0]; /// nonempty } tot = sum%MOD; for (int b = 0; b <= len; ++b) { uadd(prel[b], dp[len][b]); uadd(prep[b], dp[len][b]); } /// suff: clear_dp(len); dp[len][0] = 1; for (int i = len-1; i > 0; --i) { int bmx = min(len-i, cp); if (nxtl[i] < len) { int src = nxtl[i]+1; for (int b = 0; b <= bmx; ++b) uadd(dp[i][b], dp[src][b+1]); } if (nxtp[i] < len) { int src = nxtp[i]+1; for (int b = 1; b <= bmx; ++b) uadd(dp[i][b], dp[src][b-1]); } uadd(dp[i][0], 1); } if (nxtl[0] < len) { ///for (int b = 0; b < len; ++b) sufl[b] = dp[nxtl[0]+1][b+1]; if (nxtl[0] < len) copy_n(dp[nxtl[0]+1]+1, cp, sufl); } if (nxtp[0] < len) { ///for (int b = 1; b <= len; ++b) sufp[b] = dp[nxtp[0]+1][b-1]; if (nxtp[0] < len) copy_n(dp[nxtp[0]+1], cp, sufp+1); } } uint f(const A &pre, const A &suf) { ull res = pre.tot; for (int i = 0; i <= min(pre.cl, suf.cp); ++i) { res += mul(pre.prel[i], suf.sufl[i]); res += mul(pre.prep[i], suf.sufp[i]); } return res%MOD; } int main() { canhazfast(); int n; cin >> n; for (int i = 0; i < n; ++i) a[i].read(); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) cout << f(a[i], a[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 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 | #include <bits/stdc++.h> using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef long double ldbl; typedef pair<int, int> pii; typedef pair<uint, uint> puu; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<uint> vu; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<puu> vpuu; typedef vector<pll> vpll; typedef vector<pull> vpull; typedef vector<string> vstr; typedef vector<double> vdbl; typedef vector<ldbl> vldbl; #define pb push_back #define ppb pop_back #define pfr push_front #define ppfr pop_front #define emp emplace #define empb emplace_back #define be begin #define rbe rbegin #define all(x) (x).be(), (x).end() #define rall(x) (x).rbe(), (x).rend() #define fir first #define sec second #define mkp make_pair #define brif(cond) if (cond) break #define ctif(cond) if (cond) continue #define retif(cond) if (cond) return void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);} template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;} template<typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a/b; a %= b; swap(a, b); x0 -= q*x1; swap(x0, x1); y0 -= q*y1; swap(y0, y1); } x = x0; y = y0; return a; } int ctz(uint x) {return __builtin_ctz(x);} int ctzll(ull x) {return __builtin_ctzll(x);} int clz(uint x) {return __builtin_clz(x);} int clzll(ull x) {return __builtin_clzll(x);} int popcnt(uint x) {return __builtin_popcount(x);} int popcntll(ull x) {return __builtin_popcountll(x);} int bsr(uint x) {return 31^clz(x);} int bsrll(ull x) {return 63^clzll(x);} #define MOD 1000000007 uint add(uint a, uint b) { return a+b < MOD ? a+b : a+b-MOD; } uint sub(uint a, uint b) { return a < b ? a+MOD-b : a-b; } uint mul(uint a, uint b) { return (ull)a*b%MOD; } uint sqr(uint a) { return mul(a, a); } uint modpow(uint b, uint e) { uint r = 1; for (; e; e >>= 1) { if (e&1) r = mul(r, b); b = sqr(b); } return r; } uint modpowll(uint b, ull e) { uint r = 1; for (; e; e >>= 1) { if (e&1) r = mul(r, b); b = sqr(b); } return r; } uint modinv(uint n) { return modpow(n, MOD-2); } uint moddiv(uint n, uint d) { return mul(n, modinv(d)); } void uadd(uint &a, uint b) { a = add(a, b); } void usub(uint &a, uint b) { a = sub(a, b); } void umul(uint &a, uint b) { a = mul(a, b); } #define MX 608 struct A { void read(); int len, cl, cp; uint tot; char s[MX]; int nxtl[MX], nxtp[MX]; uint prel[MX], prep[MX]; uint sufl[MX], sufp[MX]; }; A a[MX]; uint dp[MX][MX]; void clear_dp(int m) { for (int i = 0; i <= m; ++i) fill_n(dp[i], m+1, 0); } void A::read() { ull sum = 0; cin >> s; len = strlen(s); int il = len, ip = len; for (int i = len-1; i >= 0; --i) { if (s[i] == 'L') { il = i; ++cl; } else { ip = i; ++cp; } nxtl[i] = il; nxtp[i] = ip; } /// pref: clear_dp(len); dp[0][0] = 1; for (int i = 0; i < len; ++i) { int bmx = min(i, cl); if (nxtl[i] < len) { int dst = nxtl[i]+1; for (int b = 0; b <= bmx; ++b) uadd(dp[dst][b+1], dp[i][b]); } else { for (int b = 0; b <= bmx; ++b) uadd(prel[b], dp[i][b]); } if (nxtp[i] < len) { int dst = nxtp[i]+1; for (int b = 1; b <= bmx; ++b) uadd(dp[dst][b-1], dp[i][b]); } else { for (int b = 0; b <= bmx; ++b) uadd(prep[b], dp[i][b]); } sum += dp[i+1][0]; /// nonempty } tot = sum%MOD; for (int b = 0; b <= len; ++b) { uadd(prel[b], dp[len][b]); uadd(prep[b], dp[len][b]); } /// suff: clear_dp(len); dp[len][0] = 1; for (int i = len-1; i > 0; --i) { int bmx = min(len-i, cp); if (nxtl[i] < len) { int src = nxtl[i]+1; for (int b = 0; b <= bmx; ++b) uadd(dp[i][b], dp[src][b+1]); } if (nxtp[i] < len) { int src = nxtp[i]+1; for (int b = 1; b <= bmx; ++b) uadd(dp[i][b], dp[src][b-1]); } uadd(dp[i][0], 1); } if (nxtl[0] < len) { ///for (int b = 0; b < len; ++b) sufl[b] = dp[nxtl[0]+1][b+1]; if (nxtl[0] < len) copy_n(dp[nxtl[0]+1]+1, cp, sufl); } if (nxtp[0] < len) { ///for (int b = 1; b <= len; ++b) sufp[b] = dp[nxtp[0]+1][b-1]; if (nxtp[0] < len) copy_n(dp[nxtp[0]+1], cp, sufp+1); } } uint f(const A &pre, const A &suf) { ull res = pre.tot; for (int i = 0; i <= min(pre.cl, suf.cp); ++i) { res += mul(pre.prel[i], suf.sufl[i]); res += mul(pre.prep[i], suf.sufp[i]); } return res%MOD; } int main() { canhazfast(); int n; cin >> n; for (int i = 0; i < n; ++i) a[i].read(); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) cout << f(a[i], a[j]) << ' '; cout << '\n'; } return 0; } |