#include <bits/stdc++.h> using namespace std; using ll = long long; template<ll MOD> struct Zp { ll a; Zp() : a(0) {} Zp(ll _a) { a = _a % MOD; if (a < 0) a += MOD; } }; template<ll MOD> Zp<MOD> operator+(const Zp<MOD> &l, const Zp<MOD> &r) { return Zp<MOD>(l.a + r.a); } template<ll MOD> Zp<MOD> &operator+=(Zp<MOD> &l, const Zp<MOD> &r) { l.a += r.a; if (l.a >= MOD) l.a -= MOD; return l; } template<ll MOD> Zp<MOD> operator*(const Zp<MOD> &l, const Zp<MOD> &r) { return Zp<MOD>(l.a * r.a); } const int MAXN = 610; const ll MOD = 1000000007; using F = Zp<MOD>; char S[MAXN]; int Slen[MAXN]; int nextL[MAXN]; int nextP[MAXN]; F num_paths[MAXN][MAXN]; F left_paths[MAXN]; F leftL[MAXN][MAXN]; F leftP[MAXN][MAXN]; F rightL[MAXN][MAXN]; F rightP[MAXN][MAXN]; void calc_next(int id) { int l = Slen[id]; for (int i = 0; i <= l; i++){ nextL[i] = -1; nextP[i] = -1; } int lastL = 0; int lastP = 0; for (int i = 1; i <= l; i++) { if (S[i] == 'L') { for (int j = lastL; j < i; j++) { nextL[j] = i; } lastL = i; } else { for (int j = lastP; j < i; j++) { nextP[j] = i; } lastP = i; } } } void calc_forward(int id) { int l = Slen[id]; for (int i = 0; i <= l; i++) { for (int j = 0; j <= l; j++) { num_paths[i][j] = F(0); } } num_paths[0][0] = F(1); for (int i = 0; i < l; i++) { for (int j = 0; j <= l; j++) { if (j < l && nextL[i] != -1) { num_paths[nextL[i]][j + 1] += num_paths[i][j]; } if (j > 0 && nextP[i] != -1) { num_paths[nextP[i]][j - 1] += num_paths[i][j]; } } } } void calc_backwards(int id) { int l = Slen[id]; for (int i = l; i >= 1; i--) { for (int j = 0; j <= l; j++) { num_paths[i][j] = F(j == 0 ? 1 : 0); if (j < l && nextL[i] != -1) { num_paths[i][j] += num_paths[nextL[i]][j + 1]; } if (j > 0 && nextP[i] != -1) { num_paths[i][j] += num_paths[nextP[i]][j - 1]; } } } } void process_string(int id) { int l = Slen[id]; calc_next(id); calc_forward(id); for (int i = 0; i <= l; i++) { if (nextL[i] == -1) { for (int j = 0; j <= l; j++) { leftL[id][j] += num_paths[i][j]; } } if (nextP[i] == -1) { for (int j = 0; j <= l; j++) { leftP[id][j] += num_paths[i][j]; } } left_paths[id] += num_paths[i][0]; } calc_backwards(id); int firstL = nextL[0]; int firstP = nextP[0]; for (int j = 0; j <= l; j++) { rightL[id][j] = firstL != -1 ? num_paths[firstL][j] : F(0); rightP[id][j] = firstP != -1 ? num_paths[firstP][j] : F(0); } } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", S + 1); Slen[i] = strlen(S + 1); process_string(i); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int l1 = Slen[i]; int l2 = Slen[j]; F out = left_paths[i] + F(-1); for (int b = 0; b <= min(l1, l2); b++) { if (b < min(l1, l2)) { out += leftL[i][b] * rightL[j][b + 1]; } if (b > 0) { out += leftP[i][b] * rightP[j][b - 1]; } } printf("%lld ", out.a); } puts(""); } }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long; template<ll MOD> struct Zp { ll a; Zp() : a(0) {} Zp(ll _a) { a = _a % MOD; if (a < 0) a += MOD; } }; template<ll MOD> Zp<MOD> operator+(const Zp<MOD> &l, const Zp<MOD> &r) { return Zp<MOD>(l.a + r.a); } template<ll MOD> Zp<MOD> &operator+=(Zp<MOD> &l, const Zp<MOD> &r) { l.a += r.a; if (l.a >= MOD) l.a -= MOD; return l; } template<ll MOD> Zp<MOD> operator*(const Zp<MOD> &l, const Zp<MOD> &r) { return Zp<MOD>(l.a * r.a); } const int MAXN = 610; const ll MOD = 1000000007; using F = Zp<MOD>; char S[MAXN]; int Slen[MAXN]; int nextL[MAXN]; int nextP[MAXN]; F num_paths[MAXN][MAXN]; F left_paths[MAXN]; F leftL[MAXN][MAXN]; F leftP[MAXN][MAXN]; F rightL[MAXN][MAXN]; F rightP[MAXN][MAXN]; void calc_next(int id) { int l = Slen[id]; for (int i = 0; i <= l; i++){ nextL[i] = -1; nextP[i] = -1; } int lastL = 0; int lastP = 0; for (int i = 1; i <= l; i++) { if (S[i] == 'L') { for (int j = lastL; j < i; j++) { nextL[j] = i; } lastL = i; } else { for (int j = lastP; j < i; j++) { nextP[j] = i; } lastP = i; } } } void calc_forward(int id) { int l = Slen[id]; for (int i = 0; i <= l; i++) { for (int j = 0; j <= l; j++) { num_paths[i][j] = F(0); } } num_paths[0][0] = F(1); for (int i = 0; i < l; i++) { for (int j = 0; j <= l; j++) { if (j < l && nextL[i] != -1) { num_paths[nextL[i]][j + 1] += num_paths[i][j]; } if (j > 0 && nextP[i] != -1) { num_paths[nextP[i]][j - 1] += num_paths[i][j]; } } } } void calc_backwards(int id) { int l = Slen[id]; for (int i = l; i >= 1; i--) { for (int j = 0; j <= l; j++) { num_paths[i][j] = F(j == 0 ? 1 : 0); if (j < l && nextL[i] != -1) { num_paths[i][j] += num_paths[nextL[i]][j + 1]; } if (j > 0 && nextP[i] != -1) { num_paths[i][j] += num_paths[nextP[i]][j - 1]; } } } } void process_string(int id) { int l = Slen[id]; calc_next(id); calc_forward(id); for (int i = 0; i <= l; i++) { if (nextL[i] == -1) { for (int j = 0; j <= l; j++) { leftL[id][j] += num_paths[i][j]; } } if (nextP[i] == -1) { for (int j = 0; j <= l; j++) { leftP[id][j] += num_paths[i][j]; } } left_paths[id] += num_paths[i][0]; } calc_backwards(id); int firstL = nextL[0]; int firstP = nextP[0]; for (int j = 0; j <= l; j++) { rightL[id][j] = firstL != -1 ? num_paths[firstL][j] : F(0); rightP[id][j] = firstP != -1 ? num_paths[firstP][j] : F(0); } } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", S + 1); Slen[i] = strlen(S + 1); process_string(i); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int l1 = Slen[i]; int l2 = Slen[j]; F out = left_paths[i] + F(-1); for (int b = 0; b <= min(l1, l2); b++) { if (b < min(l1, l2)) { out += leftL[i][b] * rightL[j][b + 1]; } if (b > 0) { out += leftP[i][b] * rightP[j][b - 1]; } } printf("%lld ", out.a); } puts(""); } } |