#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<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif constexpr int mod = 1e9 + 7; int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } void self_add(int& a, int b) { a = add(a, b); } int mul(int a, int b) { return int(a * LL(b) % mod); } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; debug(n); vector<vector<int>> a(n); REP(i, n) { string s; cin >> s; const int d = ssize(s); vector<int> v(d); REP(j, d) { if (s[j] == 'L') v[j] = 0; else if (s[j] == 'P') v[j] = 1; else assert(false); } a[i] = v; } debug(a); int limit = 0; REP(i, n) { limit = max(limit, ssize(a[i])); } vector dp_left(n, vector (limit + 2, vector (2, 0))); vector done(n, 0); vector dp_right(n, vector (limit + 2, vector (2, 0))); { // dp_left REP(i, n) { const int d = ssize(a[i]); vector dp(d + 2, vector (3, 0)); vector new_dp(d + 2, vector (3, 0)); dp[0][0] = 1; FOR(j, 1, d) { FOR(k, 0, j) { if (a[i][j - 1] == 0) { new_dp[k][0] = 0; new_dp[k][1] = add(dp[k][1], dp[k][2]); if (k > 0) new_dp[k][2] = add(dp[k - 1][0], dp[k - 1][2]); else new_dp[k][2] = 0; if (k == 0) self_add(done[i], new_dp[k][2]); } else { new_dp[k][0] = add(dp[k][0], dp[k][2]); new_dp[k][1] = 0; new_dp[k][2] = add(dp[k + 1][1], dp[k + 1][2]); if (k == 0) self_add(done[i], new_dp[k][2]); } } swap(dp, new_dp); debug(j, dp); } FOR(k, 0, d) { REP(l, 2) { dp_left[i][k][l] = add(dp[k][l], dp[k][2]); } } dp_left[i][0][1] = 0; } } debug(dp_left); debug(done); { // dp_right vector dp(2, vector (limit + 1, vector (limit + 2, 0))); REP(i, n) { REP(c, 2) { for (auto& v : dp[c]) { fill(v.begin(), v.end(), 0); } } const int d = ssize(a[i]); for (int p = d - 1; p >= 0; --p) { FOR(sum, 0, d) { REP(c, 2) { if (a[i][p] == c) { int cp_sum = sum; if (c == 0) { ++cp_sum; } else { --cp_sum; } if (cp_sum >= 0) { self_add(dp[c][p][sum], dp[0][p + 1][cp_sum]); if (cp_sum == 0) { self_add(dp[c][p][sum], 1); } else { self_add(dp[c][p][sum], dp[1][p + 1][cp_sum]); } } } else { dp[c][p][sum] = dp[c][p + 1][sum]; } } } } debug(i, dp); REP(c, 2) { FOR(sum, 0, d) { dp_right[i][sum][c] = dp[c][0][sum]; } } } } debug(dp_right); REP(i, n) { REP(j, n) { int ans = 0; FOR(k, 0, limit) { REP(l, 2) { ans = add(ans, mul(dp_left[i][k][l], dp_right[j][k][l])); } } self_add(ans, done[i]); cout << ans << ' '; } 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 | #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<<$<<"; "),...);}(x),cerr<<'\n' #else #define debug(...) {} #endif constexpr int mod = 1e9 + 7; int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } void self_add(int& a, int b) { a = add(a, b); } int mul(int a, int b) { return int(a * LL(b) % mod); } int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; debug(n); vector<vector<int>> a(n); REP(i, n) { string s; cin >> s; const int d = ssize(s); vector<int> v(d); REP(j, d) { if (s[j] == 'L') v[j] = 0; else if (s[j] == 'P') v[j] = 1; else assert(false); } a[i] = v; } debug(a); int limit = 0; REP(i, n) { limit = max(limit, ssize(a[i])); } vector dp_left(n, vector (limit + 2, vector (2, 0))); vector done(n, 0); vector dp_right(n, vector (limit + 2, vector (2, 0))); { // dp_left REP(i, n) { const int d = ssize(a[i]); vector dp(d + 2, vector (3, 0)); vector new_dp(d + 2, vector (3, 0)); dp[0][0] = 1; FOR(j, 1, d) { FOR(k, 0, j) { if (a[i][j - 1] == 0) { new_dp[k][0] = 0; new_dp[k][1] = add(dp[k][1], dp[k][2]); if (k > 0) new_dp[k][2] = add(dp[k - 1][0], dp[k - 1][2]); else new_dp[k][2] = 0; if (k == 0) self_add(done[i], new_dp[k][2]); } else { new_dp[k][0] = add(dp[k][0], dp[k][2]); new_dp[k][1] = 0; new_dp[k][2] = add(dp[k + 1][1], dp[k + 1][2]); if (k == 0) self_add(done[i], new_dp[k][2]); } } swap(dp, new_dp); debug(j, dp); } FOR(k, 0, d) { REP(l, 2) { dp_left[i][k][l] = add(dp[k][l], dp[k][2]); } } dp_left[i][0][1] = 0; } } debug(dp_left); debug(done); { // dp_right vector dp(2, vector (limit + 1, vector (limit + 2, 0))); REP(i, n) { REP(c, 2) { for (auto& v : dp[c]) { fill(v.begin(), v.end(), 0); } } const int d = ssize(a[i]); for (int p = d - 1; p >= 0; --p) { FOR(sum, 0, d) { REP(c, 2) { if (a[i][p] == c) { int cp_sum = sum; if (c == 0) { ++cp_sum; } else { --cp_sum; } if (cp_sum >= 0) { self_add(dp[c][p][sum], dp[0][p + 1][cp_sum]); if (cp_sum == 0) { self_add(dp[c][p][sum], 1); } else { self_add(dp[c][p][sum], dp[1][p + 1][cp_sum]); } } } else { dp[c][p][sum] = dp[c][p + 1][sum]; } } } } debug(i, dp); REP(c, 2) { FOR(sum, 0, d) { dp_right[i][sum][c] = dp[c][0][sum]; } } } } debug(dp_right); REP(i, n) { REP(j, n) { int ans = 0; FOR(k, 0, limit) { REP(l, 2) { ans = add(ans, mul(dp_left[i][k][l], dp_right[j][k][l])); } } self_add(ans, done[i]); cout << ans << ' '; } cout << '\n'; } } |