#include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k);i>=(p);--i) #define ALL(v) begin(v), end(v) #define RET(smth) return void(cout<<smth<<'\n'); #define SIZ(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int MM = 607; const int P = 1'000'000'007; const long long SQ = 1LL * P * P; int add(int a, int b) { return a+b>=P ? a+b-P : a+b; } int sub(int a, int b) { return a<b ? a-b+P : a-b; } int dp[MM][MM]; int calc[MM][MM], icalc[MM][MM], O[MM][MM], Z[MM][MM], iO[MM][MM], iZ[MM][MM]; int zeros[MM]; void calculate(int * res_calc, int * res_O, int * res_Z, const string & S) { dp[0][0] = 1; int * cur_O = zeros; int * cur_Z = zeros; for(int i=1;i<=SIZ(S);i++) { if(S[i-1] == 'L') { dp[i][0]=dp[i-1][0]; for(int k=1;k<=i+3;k++) { dp[i][k] = sub(add(dp[i-1][k-1],dp[i-1][k]),cur_O[k]); } cur_O = dp[i-1] - 1; } else { for(int k=0;k<=i+3;k++) { dp[i][k] = sub(add(dp[i-1][k+1],dp[i-1][k]),cur_Z[k]); } cur_Z = dp[i-1] + 1; } } memcpy(res_calc, dp[SIZ(S)], (SIZ(S)+2) * sizeof(int)); memcpy(res_O, cur_O, (SIZ(S)+2)*sizeof(int)); memcpy(res_Z, cur_Z, (SIZ(S)+2)*sizeof(int)); } void rev_calculate(int * res_calc, int * res_O, int * res_Z, const string & S) { dp[0][0] = 1; int * cur_O = zeros; int * cur_Z = zeros; for(int i=1;i<=SIZ(S);i++) { if(S[i-1]=='L') { for(int k=1;k<=i+3;k++) { dp[i][k-1] = sub(add(dp[i-1][k-1],dp[i-1][k]),cur_O[k]); } cur_O = dp[i-1]; } else { dp[i][0] = dp[i-1][0]; for(int k=0;k<=i+3;k++) { dp[i][k+1] = sub(add(dp[i-1][k],dp[i-1][k+1]),cur_Z[k]); } cur_Z = dp[i-1]; } } memcpy(res_calc, dp[SIZ(S)], (SIZ(S)+2) * sizeof(int)); memcpy(res_O, cur_O, (SIZ(S)+2)*sizeof(int)); memcpy(res_Z, cur_Z, (SIZ(S)+2)*sizeof(int)); } int len[MM]; int get_res(int x, int y) { long long res = 0; for(int i=0;i<=min(len[x],len[y])+1;i++) { res += 1LL * calc[x][i] * icalc[y][i]; if(res >= SQ) res -= SQ; res += 1LL * O[x][i] * (P-iO[y][i]); if(res >= SQ) res -= SQ; res += 1LL * Z[x][i] * (P-iZ[y][i]); if(res >= SQ) res -= SQ; } res %= P; return res == 0 ? P-1 : res-1; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(0); cin.tie(0); #endif int n; cin >> n; string S; for(int i=0;i<n;i++) { cin >> S; calculate(calc[i],O[i],Z[i],S); reverse(ALL(S)); rev_calculate(icalc[i],iO[i],iZ[i],S); len[i] = SIZ(S); } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { printf("%d ", get_res(i,j)); } puts(""); } 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 | #include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k);i>=(p);--i) #define ALL(v) begin(v), end(v) #define RET(smth) return void(cout<<smth<<'\n'); #define SIZ(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int MM = 607; const int P = 1'000'000'007; const long long SQ = 1LL * P * P; int add(int a, int b) { return a+b>=P ? a+b-P : a+b; } int sub(int a, int b) { return a<b ? a-b+P : a-b; } int dp[MM][MM]; int calc[MM][MM], icalc[MM][MM], O[MM][MM], Z[MM][MM], iO[MM][MM], iZ[MM][MM]; int zeros[MM]; void calculate(int * res_calc, int * res_O, int * res_Z, const string & S) { dp[0][0] = 1; int * cur_O = zeros; int * cur_Z = zeros; for(int i=1;i<=SIZ(S);i++) { if(S[i-1] == 'L') { dp[i][0]=dp[i-1][0]; for(int k=1;k<=i+3;k++) { dp[i][k] = sub(add(dp[i-1][k-1],dp[i-1][k]),cur_O[k]); } cur_O = dp[i-1] - 1; } else { for(int k=0;k<=i+3;k++) { dp[i][k] = sub(add(dp[i-1][k+1],dp[i-1][k]),cur_Z[k]); } cur_Z = dp[i-1] + 1; } } memcpy(res_calc, dp[SIZ(S)], (SIZ(S)+2) * sizeof(int)); memcpy(res_O, cur_O, (SIZ(S)+2)*sizeof(int)); memcpy(res_Z, cur_Z, (SIZ(S)+2)*sizeof(int)); } void rev_calculate(int * res_calc, int * res_O, int * res_Z, const string & S) { dp[0][0] = 1; int * cur_O = zeros; int * cur_Z = zeros; for(int i=1;i<=SIZ(S);i++) { if(S[i-1]=='L') { for(int k=1;k<=i+3;k++) { dp[i][k-1] = sub(add(dp[i-1][k-1],dp[i-1][k]),cur_O[k]); } cur_O = dp[i-1]; } else { dp[i][0] = dp[i-1][0]; for(int k=0;k<=i+3;k++) { dp[i][k+1] = sub(add(dp[i-1][k],dp[i-1][k+1]),cur_Z[k]); } cur_Z = dp[i-1]; } } memcpy(res_calc, dp[SIZ(S)], (SIZ(S)+2) * sizeof(int)); memcpy(res_O, cur_O, (SIZ(S)+2)*sizeof(int)); memcpy(res_Z, cur_Z, (SIZ(S)+2)*sizeof(int)); } int len[MM]; int get_res(int x, int y) { long long res = 0; for(int i=0;i<=min(len[x],len[y])+1;i++) { res += 1LL * calc[x][i] * icalc[y][i]; if(res >= SQ) res -= SQ; res += 1LL * O[x][i] * (P-iO[y][i]); if(res >= SQ) res -= SQ; res += 1LL * Z[x][i] * (P-iZ[y][i]); if(res >= SQ) res -= SQ; } res %= P; return res == 0 ? P-1 : res-1; } int main() { #ifndef DEBUG ios_base::sync_with_stdio(0); cin.tie(0); #endif int n; cin >> n; string S; for(int i=0;i<n;i++) { cin >> S; calculate(calc[i],O[i],Z[i],S); reverse(ALL(S)); rev_calculate(icalc[i],iO[i],iZ[i],S); len[i] = SIZ(S); } for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { printf("%d ", get_res(i,j)); } puts(""); } return 0; } |