// {{{ #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define each(...) for (auto& __VA_ARGS__) #define rep(i, b, e) for (int i = (b); i <= (e); i++) #define rev(i, b, e) for (int i = (e); i >= (b); i--) #define mp make_pair #define mt make_tuple #define x first #define y second #define pb push_back #define all(x) (x).begin(), (x).end() #ifdef SPONGE #define dbg(f, ...) fprintf(stderr, "\033[1;33m" f "\033[0m", ##__VA_ARGS__) #else #define dbg(...) 1 #endif #define tC template<class using namespace std; tC T> using V = vector<T>; tC T1, class T2> using P = pair<T1,T2>; tC T, class C=greater<T>> using PQ = priority_queue<T,V<T>,C>; using ll = long long; using ld = long double; using ul = unsigned long long; using pii = P<int,int>; using pll = P<ll,ll>; using pld = P<ld,ld>; using vi = V<int>; using vll = V<ll>; using vld = V<ld>; using vb = V<bool>; using vs = V<string>; using vpii = V<pii>; using vpll = V<pll>; using vpld = V<pld>; tC T> int sz(const T& a) { return (int)a.size(); } tC T> bool amin(T& a, T b) { return b < a ? a = b, 1 : 0; } tC T> bool amax(T& a, T b) { return b > a ? a = b, 1 : 0; } const size_t rseed = 4488; // chrono::high_resolution_clock::now().time_since_epoch().count(); mt19937 rnd(rseed); tC T> T rand(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); } const int oo = 1e9 + 1; const ll OO = 1e18 + 1; namespace { void solve(); } // }}} int main() { int t = 1; // scanf("%d", &t); rep(i, 1, t) { // printf("Case #%d: ", i); solve(); } } namespace { const int N = 611; const int M = 1e9 + 7; void dodaj(int& a, int b) { a = (a + b) % M; } int n, dl[N]; char str[N][N]; int nastL[N], nastP[N]; int dp[N][N], ile[N], konL[N][N], konP[N][N], poczL[N][N], poczP[N][N]; bool sameP[N]; void policz(int s) { int pierL = -1, ostL = -1, pierP = -1, ostP = -1; rep(i, 1, dl[s]) { if (str[s][i] == 'L') { if (pierL == -1) pierL = i; ostL = i; } else { if (pierP == -1) pierP = i; ostP = i; } } nastL[dl[s]] = -1; nastP[dl[s]] = -1; rev(i, 1, dl[s] - 1) { if (str[s][i + 1] == 'L') nastL[i] = i + 1; else nastL[i] = nastL[i + 1]; if (str[s][i + 1] == 'P') nastP[i] = i + 1; else nastP[i] = nastP[i + 1]; } rep(i, 0, dl[s]) rep(j, 0, dl[s]) dp[i][j] = 0; rep(i, 1, dl[s]) { if (i == pierL) dp[i][1] = 1; if (str[s][i] == 'P') dodaj(ile[s], dp[i][0]); if (nastL[i] != -1) { rep(b, 0, dl[s]) dodaj(dp[nastL[i]][b + 1], dp[i][b]); } else { rep(b, 0, dl[s]) dodaj(konL[s][b], dp[i][b]); } if (nastP[i] != -1) { rep(b, 1, dl[s]) dodaj(dp[nastP[i]][b - 1], dp[i][b]); } else { rep(b, 0, dl[s]) dodaj(konP[s][b], dp[i][b]); } } rep(i, 0, dl[s]) rep(j, 0, dl[s]) dp[i][j] = 0; rev(i, 1, dl[s]) { if (str[s][i] == 'P') dodaj(dp[i][1], 1); if (nastL[i] != -1) { if (str[s][i] == 'L') { rep(b, 1, dl[s]) dodaj(dp[i][b - 1], dp[nastL[i]][b]); } else { rep(b, 0, dl[s]) dodaj(dp[i][b + 1], dp[nastL[i]][b]); } } if (nastP[i] != -1) { if (str[s][i] == 'L') { rep(b, 1, dl[s]) dodaj(dp[i][b - 1], dp[nastP[i]][b]); } else { rep(b, 0, dl[s]) dodaj(dp[i][b + 1], dp[nastP[i]][b]); } } } if (pierL != -1) { rep(b, 0, dl[s]) poczL[s][b] = dp[pierL][b]; } else { sameP[s] = true; } if (pierP != -1) { rep(b, 0, dl[s]) poczP[s][b] = dp[pierP][b]; } } void debuguj(int s) { dbg("NAPIS: %s\n", &str[s][1]); dbg("ile: "); dbg("%d ", ile[s]); dbg("\n"); dbg("konL: "); rep(b, 0, dl[s]) dbg("%d ", konL[s][b]); dbg("\n"); dbg("konP: "); rep(b, 0, dl[s]) dbg("%d ", konP[s][b]); dbg("\n"); dbg("poczL: "); rep(b, 0, dl[s]) dbg("%d ", poczL[s][b]); dbg("\n"); dbg("poczP: "); rep(b, 0, dl[s]) dbg("%d ", poczP[s][b]); dbg("\n"); } void solve() { scanf("%d", &n); // n = 600; rep(i, 1, n) { scanf("%s", &str[i][1]); dl[i] = strlen(&str[i][1]); /* dl[i] = 600; rep(j, 1, dl[i]) { if (rand(0, 1)) str[i][j] = 'L'; else str[i][j] = 'P'; } */ policz(i); //debuguj(i); } rep(i, 1, n) { rep(j, 1, n) { if (sameP[i]) { printf("%d ", ile[j]); } else { int odp = ile[i]; int xd = min(dl[i], dl[j]); rep(b, 0, xd) { dodaj(odp, ll(konL[i][b]) * poczL[j][b] % M); dodaj(odp, ll(konP[i][b]) * poczP[j][b] % M); } printf("%d ", odp); } } printf("\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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | // {{{ #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define each(...) for (auto& __VA_ARGS__) #define rep(i, b, e) for (int i = (b); i <= (e); i++) #define rev(i, b, e) for (int i = (e); i >= (b); i--) #define mp make_pair #define mt make_tuple #define x first #define y second #define pb push_back #define all(x) (x).begin(), (x).end() #ifdef SPONGE #define dbg(f, ...) fprintf(stderr, "\033[1;33m" f "\033[0m", ##__VA_ARGS__) #else #define dbg(...) 1 #endif #define tC template<class using namespace std; tC T> using V = vector<T>; tC T1, class T2> using P = pair<T1,T2>; tC T, class C=greater<T>> using PQ = priority_queue<T,V<T>,C>; using ll = long long; using ld = long double; using ul = unsigned long long; using pii = P<int,int>; using pll = P<ll,ll>; using pld = P<ld,ld>; using vi = V<int>; using vll = V<ll>; using vld = V<ld>; using vb = V<bool>; using vs = V<string>; using vpii = V<pii>; using vpll = V<pll>; using vpld = V<pld>; tC T> int sz(const T& a) { return (int)a.size(); } tC T> bool amin(T& a, T b) { return b < a ? a = b, 1 : 0; } tC T> bool amax(T& a, T b) { return b > a ? a = b, 1 : 0; } const size_t rseed = 4488; // chrono::high_resolution_clock::now().time_since_epoch().count(); mt19937 rnd(rseed); tC T> T rand(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); } const int oo = 1e9 + 1; const ll OO = 1e18 + 1; namespace { void solve(); } // }}} int main() { int t = 1; // scanf("%d", &t); rep(i, 1, t) { // printf("Case #%d: ", i); solve(); } } namespace { const int N = 611; const int M = 1e9 + 7; void dodaj(int& a, int b) { a = (a + b) % M; } int n, dl[N]; char str[N][N]; int nastL[N], nastP[N]; int dp[N][N], ile[N], konL[N][N], konP[N][N], poczL[N][N], poczP[N][N]; bool sameP[N]; void policz(int s) { int pierL = -1, ostL = -1, pierP = -1, ostP = -1; rep(i, 1, dl[s]) { if (str[s][i] == 'L') { if (pierL == -1) pierL = i; ostL = i; } else { if (pierP == -1) pierP = i; ostP = i; } } nastL[dl[s]] = -1; nastP[dl[s]] = -1; rev(i, 1, dl[s] - 1) { if (str[s][i + 1] == 'L') nastL[i] = i + 1; else nastL[i] = nastL[i + 1]; if (str[s][i + 1] == 'P') nastP[i] = i + 1; else nastP[i] = nastP[i + 1]; } rep(i, 0, dl[s]) rep(j, 0, dl[s]) dp[i][j] = 0; rep(i, 1, dl[s]) { if (i == pierL) dp[i][1] = 1; if (str[s][i] == 'P') dodaj(ile[s], dp[i][0]); if (nastL[i] != -1) { rep(b, 0, dl[s]) dodaj(dp[nastL[i]][b + 1], dp[i][b]); } else { rep(b, 0, dl[s]) dodaj(konL[s][b], dp[i][b]); } if (nastP[i] != -1) { rep(b, 1, dl[s]) dodaj(dp[nastP[i]][b - 1], dp[i][b]); } else { rep(b, 0, dl[s]) dodaj(konP[s][b], dp[i][b]); } } rep(i, 0, dl[s]) rep(j, 0, dl[s]) dp[i][j] = 0; rev(i, 1, dl[s]) { if (str[s][i] == 'P') dodaj(dp[i][1], 1); if (nastL[i] != -1) { if (str[s][i] == 'L') { rep(b, 1, dl[s]) dodaj(dp[i][b - 1], dp[nastL[i]][b]); } else { rep(b, 0, dl[s]) dodaj(dp[i][b + 1], dp[nastL[i]][b]); } } if (nastP[i] != -1) { if (str[s][i] == 'L') { rep(b, 1, dl[s]) dodaj(dp[i][b - 1], dp[nastP[i]][b]); } else { rep(b, 0, dl[s]) dodaj(dp[i][b + 1], dp[nastP[i]][b]); } } } if (pierL != -1) { rep(b, 0, dl[s]) poczL[s][b] = dp[pierL][b]; } else { sameP[s] = true; } if (pierP != -1) { rep(b, 0, dl[s]) poczP[s][b] = dp[pierP][b]; } } void debuguj(int s) { dbg("NAPIS: %s\n", &str[s][1]); dbg("ile: "); dbg("%d ", ile[s]); dbg("\n"); dbg("konL: "); rep(b, 0, dl[s]) dbg("%d ", konL[s][b]); dbg("\n"); dbg("konP: "); rep(b, 0, dl[s]) dbg("%d ", konP[s][b]); dbg("\n"); dbg("poczL: "); rep(b, 0, dl[s]) dbg("%d ", poczL[s][b]); dbg("\n"); dbg("poczP: "); rep(b, 0, dl[s]) dbg("%d ", poczP[s][b]); dbg("\n"); } void solve() { scanf("%d", &n); // n = 600; rep(i, 1, n) { scanf("%s", &str[i][1]); dl[i] = strlen(&str[i][1]); /* dl[i] = 600; rep(j, 1, dl[i]) { if (rand(0, 1)) str[i][j] = 'L'; else str[i][j] = 'P'; } */ policz(i); //debuguj(i); } rep(i, 1, n) { rep(j, 1, n) { if (sameP[i]) { printf("%d ", ile[j]); } else { int odp = ile[i]; int xd = min(dl[i], dl[j]); rep(b, 0, xd) { dodaj(odp, ll(konL[i][b]) * poczL[j][b] % M); dodaj(odp, ll(konP[i][b]) * poczP[j][b] % M); } printf("%d ", odp); } } printf("\n"); } } } |