#include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int nax = 606; string s[nax]; int n; const int mod = 1e9 + 7; int dp[nax][nax]; void add(int &a, int b){ a += b; if(a >= mod) a -= mod; } int suf[nax][nax]; // i, j adv, is being taken int ways[2][nax][nax]; void calc_suf(int id){ string cur = "$" + s[id]; int sz = cur.size(); sz--; for(int i=0;i<nax;i++){ for(int j=0;j<nax;j++){ suf[i][j] = 0; } } suf[sz][0] = 1; vector<int> nxtL(sz + 2, sz + 1); vector<int> nxtP(sz + 2, sz + 1); for(int i=sz;i>=1;i--){ nxtL[i] = nxtL[i + 1]; nxtP[i] = nxtP[i + 1]; if(cur[i] == 'L') nxtL[i] = i; else nxtP[i] = i; } for(int i=sz-1;i>=1;i--){ for(int j=0;j<nax-1;j++){ int p = nxtL[i + 1]; if(p <= sz) add(suf[i][j], suf[p][j + 1]); p = nxtP[i + 1]; if(p <= sz && j != 0) add(suf[i][j], suf[p][j - 1]); if(j == 0) add(suf[i][j], 1); } } int pL = nxtL[1]; int pP = nxtP[1]; for(int i=0;i<nax;i++){ ways[0][id][i] = suf[pL][i]; ways[1][id][i] = suf[pP][i]; } } int sufix_sum[nax][nax]; void calc(int x1){ string cur = "$" + s[x1]; int sz = cur.size(); for(int i=0;i<nax;i++){ for(int j=0;j<nax;j++){ dp[i][j] = 0; } } sz--; vector<int> nxtL(sz + 2, sz + 1); vector<int> nxtP(sz + 2, sz + 1); for(int i=sz;i>=1;i--){ nxtL[i] = nxtL[i + 1]; nxtP[i] = nxtP[i + 1]; if(cur[i] == 'L') nxtL[i] = i; else nxtP[i] = i; } dp[0][0] = 1; for(int i=0;i<sz;i++){ for(int j=0;j<=i;j++){ int p = nxtL[i + 1]; if(p <= sz) add(dp[p][j + 1], dp[i][j]); p = nxtP[i + 1]; if(p <= sz && j != 0) add(dp[p][j - 1], dp[i][j]); } } int ans = 0; for(int i=0;i<=sz;i++) add(ans, dp[i][0]); for(int i=0;i<nax;i++){ for(int j=0;j<nax;j++){ sufix_sum[i][j] = 0; } } for(int i=sz;i>=0;i--){ for(int j=0;j<nax;j++){ sufix_sum[i][j] = dp[i][j]; add(sufix_sum[i][j], sufix_sum[i + 1][j]); } } add(ans, mod - 1); vector<int> popL(sz + 2, 0); vector<int> popP(sz + 2, 0); for(int i=1;i<=sz;i++){ popL[i] = popL[i - 1]; popP[i] = popP[i - 1]; if(cur[i] == 'L') popL[i] = i; else popP[i] = i; } for(int x2=1;x2<=n;x2++){ int tmp = ans; int pL = popL[sz]; int pP = popP[sz]; for(int j=0;j<nax-1;j++){ add(tmp, ((ll)sufix_sum[pL][j] * ways[0][x2][j + 1]) % mod); if(j != 0){ add(tmp, ((ll)sufix_sum[pP][j] * ways[1][x2][j - 1]) % mod); } } cout << tmp << " "; } } void solve(){ cin >> n; for(int i=1;i<=n;i++) cin >> s[i]; for(int i=1;i<=n;i++) calc_suf(i); for(int i=1;i<=n;i++){ calc(i); cout << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); 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 155 156 157 | #include <bits/stdc++.h> #include <random> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> #define ld long double #define ull unsigned long long #define mt make_tuple using namespace std; const int nax = 606; string s[nax]; int n; const int mod = 1e9 + 7; int dp[nax][nax]; void add(int &a, int b){ a += b; if(a >= mod) a -= mod; } int suf[nax][nax]; // i, j adv, is being taken int ways[2][nax][nax]; void calc_suf(int id){ string cur = "$" + s[id]; int sz = cur.size(); sz--; for(int i=0;i<nax;i++){ for(int j=0;j<nax;j++){ suf[i][j] = 0; } } suf[sz][0] = 1; vector<int> nxtL(sz + 2, sz + 1); vector<int> nxtP(sz + 2, sz + 1); for(int i=sz;i>=1;i--){ nxtL[i] = nxtL[i + 1]; nxtP[i] = nxtP[i + 1]; if(cur[i] == 'L') nxtL[i] = i; else nxtP[i] = i; } for(int i=sz-1;i>=1;i--){ for(int j=0;j<nax-1;j++){ int p = nxtL[i + 1]; if(p <= sz) add(suf[i][j], suf[p][j + 1]); p = nxtP[i + 1]; if(p <= sz && j != 0) add(suf[i][j], suf[p][j - 1]); if(j == 0) add(suf[i][j], 1); } } int pL = nxtL[1]; int pP = nxtP[1]; for(int i=0;i<nax;i++){ ways[0][id][i] = suf[pL][i]; ways[1][id][i] = suf[pP][i]; } } int sufix_sum[nax][nax]; void calc(int x1){ string cur = "$" + s[x1]; int sz = cur.size(); for(int i=0;i<nax;i++){ for(int j=0;j<nax;j++){ dp[i][j] = 0; } } sz--; vector<int> nxtL(sz + 2, sz + 1); vector<int> nxtP(sz + 2, sz + 1); for(int i=sz;i>=1;i--){ nxtL[i] = nxtL[i + 1]; nxtP[i] = nxtP[i + 1]; if(cur[i] == 'L') nxtL[i] = i; else nxtP[i] = i; } dp[0][0] = 1; for(int i=0;i<sz;i++){ for(int j=0;j<=i;j++){ int p = nxtL[i + 1]; if(p <= sz) add(dp[p][j + 1], dp[i][j]); p = nxtP[i + 1]; if(p <= sz && j != 0) add(dp[p][j - 1], dp[i][j]); } } int ans = 0; for(int i=0;i<=sz;i++) add(ans, dp[i][0]); for(int i=0;i<nax;i++){ for(int j=0;j<nax;j++){ sufix_sum[i][j] = 0; } } for(int i=sz;i>=0;i--){ for(int j=0;j<nax;j++){ sufix_sum[i][j] = dp[i][j]; add(sufix_sum[i][j], sufix_sum[i + 1][j]); } } add(ans, mod - 1); vector<int> popL(sz + 2, 0); vector<int> popP(sz + 2, 0); for(int i=1;i<=sz;i++){ popL[i] = popL[i - 1]; popP[i] = popP[i - 1]; if(cur[i] == 'L') popL[i] = i; else popP[i] = i; } for(int x2=1;x2<=n;x2++){ int tmp = ans; int pL = popL[sz]; int pP = popP[sz]; for(int j=0;j<nax-1;j++){ add(tmp, ((ll)sufix_sum[pL][j] * ways[0][x2][j + 1]) % mod); if(j != 0){ add(tmp, ((ll)sufix_sum[pP][j] * ways[1][x2][j - 1]) % mod); } } cout << tmp << " "; } } void solve(){ cin >> n; for(int i=1;i<=n;i++) cin >> s[i]; for(int i=1;i<=n;i++) calc_suf(i); for(int i=1;i<=n;i++){ calc(i); cout << "\n"; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); return 0; } |