#ifndef LOCAL #pragma GCC optimize("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) { sim > struct rge {c b, e; }; sim > rge<c> range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") int mod = 1000 * 1000 * 1000 + 7; long long solve(string s) { int n = s.size(); vector<vector<map<int, long long>>> v; vector<int> nearestL; vector<int> nearestP; v.resize(n + 5); for (auto& x : v) { x.resize(n+5); } nearestL.resize(n+5); nearestP.resize(n+5); nearestL[n] = n+1; nearestP[n] = n+1; for (int i = n-1; i >= 0; i --) { nearestL[i] = nearestL[i+1]; nearestP[i] = nearestP[i+1]; if (s[i] == 'L') nearestL[i] = i; if (s[i] == 'P') nearestP[i] = i; } // for (int i = 0; i < n; i ++) { // cout << nearestL[i] << " " << nearestP[i] << "\n"; // } v[0][0][0] = 1; long long total_score = 0; for (int i = 0; i <= n; i ++) { for (int depth = 0; depth <= i; depth ++) { for (auto& score : v[i][depth]) { int pos = score.first; long long cnt = score.second % mod; if (pos < n + 1) { v[i+1][depth+1][nearestL[pos]+1] += cnt; } if (depth > 0 && pos < n + 1) { v[i+1][depth-1][nearestP[pos]+1] += cnt; } } } if (i > 0) for (auto& score : v[i][0]) { if (score.first < n + 1) { total_score += (score.second % mod); } } } return total_score % mod; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> v; for (int i = 0; i < n; i ++) { string s; cin >> s; v.push_back(s); } for (int i =0; i < n; i ++) { for (int j = 0;j < n; j ++) { cout << solve(v[i] + v[j]) << " "; } 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 | #ifndef LOCAL #pragma GCC optimize("O3") #endif #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define mor > muu & operator << ( #define R22(r) sim > typename \ enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) { sim > struct rge {c b, e; }; sim > rge<c> range(c h, c n) { return {h, n}; } sim > auto dud(c * r) -> decltype(cerr << *r); sim > char dud(...); struct muu { #ifdef LOCAL stringstream a; ~muu() { cerr << a.str() << endl; } R22(<) a << boolalpha << g; ris; } R22(==) ris << range(begin(g), end(g)); } sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; } sim mor rge<c> u) { a << "["; for (c i = u.b; i != u.e; ++i) *this << ", " + 2 * (i == u.b) << *i; ris << "]"; } #else sim mor const c&) { ris; } #endif muu & operator()() { ris; } }; #define imie(r...) "[" #r ": " << (r) << "] " #define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ") int mod = 1000 * 1000 * 1000 + 7; long long solve(string s) { int n = s.size(); vector<vector<map<int, long long>>> v; vector<int> nearestL; vector<int> nearestP; v.resize(n + 5); for (auto& x : v) { x.resize(n+5); } nearestL.resize(n+5); nearestP.resize(n+5); nearestL[n] = n+1; nearestP[n] = n+1; for (int i = n-1; i >= 0; i --) { nearestL[i] = nearestL[i+1]; nearestP[i] = nearestP[i+1]; if (s[i] == 'L') nearestL[i] = i; if (s[i] == 'P') nearestP[i] = i; } // for (int i = 0; i < n; i ++) { // cout << nearestL[i] << " " << nearestP[i] << "\n"; // } v[0][0][0] = 1; long long total_score = 0; for (int i = 0; i <= n; i ++) { for (int depth = 0; depth <= i; depth ++) { for (auto& score : v[i][depth]) { int pos = score.first; long long cnt = score.second % mod; if (pos < n + 1) { v[i+1][depth+1][nearestL[pos]+1] += cnt; } if (depth > 0 && pos < n + 1) { v[i+1][depth-1][nearestP[pos]+1] += cnt; } } } if (i > 0) for (auto& score : v[i][0]) { if (score.first < n + 1) { total_score += (score.second % mod); } } } return total_score % mod; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> v; for (int i = 0; i < n; i ++) { string s; cin >> s; v.push_back(s); } for (int i =0; i < n; i ++) { for (int j = 0;j < n; j ++) { cout << solve(v[i] + v[j]) << " "; } cout << "\n"; } } |