#include<bits/stdc++.h> using namespace std; using lld = long long; using int128 = __int128_t; const int mod = 1e9+7; const int MAXN = 600; int tmp[MAXN+5]; int tL[MAXN][MAXN+5], tP[MAXN][MAXN+5]; int rtL[MAXN][MAXN+5], rtP[MAXN][MAXN+5]; int res[MAXN]; int subL[MAXN+5][MAXN+5], subP[MAXN+5][MAXN+5]; int addL[MAXN+5], addP[MAXN+5]; inline int N(const string& s) { return s.length()+1; } inline int add(int a, int b) { return (a+b > mod ? a+b-mod : a+b); } int solve(const string& s, int *L, int *P) { int n = N(s); L[0] = 1; int result = 0; for(int i=0;i<s.length();i++) { for(int j=0;j<=n;j++) tmp[j] = 0; if(s[i] == 'L') { for(int j=1;j<=n;j++) tmp[j] = L[j-1]; for(int j=0;j<n;j++) { P[j] = add(tmp[j], P[j]); L[j] = tmp[j]; } result = add(result, L[0]); } else { for(int j=1;j<=n;j++) tmp[j-1] = P[j]; for(int j=0;j<n;j++) { P[j] = tmp[j]; L[j] = add(tmp[j], L[j]); } result = add(result, P[0]); } } return result; } void revSolve(const string& s, int *L, int *P) { int n = N(s); for(int i=0;i<=n;i++) addL[i] = addP[i] = 0; addP[1] = 1; vector<int> prevL(s.length(),0), prevP(s.length(), 0); for(int i=1;i<s.length();i++) { prevL[i] = prevL[i-1]; prevP[i] = prevP[i-1]; (s[i-1] == 'L' ? prevL : prevP)[i] = i-1; } int result = 0; for(int i=s.length()-1;i>=0;i--) { for(int j=0;j<=n;j++) tmp[j] = 0; for(int j=0;j<=n;j++) { addL[j] = add(addL[j], mod-subL[i+1][j]); subL[i+1][j] = 0; } for(int j=0;j<=n;j++) { addP[j] = add(addP[j], mod-subP[i+1][j]); subP[i+1][j] = 0; } if(s[i] == 'L') { for(int j=0;j<=n;j++) L[j] = addL[j]; for(int j=1;j<=n;j++) addL[j-1] = add(addL[j-1], L[j]); for(int j=1;j<=n;j++) addP[j] = add(addP[j], L[j-1]); int x = prevL[i]; for(int j=1;j<=n;j++) subL[x][j-1] = add(subL[x][j-1], L[j]); for(int j=1;j<=n;j++) subP[x][j] = add(subP[x][j], L[j-1]); } else { for(int j=0;j<=n;j++) P[j] = addP[j]; for(int j=1;j<=n;j++) addL[j-1] = add(addL[j-1], P[j]); for(int j=1;j<=n;j++) addP[j] = add(addP[j], P[j-1]); int x = prevP[i]; for(int j=1;j<=n;j++) subL[x][j-1] = add(subL[x][j-1], P[j]); for(int j=1;j<=n;j++) subP[x][j] = add(subP[x][j], P[j-1]); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<string> t(n); for(auto& s : t) cin >> s; for(int i=0;i<n;i++) res[i] = solve(t[i], tL[i], tP[i]); for(int i=0;i<n;i++) revSolve(t[i], rtL[i], rtP[i]); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { int128 result = res[i]; int l = min(N(t[i]), N(t[j])); for(int k=0;k<=l;k++) result += (int128) tL[i][k] * (int128) rtL[j][k]; for(int k=0;k<=l;k++) result += (int128) tP[i][k] * (int128) rtP[j][k]; cout << (int) (result%mod) << " "; } cout << "\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 | #include<bits/stdc++.h> using namespace std; using lld = long long; using int128 = __int128_t; const int mod = 1e9+7; const int MAXN = 600; int tmp[MAXN+5]; int tL[MAXN][MAXN+5], tP[MAXN][MAXN+5]; int rtL[MAXN][MAXN+5], rtP[MAXN][MAXN+5]; int res[MAXN]; int subL[MAXN+5][MAXN+5], subP[MAXN+5][MAXN+5]; int addL[MAXN+5], addP[MAXN+5]; inline int N(const string& s) { return s.length()+1; } inline int add(int a, int b) { return (a+b > mod ? a+b-mod : a+b); } int solve(const string& s, int *L, int *P) { int n = N(s); L[0] = 1; int result = 0; for(int i=0;i<s.length();i++) { for(int j=0;j<=n;j++) tmp[j] = 0; if(s[i] == 'L') { for(int j=1;j<=n;j++) tmp[j] = L[j-1]; for(int j=0;j<n;j++) { P[j] = add(tmp[j], P[j]); L[j] = tmp[j]; } result = add(result, L[0]); } else { for(int j=1;j<=n;j++) tmp[j-1] = P[j]; for(int j=0;j<n;j++) { P[j] = tmp[j]; L[j] = add(tmp[j], L[j]); } result = add(result, P[0]); } } return result; } void revSolve(const string& s, int *L, int *P) { int n = N(s); for(int i=0;i<=n;i++) addL[i] = addP[i] = 0; addP[1] = 1; vector<int> prevL(s.length(),0), prevP(s.length(), 0); for(int i=1;i<s.length();i++) { prevL[i] = prevL[i-1]; prevP[i] = prevP[i-1]; (s[i-1] == 'L' ? prevL : prevP)[i] = i-1; } int result = 0; for(int i=s.length()-1;i>=0;i--) { for(int j=0;j<=n;j++) tmp[j] = 0; for(int j=0;j<=n;j++) { addL[j] = add(addL[j], mod-subL[i+1][j]); subL[i+1][j] = 0; } for(int j=0;j<=n;j++) { addP[j] = add(addP[j], mod-subP[i+1][j]); subP[i+1][j] = 0; } if(s[i] == 'L') { for(int j=0;j<=n;j++) L[j] = addL[j]; for(int j=1;j<=n;j++) addL[j-1] = add(addL[j-1], L[j]); for(int j=1;j<=n;j++) addP[j] = add(addP[j], L[j-1]); int x = prevL[i]; for(int j=1;j<=n;j++) subL[x][j-1] = add(subL[x][j-1], L[j]); for(int j=1;j<=n;j++) subP[x][j] = add(subP[x][j], L[j-1]); } else { for(int j=0;j<=n;j++) P[j] = addP[j]; for(int j=1;j<=n;j++) addL[j-1] = add(addL[j-1], P[j]); for(int j=1;j<=n;j++) addP[j] = add(addP[j], P[j-1]); int x = prevP[i]; for(int j=1;j<=n;j++) subL[x][j-1] = add(subL[x][j-1], P[j]); for(int j=1;j<=n;j++) subP[x][j] = add(subP[x][j], P[j-1]); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<string> t(n); for(auto& s : t) cin >> s; for(int i=0;i<n;i++) res[i] = solve(t[i], tL[i], tP[i]); for(int i=0;i<n;i++) revSolve(t[i], rtL[i], rtP[i]); for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { int128 result = res[i]; int l = min(N(t[i]), N(t[j])); for(int k=0;k<=l;k++) result += (int128) tL[i][k] * (int128) rtL[j][k]; for(int k=0;k<=l;k++) result += (int128) tP[i][k] * (int128) rtP[j][k]; cout << (int) (result%mod) << " "; } cout << "\n"; } return 0; } |