#include <cstring> #include <cstdio> #include <vector> const int max_n = 610; const int max_len = 610; const int mod = 1000000007; int n; int len; char in[max_len]; int nextL[max_len]; int nextR[max_len]; std::vector<int> invL[max_len]; std::vector<int> invR[max_len]; int count[max_len][max_len]; int suffStartL[max_n][max_len]; int suffStartR[max_n][max_len]; int prefStartL[max_n][max_len]; int prefStartR[max_n][max_len]; int prefAns[max_n]; void forwardCompute(int index) { for (int i = 0; i <= len + 1; ++i) { for (int j = 0; j <= i; ++j) { count[i][j] = 0; } } for (int i = 0; i < max_len; ++i) { suffStartL[index][i] = suffStartR[index][i] = 0; } count[nextL[0]][1] = 1; if (nextL[0] == len + 1) { suffStartL[index][1] = 1; } int ans = 0; for (int i = 1; i <= len; ++i) { const int nL = nextL[i]; const int nR = nextR[i]; count[nL][1] += count[i][0]; if (count[nL][1] >= mod) count[nL][1] -= mod; if (nL == len + 1) { suffStartL[index][1] += count[i][0]; if (suffStartL[index][1] >= mod) suffStartL[index][1] -= mod; } for (int j = 1; j <= i; ++j) { count[nL][j + 1] += count[i][j]; count[nR][j - 1] += count[i][j]; if (count[nL][j + 1] >= mod) count[nL][j + 1] -= mod; if (count[nR][j - 1] >= mod) count[nR][j - 1] -= mod; if (nL == len + 1) { suffStartL[index][j + 1] += count[i][j]; if (suffStartL[index][j + 1] >= mod) suffStartL[index][j + 1] -= mod; } if (nR == len + 1) { suffStartR[index][j - 1] += count[i][j]; if (suffStartR[index][j - 1] >= mod) suffStartR[index][j - 1] -= mod; } } ans += count[i][0]; if (ans >= mod) ans -= mod; } prefAns[index] = ans; } void backwardCompute(int index) { for (int i = 0; i <= len + 1; ++i) { for (int j = 0; j <= len - i; ++j) { count[i][j] = 0; } } for (int i = 0; i < max_len; ++i) { prefStartL[index][i] = prefStartR[index][i] = 0; } for (int i = len; i > 0; --i) { count[i][0] += 1; if (count[i][0] >= mod) count[i][0] -= mod; for (int j = 0; j < invR[i].size(); ++j) { int iR = invR[i][j]; count[iR][1] += count[i][0]; if (count[iR][1] >= mod) count[iR][1] -= mod; if (iR == 0) { prefStartR[index][0] += count[i][0]; if (prefStartR[index][0] >= mod) prefStartR[index][0] -= mod; } } for (int j = 1; j <= len - i; ++j) { for (int k = 0; k < invR[i].size(); ++k) { int iR = invR[i][k]; count[iR][j + 1] += count[i][j]; if (count[iR][j + 1] >= mod) count[iR][j + 1] -= mod; if (iR == 0) { prefStartR[index][j] += count[i][j]; if (prefStartR[index][j] >= mod) prefStartR[index][j] -= mod; } } for (int k = 0; k < invL[i].size(); ++k) { int iL = invL[i][k]; count[iL][j - 1] += count[i][j]; if (count[iL][j - 1] >= mod) count[iL][j - 1] -= mod; if (iL == 0) { prefStartL[index][j] += count[i][j]; if (prefStartL[index][j] >= mod) prefStartL[index][j] -= mod; } } } } } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf(" %s ", in); len = strlen(in); for (int j = 0; j < len + 1; ++j) { invL[j].clear(); invR[j].clear(); } nextL[len] = len + 1; nextR[len] = len + 1; for (int j = len; j > 0; --j) { nextL[j - 1] = in[j - 1] == 'L' ? j : nextL[j]; nextR[j - 1] = in[j - 1] == 'P' ? j : nextR[j]; invL[nextL[j - 1]].push_back(j - 1); invR[nextR[j - 1]].push_back(j - 1); } forwardCompute(i); backwardCompute(i); /* printf("%s %d\n", in, prefAns[i]); for (int j = 0; j <= len; ++j) { printf("L[%d] %d %d\n", j, suffStartL[i][j], prefStartL[i][j]); } for (int j = 0; j <= len; ++j) { printf("R[%d] %d %d\n", j, suffStartR[i][j], prefStartR[i][j]); } */ } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { long long ans = prefAns[i]; for (int k = 0; k < max_len; ++k) { ans += ((long long)(suffStartL[i][k]) * prefStartL[j][k]) % mod; ans += ((long long)(suffStartR[i][k]) * prefStartR[j][k]) % mod; } printf("%lld ", ans % mod); } printf("\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 149 150 151 152 153 154 | #include <cstring> #include <cstdio> #include <vector> const int max_n = 610; const int max_len = 610; const int mod = 1000000007; int n; int len; char in[max_len]; int nextL[max_len]; int nextR[max_len]; std::vector<int> invL[max_len]; std::vector<int> invR[max_len]; int count[max_len][max_len]; int suffStartL[max_n][max_len]; int suffStartR[max_n][max_len]; int prefStartL[max_n][max_len]; int prefStartR[max_n][max_len]; int prefAns[max_n]; void forwardCompute(int index) { for (int i = 0; i <= len + 1; ++i) { for (int j = 0; j <= i; ++j) { count[i][j] = 0; } } for (int i = 0; i < max_len; ++i) { suffStartL[index][i] = suffStartR[index][i] = 0; } count[nextL[0]][1] = 1; if (nextL[0] == len + 1) { suffStartL[index][1] = 1; } int ans = 0; for (int i = 1; i <= len; ++i) { const int nL = nextL[i]; const int nR = nextR[i]; count[nL][1] += count[i][0]; if (count[nL][1] >= mod) count[nL][1] -= mod; if (nL == len + 1) { suffStartL[index][1] += count[i][0]; if (suffStartL[index][1] >= mod) suffStartL[index][1] -= mod; } for (int j = 1; j <= i; ++j) { count[nL][j + 1] += count[i][j]; count[nR][j - 1] += count[i][j]; if (count[nL][j + 1] >= mod) count[nL][j + 1] -= mod; if (count[nR][j - 1] >= mod) count[nR][j - 1] -= mod; if (nL == len + 1) { suffStartL[index][j + 1] += count[i][j]; if (suffStartL[index][j + 1] >= mod) suffStartL[index][j + 1] -= mod; } if (nR == len + 1) { suffStartR[index][j - 1] += count[i][j]; if (suffStartR[index][j - 1] >= mod) suffStartR[index][j - 1] -= mod; } } ans += count[i][0]; if (ans >= mod) ans -= mod; } prefAns[index] = ans; } void backwardCompute(int index) { for (int i = 0; i <= len + 1; ++i) { for (int j = 0; j <= len - i; ++j) { count[i][j] = 0; } } for (int i = 0; i < max_len; ++i) { prefStartL[index][i] = prefStartR[index][i] = 0; } for (int i = len; i > 0; --i) { count[i][0] += 1; if (count[i][0] >= mod) count[i][0] -= mod; for (int j = 0; j < invR[i].size(); ++j) { int iR = invR[i][j]; count[iR][1] += count[i][0]; if (count[iR][1] >= mod) count[iR][1] -= mod; if (iR == 0) { prefStartR[index][0] += count[i][0]; if (prefStartR[index][0] >= mod) prefStartR[index][0] -= mod; } } for (int j = 1; j <= len - i; ++j) { for (int k = 0; k < invR[i].size(); ++k) { int iR = invR[i][k]; count[iR][j + 1] += count[i][j]; if (count[iR][j + 1] >= mod) count[iR][j + 1] -= mod; if (iR == 0) { prefStartR[index][j] += count[i][j]; if (prefStartR[index][j] >= mod) prefStartR[index][j] -= mod; } } for (int k = 0; k < invL[i].size(); ++k) { int iL = invL[i][k]; count[iL][j - 1] += count[i][j]; if (count[iL][j - 1] >= mod) count[iL][j - 1] -= mod; if (iL == 0) { prefStartL[index][j] += count[i][j]; if (prefStartL[index][j] >= mod) prefStartL[index][j] -= mod; } } } } } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf(" %s ", in); len = strlen(in); for (int j = 0; j < len + 1; ++j) { invL[j].clear(); invR[j].clear(); } nextL[len] = len + 1; nextR[len] = len + 1; for (int j = len; j > 0; --j) { nextL[j - 1] = in[j - 1] == 'L' ? j : nextL[j]; nextR[j - 1] = in[j - 1] == 'P' ? j : nextR[j]; invL[nextL[j - 1]].push_back(j - 1); invR[nextR[j - 1]].push_back(j - 1); } forwardCompute(i); backwardCompute(i); /* printf("%s %d\n", in, prefAns[i]); for (int j = 0; j <= len; ++j) { printf("L[%d] %d %d\n", j, suffStartL[i][j], prefStartL[i][j]); } for (int j = 0; j <= len; ++j) { printf("R[%d] %d %d\n", j, suffStartR[i][j], prefStartR[i][j]); } */ } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { long long ans = prefAns[i]; for (int k = 0; k < max_len; ++k) { ans += ((long long)(suffStartL[i][k]) * prefStartL[j][k]) % mod; ans += ((long long)(suffStartR[i][k]) * prefStartR[j][k]) % mod; } printf("%lld ", ans % mod); } printf("\n"); } return 0; } |