#include <bits/stdc++.h> const size_t MOD = 1000000007; using namespace std; template <size_t M> class mod_t { size_t value; public: mod_t(size_t value) : value(value % MOD) {} mod_t(const mod_t& old) : value(old.value) {} mod_t(mod_t&& old) : value(move(old.value)) {} mod_t& operator=(const mod_t& rhs) { value = rhs.value; return *this; } mod_t& operator+=(mod_t rhs) { value = (value + rhs.value) % MOD; return *this; } mod_t operator+(mod_t rhs) { mod_t result{*this}; result += rhs; return result; } mod_t& operator-=(mod_t rhs) { value = (MOD + value - rhs.value) % MOD; return *this; } mod_t operator-(mod_t rhs) { mod_t result{*this}; result -= rhs; return result; } friend ostream& operator<<(ostream& stream, const mod_t& v) { stream << v.value; return stream; } }; using Seq = vector<bool>; struct TestCase { size_t N; vector<Seq> sequences; }; TestCase read_test_case(); void solve_test_case(const TestCase&); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(read_test_case()); } TestCase read_test_case() { TestCase tc; cin >> tc.N; tc.sequences.resize(tc.N); for (size_t i = 0; i < tc.N; i++) { string s; cin >> s; tc.sequences[i].resize(s.size()); for (size_t j = 0; j < s.size(); j++) tc.sequences[i][j] = s[j] == 'L'; } return tc; } template <size_t M> struct DPDump { vector<mod_t<M>> dp; vector<mod_t<M>> seen[2]; size_t seq_size = 1; DPDump(size_t size) { add_size(size); } void add_size(size_t size) { seq_size += size; dp.resize(seq_size + 1, 0); seen[0].resize(seq_size + 1, 0); seen[1].resize(seq_size + 1, 0); } }; template <size_t M> DPDump<M> calculate_dp(const Seq& seq); template <size_t M> DPDump<M> continue_dp(DPDump<M> dump, const Seq& seq); void solve_test_case(const TestCase& tc) { vector<DPDump<MOD>> dumps; for (const auto& seq : tc.sequences) dumps.push_back(calculate_dp<MOD>(seq)); for (size_t i = 0; i < tc.N; i++) { for (const auto& s2 : tc.sequences) cout << continue_dp<MOD>(dumps[i], s2).dp[0] << " "; cout << "\n"; } } template <size_t M> DPDump<M> calculate_dp(const Seq& seq) { DPDump<M> dump(0); return continue_dp(dump, seq); } template <size_t M> DPDump<M> continue_dp(DPDump<M> dump, const Seq& seq) { dump.add_size(seq.size()); for (size_t i = 0; i < seq.size(); i++) { bool x = seq[i]; vector<mod_t<M>> new_unique(dump.seq_size, 0); if (x) // L { new_unique[1] = 1; for (size_t b = 0; b < new_unique.size() - 1; b++) new_unique[b + 1] = (new_unique[b + 1] + dump.dp[b]) - dump.seen[x][b + 1]; } else // R { for (size_t b = 0; b < new_unique.size() - 1; b++) new_unique[b] += dump.dp[b + 1] - dump.seen[x][b]; } for (size_t b = 0; b < new_unique.size(); b++) { dump.dp[b] += new_unique[b]; dump.seen[x][b] += new_unique[b]; } } return dump; }
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> const size_t MOD = 1000000007; using namespace std; template <size_t M> class mod_t { size_t value; public: mod_t(size_t value) : value(value % MOD) {} mod_t(const mod_t& old) : value(old.value) {} mod_t(mod_t&& old) : value(move(old.value)) {} mod_t& operator=(const mod_t& rhs) { value = rhs.value; return *this; } mod_t& operator+=(mod_t rhs) { value = (value + rhs.value) % MOD; return *this; } mod_t operator+(mod_t rhs) { mod_t result{*this}; result += rhs; return result; } mod_t& operator-=(mod_t rhs) { value = (MOD + value - rhs.value) % MOD; return *this; } mod_t operator-(mod_t rhs) { mod_t result{*this}; result -= rhs; return result; } friend ostream& operator<<(ostream& stream, const mod_t& v) { stream << v.value; return stream; } }; using Seq = vector<bool>; struct TestCase { size_t N; vector<Seq> sequences; }; TestCase read_test_case(); void solve_test_case(const TestCase&); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve_test_case(read_test_case()); } TestCase read_test_case() { TestCase tc; cin >> tc.N; tc.sequences.resize(tc.N); for (size_t i = 0; i < tc.N; i++) { string s; cin >> s; tc.sequences[i].resize(s.size()); for (size_t j = 0; j < s.size(); j++) tc.sequences[i][j] = s[j] == 'L'; } return tc; } template <size_t M> struct DPDump { vector<mod_t<M>> dp; vector<mod_t<M>> seen[2]; size_t seq_size = 1; DPDump(size_t size) { add_size(size); } void add_size(size_t size) { seq_size += size; dp.resize(seq_size + 1, 0); seen[0].resize(seq_size + 1, 0); seen[1].resize(seq_size + 1, 0); } }; template <size_t M> DPDump<M> calculate_dp(const Seq& seq); template <size_t M> DPDump<M> continue_dp(DPDump<M> dump, const Seq& seq); void solve_test_case(const TestCase& tc) { vector<DPDump<MOD>> dumps; for (const auto& seq : tc.sequences) dumps.push_back(calculate_dp<MOD>(seq)); for (size_t i = 0; i < tc.N; i++) { for (const auto& s2 : tc.sequences) cout << continue_dp<MOD>(dumps[i], s2).dp[0] << " "; cout << "\n"; } } template <size_t M> DPDump<M> calculate_dp(const Seq& seq) { DPDump<M> dump(0); return continue_dp(dump, seq); } template <size_t M> DPDump<M> continue_dp(DPDump<M> dump, const Seq& seq) { dump.add_size(seq.size()); for (size_t i = 0; i < seq.size(); i++) { bool x = seq[i]; vector<mod_t<M>> new_unique(dump.seq_size, 0); if (x) // L { new_unique[1] = 1; for (size_t b = 0; b < new_unique.size() - 1; b++) new_unique[b + 1] = (new_unique[b + 1] + dump.dp[b]) - dump.seen[x][b + 1]; } else // R { for (size_t b = 0; b < new_unique.size() - 1; b++) new_unique[b] += dump.dp[b + 1] - dump.seen[x][b]; } for (size_t b = 0; b < new_unique.size(); b++) { dump.dp[b] += new_unique[b]; dump.seen[x][b] += new_unique[b]; } } return dump; } |