#include <bits/stdc++.h> #include <unordered_map> #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define int short using namespace std; const int32_t MOD = 1e9 + 7; const int LUCKY_NUM = 13; const int LIMIT = 6e2 + LUCKY_NUM; const int DLIMIT = 2 * LIMIT; int nextLeft[DLIMIT]; int nextRight[DLIMIT]; int rightLeft[DLIMIT]; unordered_map<uint32_t, uint32_t> memRes; int n, len; string str[LIMIT], currentStr; inline int findNextRight(int i) { return nextRight[i + 1]; } inline int findNextLeft(int i) { return nextLeft[i + 1]; } inline void buildHilfeArrays(int I, int J) { memRes.clear(); currentStr = str[I] + str[J]; len = currentStr.size(); nextRight[len] = -1; nextLeft[len] = -1; rightLeft[len] = 0; for (int i = len - 1; i >= 0; i--) { rightLeft[i] = rightLeft[i + 1] + (currentStr[i] == 'P'); nextRight[i] = nextRight[i + 1]; nextLeft[i] = nextLeft[i + 1]; if (currentStr[i] == 'L') { nextLeft[i] = i; } else { nextRight[i] = i; } } } uint32_t calcValidSubSeqs(int it, int leftCount) { uint32_t key = it * DLIMIT + leftCount; if (memRes.count(key)) return memRes[key]; uint32_t res = (leftCount == 0); if (leftCount < rightLeft[it + 1]) { int next = findNextLeft(it); if (next != -1) { res += calcValidSubSeqs(next, leftCount + 1); } } if (leftCount > 0) { int next = findNextRight(it); if (next != -1) { res += calcValidSubSeqs(next, leftCount - 1); } } while (res >= MOD) { res -= MOD; } memRes[key] = res; return res; } inline void readInput() { boost; cin >> n; for (int i = 1; i <= n; i++) { cin >> str[i]; } } void solve() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { buildHilfeArrays(i, j); cout << calcValidSubSeqs(-1, 0) - 1 << " "; } cout << endl; } } int32_t main() { readInput(); solve(); }
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 | #include <bits/stdc++.h> #include <unordered_map> #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define int short using namespace std; const int32_t MOD = 1e9 + 7; const int LUCKY_NUM = 13; const int LIMIT = 6e2 + LUCKY_NUM; const int DLIMIT = 2 * LIMIT; int nextLeft[DLIMIT]; int nextRight[DLIMIT]; int rightLeft[DLIMIT]; unordered_map<uint32_t, uint32_t> memRes; int n, len; string str[LIMIT], currentStr; inline int findNextRight(int i) { return nextRight[i + 1]; } inline int findNextLeft(int i) { return nextLeft[i + 1]; } inline void buildHilfeArrays(int I, int J) { memRes.clear(); currentStr = str[I] + str[J]; len = currentStr.size(); nextRight[len] = -1; nextLeft[len] = -1; rightLeft[len] = 0; for (int i = len - 1; i >= 0; i--) { rightLeft[i] = rightLeft[i + 1] + (currentStr[i] == 'P'); nextRight[i] = nextRight[i + 1]; nextLeft[i] = nextLeft[i + 1]; if (currentStr[i] == 'L') { nextLeft[i] = i; } else { nextRight[i] = i; } } } uint32_t calcValidSubSeqs(int it, int leftCount) { uint32_t key = it * DLIMIT + leftCount; if (memRes.count(key)) return memRes[key]; uint32_t res = (leftCount == 0); if (leftCount < rightLeft[it + 1]) { int next = findNextLeft(it); if (next != -1) { res += calcValidSubSeqs(next, leftCount + 1); } } if (leftCount > 0) { int next = findNextRight(it); if (next != -1) { res += calcValidSubSeqs(next, leftCount - 1); } } while (res >= MOD) { res -= MOD; } memRes[key] = res; return res; } inline void readInput() { boost; cin >> n; for (int i = 1; i <= n; i++) { cin >> str[i]; } } void solve() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { buildHilfeArrays(i, j); cout << calcValidSubSeqs(-1, 0) - 1 << " "; } cout << endl; } } int32_t main() { readInput(); solve(); } |