#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; i >= a; i--) #define cat(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; template<class T> T power(T a, int64_t b) { T res = 1; for (; b; b /= 2, a *= a) { if (b & 1) { res *= a; } } return res; } template<int MOD> struct Mint { static_assert(MOD > 0, "MOD must be positive"); private: int v; public: operator int() { return v; } Mint (int64_t x = 0) : v(x % MOD) { if (v < 0) { v += MOD; } } Mint& operator+= (const Mint& o) { v += o.v; if (v >= MOD) { v -= MOD; } return *this; } Mint& operator-= (const Mint& o) { v -= o.v; if (v < 0) { v += MOD; } return *this; } Mint& operator*= (const Mint& o) { v = (int64_t) v * o.v % MOD; return *this; } Mint& operator/= (const Mint& o) { v = (int64_t) v * int(power(o, MOD - 2)) % MOD; return *this; } friend Mint operator+ (Mint lhs, const Mint& rhs) { return lhs += rhs; } friend Mint operator- (Mint lhs, const Mint& rhs) { return lhs -= rhs; } friend Mint operator* (Mint lhs, const Mint& rhs) { return lhs *= rhs; } friend Mint operator/ (Mint lhs, const Mint& rhs) { return lhs /= rhs; } friend bool operator== (const Mint& lhs, const Mint& rhs) { return lhs.v == rhs.v; } friend bool operator!= (const Mint& lhs, const Mint& rhs) { return lhs.v != rhs.v; } friend std::ostream& operator<< (std::ostream& os, const Mint& num) { os << num.v; return os; } friend std::istream& operator>> (std::istream& is, Mint& num) { int64_t x; is >> x; num = Mint(x); return is; } }; using Z = Mint<int(1e9 + 7)>; const int N = 606; Z dp[N][N], res[N], last1[N][N], sum1[N][N], first2[N][2][N]; int m, n, a[N], jump[N][2], L[N][2], R[N][2]; char s[N]; int main() { cin.tie(0)->sync_with_stdio(0); cin >> m; rep(k, 1, m) { cin >> (s + 1); n = strlen(s + 1); rep(i, 1, n) a[i] = s[i] == 'L'; L[k][0] = n + 1; L[k][1] = n + 1; rep(i, 1, n) { L[k][a[i]] = min(L[k][a[i]], i); R[k][a[i]] = max(R[k][a[i]], i); } jump[n][0] = n + 1; jump[n][1] = n + 1; per(i, 0, n - 1) { jump[i][0] = jump[i + 1][0]; jump[i][1] = jump[i + 1][1]; jump[i][a[i + 1]] = i + 1; } memset(dp, 0, sizeof dp); dp[0][0] = 1; rep(i, 0, n) { rep(b, 0, i) { dp[jump[i][1]][b + 1] += dp[i][b]; if (b > 0) { dp[jump[i][0]][b - 1] += dp[i][b]; } } } rep(i, 1, n) res[k] += dp[i][0]; rep(b, 0, n) last1[k][b] = dp[n][b]; rep(i, R[k][!a[n]], n) rep(b, 0, n) { sum1[k][b] += dp[i][b]; } memset(dp, 0, sizeof dp); per(i, 1, n) { if (a[i] == 0) dp[i][1] = 1; rep(b, a[i], n - i + 1) { if (a[i] == 0) { dp[i][b + 1] += dp[jump[i][0]][b] + dp[jump[i][1]][b]; } if (a[i] == 1) { dp[i][b - 1] += dp[jump[i][0]][b] + dp[jump[i][1]][b]; } } } rep(b, 0, n) { first2[k][0][b] += dp[L[k][0]][b]; first2[k][1][b] += dp[L[k][1]][b]; } } rep(i, 1, m) { int r = R[i][1] > R[i][0]; rep(j, 1, m) { Z ans = res[i]; rep(b, 0, 600) ans += last1[i][b] * first2[j][r][b]; rep(b, 0, 600) ans += sum1[i][b] * first2[j][!r][b]; cout << ans << " "; } 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 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <= b; i++) #define per(i, a, b) for (int i = b; i >= a; i--) #define cat(x) cout << #x << ": " << x << endl using namespace std; using ll = long long; template<class T> T power(T a, int64_t b) { T res = 1; for (; b; b /= 2, a *= a) { if (b & 1) { res *= a; } } return res; } template<int MOD> struct Mint { static_assert(MOD > 0, "MOD must be positive"); private: int v; public: operator int() { return v; } Mint (int64_t x = 0) : v(x % MOD) { if (v < 0) { v += MOD; } } Mint& operator+= (const Mint& o) { v += o.v; if (v >= MOD) { v -= MOD; } return *this; } Mint& operator-= (const Mint& o) { v -= o.v; if (v < 0) { v += MOD; } return *this; } Mint& operator*= (const Mint& o) { v = (int64_t) v * o.v % MOD; return *this; } Mint& operator/= (const Mint& o) { v = (int64_t) v * int(power(o, MOD - 2)) % MOD; return *this; } friend Mint operator+ (Mint lhs, const Mint& rhs) { return lhs += rhs; } friend Mint operator- (Mint lhs, const Mint& rhs) { return lhs -= rhs; } friend Mint operator* (Mint lhs, const Mint& rhs) { return lhs *= rhs; } friend Mint operator/ (Mint lhs, const Mint& rhs) { return lhs /= rhs; } friend bool operator== (const Mint& lhs, const Mint& rhs) { return lhs.v == rhs.v; } friend bool operator!= (const Mint& lhs, const Mint& rhs) { return lhs.v != rhs.v; } friend std::ostream& operator<< (std::ostream& os, const Mint& num) { os << num.v; return os; } friend std::istream& operator>> (std::istream& is, Mint& num) { int64_t x; is >> x; num = Mint(x); return is; } }; using Z = Mint<int(1e9 + 7)>; const int N = 606; Z dp[N][N], res[N], last1[N][N], sum1[N][N], first2[N][2][N]; int m, n, a[N], jump[N][2], L[N][2], R[N][2]; char s[N]; int main() { cin.tie(0)->sync_with_stdio(0); cin >> m; rep(k, 1, m) { cin >> (s + 1); n = strlen(s + 1); rep(i, 1, n) a[i] = s[i] == 'L'; L[k][0] = n + 1; L[k][1] = n + 1; rep(i, 1, n) { L[k][a[i]] = min(L[k][a[i]], i); R[k][a[i]] = max(R[k][a[i]], i); } jump[n][0] = n + 1; jump[n][1] = n + 1; per(i, 0, n - 1) { jump[i][0] = jump[i + 1][0]; jump[i][1] = jump[i + 1][1]; jump[i][a[i + 1]] = i + 1; } memset(dp, 0, sizeof dp); dp[0][0] = 1; rep(i, 0, n) { rep(b, 0, i) { dp[jump[i][1]][b + 1] += dp[i][b]; if (b > 0) { dp[jump[i][0]][b - 1] += dp[i][b]; } } } rep(i, 1, n) res[k] += dp[i][0]; rep(b, 0, n) last1[k][b] = dp[n][b]; rep(i, R[k][!a[n]], n) rep(b, 0, n) { sum1[k][b] += dp[i][b]; } memset(dp, 0, sizeof dp); per(i, 1, n) { if (a[i] == 0) dp[i][1] = 1; rep(b, a[i], n - i + 1) { if (a[i] == 0) { dp[i][b + 1] += dp[jump[i][0]][b] + dp[jump[i][1]][b]; } if (a[i] == 1) { dp[i][b - 1] += dp[jump[i][0]][b] + dp[jump[i][1]][b]; } } } rep(b, 0, n) { first2[k][0][b] += dp[L[k][0]][b]; first2[k][1][b] += dp[L[k][1]][b]; } } rep(i, 1, m) { int r = R[i][1] > R[i][0]; rep(j, 1, m) { Z ans = res[i]; rep(b, 0, 600) ans += last1[i][b] * first2[j][r][b]; rep(b, 0, 600) ans += sum1[i][b] * first2[j][!r][b]; cout << ans << " "; } cout << "\n"; } return 0; } |