#include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) int M[603][603]; int NEXT[603][2]; inline int sum(int a, int b) { return a + b < MOD ? (a + b): (a + b - MOD); } void computeM(const std::vector<bool>& s) { ZERO(M); MINUS(NEXT); int start = INF, max_h = 0; FOR(i,0,SZ(s)) if (s[i]) { M[i][1] = 1; start = i; break; } int next_true = -1, next_false = -1; for (int i = SZ(s) - 1; i >= 0; --i) { NEXT[i][0] = next_false; NEXT[i][1] = next_true; if (s[i]) next_true = i; else next_false = i; } FOR(i,start,SZ(s)) { max_h += s[i]; if (M[i][0]) { if (NEXT[i][1] != -1) { M[NEXT[i][1]][1] = sum(M[NEXT[i][1]][1], M[i][0]); } } FORE(h,1,max_h) { if (M[i][h]) { if (NEXT[i][1] != -1) { M[NEXT[i][1]][h+1] = sum(M[NEXT[i][1]][h+1], M[i][h]); } if (NEXT[i][0] != -1) { M[NEXT[i][0]][h-1] = sum(M[NEXT[i][0]][h-1], M[i][h]); } } } } } int RSUM[603][603][2]; void computeR(const std::vector<bool>& s, int idx) { int max_h = 0; FOR(i,0,SZ(s)) max_h += s[i]; FORE(h,0,max_h) { LL sum0 = 0, sum1 = 0; FOR(i,0,SZ(s)) { if (s[i]) sum1 += M[i][h]; else sum0 += M[i][h]; } RSUM[idx][h][0] = sum0 % MOD; RSUM[idx][h][1] = sum1 % MOD; } } int LSUM[603]; void computeL(const std::vector<bool>& s) { ZERO(LSUM); int max_h = 0; FOR(i,0,SZ(s)) max_h += s[i]; bool last = s.back(); if (SZ(s) >= 2) { for (int i = SZ(s) - 2; i >= 0; --i) { FORE(h,0,max_h) LSUM[h] = sum(LSUM[h], M[i][h]); if (s[i] != last) break; } } } char S[1000]; int main() { int n; scanf("%d", &n); vector<vector<bool> > input(n); FOR(i,0,n) { scanf("%s", S); int k = strlen(S); vector<bool> rev; for (int j = k - 1; j >= 0; --j) rev.push_back(S[j] == 'P'); FOR(j,0,k) input[i].push_back(S[j] == 'L'); computeM(rev); computeR(rev, i); } FOR(i,0,n) { bool all_false = true; int k = SZ(input[i]); FOR(j,0,k) { all_false &= !input[i][j]; } if (all_false) { FOR(j,0,n) printf("%d ", sum(RSUM[j][0][0], RSUM[j][0][1])); printf("\n"); continue; } computeM(input[i]); computeL(input[i]); LL start = 0; int max_h = 0; FOR(j,0,k) { start += M[j][0]; max_h += input[i][j]; } start %= MOD; FOR(j,0,n) { LL result = start; FORE(h,0,max_h) { result += (LL)M[k-1][h] * sum(RSUM[j][h][0], RSUM[j][h][1]); result %= MOD; } if (SZ(input[i]) > 1) { bool p = input[i].back(); FORE(h,0,max_h) { result += (LL)LSUM[h] * RSUM[j][h][p]; result %= MOD; } } printf("%lld ", result); } printf("\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 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 | #include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) int M[603][603]; int NEXT[603][2]; inline int sum(int a, int b) { return a + b < MOD ? (a + b): (a + b - MOD); } void computeM(const std::vector<bool>& s) { ZERO(M); MINUS(NEXT); int start = INF, max_h = 0; FOR(i,0,SZ(s)) if (s[i]) { M[i][1] = 1; start = i; break; } int next_true = -1, next_false = -1; for (int i = SZ(s) - 1; i >= 0; --i) { NEXT[i][0] = next_false; NEXT[i][1] = next_true; if (s[i]) next_true = i; else next_false = i; } FOR(i,start,SZ(s)) { max_h += s[i]; if (M[i][0]) { if (NEXT[i][1] != -1) { M[NEXT[i][1]][1] = sum(M[NEXT[i][1]][1], M[i][0]); } } FORE(h,1,max_h) { if (M[i][h]) { if (NEXT[i][1] != -1) { M[NEXT[i][1]][h+1] = sum(M[NEXT[i][1]][h+1], M[i][h]); } if (NEXT[i][0] != -1) { M[NEXT[i][0]][h-1] = sum(M[NEXT[i][0]][h-1], M[i][h]); } } } } } int RSUM[603][603][2]; void computeR(const std::vector<bool>& s, int idx) { int max_h = 0; FOR(i,0,SZ(s)) max_h += s[i]; FORE(h,0,max_h) { LL sum0 = 0, sum1 = 0; FOR(i,0,SZ(s)) { if (s[i]) sum1 += M[i][h]; else sum0 += M[i][h]; } RSUM[idx][h][0] = sum0 % MOD; RSUM[idx][h][1] = sum1 % MOD; } } int LSUM[603]; void computeL(const std::vector<bool>& s) { ZERO(LSUM); int max_h = 0; FOR(i,0,SZ(s)) max_h += s[i]; bool last = s.back(); if (SZ(s) >= 2) { for (int i = SZ(s) - 2; i >= 0; --i) { FORE(h,0,max_h) LSUM[h] = sum(LSUM[h], M[i][h]); if (s[i] != last) break; } } } char S[1000]; int main() { int n; scanf("%d", &n); vector<vector<bool> > input(n); FOR(i,0,n) { scanf("%s", S); int k = strlen(S); vector<bool> rev; for (int j = k - 1; j >= 0; --j) rev.push_back(S[j] == 'P'); FOR(j,0,k) input[i].push_back(S[j] == 'L'); computeM(rev); computeR(rev, i); } FOR(i,0,n) { bool all_false = true; int k = SZ(input[i]); FOR(j,0,k) { all_false &= !input[i][j]; } if (all_false) { FOR(j,0,n) printf("%d ", sum(RSUM[j][0][0], RSUM[j][0][1])); printf("\n"); continue; } computeM(input[i]); computeL(input[i]); LL start = 0; int max_h = 0; FOR(j,0,k) { start += M[j][0]; max_h += input[i][j]; } start %= MOD; FOR(j,0,n) { LL result = start; FORE(h,0,max_h) { result += (LL)M[k-1][h] * sum(RSUM[j][h][0], RSUM[j][h][1]); result %= MOD; } if (SZ(input[i]) > 1) { bool p = input[i].back(); FORE(h,0,max_h) { result += (LL)LSUM[h] * RSUM[j][h][p]; result %= MOD; } } printf("%lld ", result); } printf("\n"); } } |