#include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; // O(n^4) int tab[2][609][3]; vector<int> drb[609]; string s; const int M = 1e9 + 7; void solve(int x, int y) { int n = drb[x].size() + drb[y].size(), wyn = 0, a, b; for(int i=0; i<=1; i++) for(int j=0; j<=600; j++) for(int k=0; k<=2; k++) tab[i][j][k] = 0; tab[0][0][1] = 1; for(int i=0; i<n; i++) { if(i < drb[x].size()) b = drb[x][i]; else b = drb[y][i - drb[x].size()]; for(int k=0; k<=n; k++) { a = k + b; if(k > 600) break; if(a < 0 || a > 600) continue; if(b == 1) { tab[1][a][0] = (tab[1][a][0] + tab[0][k][1]) % M; tab[1][a][1] = (tab[1][a][1] + tab[0][k][1]) % M; tab[1][a][2] = (tab[1][a][2] + tab[0][k][1]) % M; tab[0][k][1] = 0; } else { tab[1][a][0] = (tab[1][a][0] + tab[0][k][2]) % M; tab[1][a][1] = (tab[1][a][1] + tab[0][k][2]) % M; tab[1][a][2] = (tab[1][a][2] + tab[0][k][2]) % M; tab[0][k][2] = 0; } } for(int j=0; j<=n; j++) { if(j > 600) break; for(int k=0; k<=2; k++) { tab[0][j][k] = (tab[0][j][k] + tab[1][j][k]) % M; tab[1][j][k] = 0; } } } cout<<tab[0][0][0]<<" "; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; for(int i=1; i<=n; i++) { cin>>s; for(int j=0; j<s.size(); j++) { if(s[j] == 'L') drb[i].pb(1); else drb[i].pb(-1); } } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) solve(i, j); 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 | #include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; // O(n^4) int tab[2][609][3]; vector<int> drb[609]; string s; const int M = 1e9 + 7; void solve(int x, int y) { int n = drb[x].size() + drb[y].size(), wyn = 0, a, b; for(int i=0; i<=1; i++) for(int j=0; j<=600; j++) for(int k=0; k<=2; k++) tab[i][j][k] = 0; tab[0][0][1] = 1; for(int i=0; i<n; i++) { if(i < drb[x].size()) b = drb[x][i]; else b = drb[y][i - drb[x].size()]; for(int k=0; k<=n; k++) { a = k + b; if(k > 600) break; if(a < 0 || a > 600) continue; if(b == 1) { tab[1][a][0] = (tab[1][a][0] + tab[0][k][1]) % M; tab[1][a][1] = (tab[1][a][1] + tab[0][k][1]) % M; tab[1][a][2] = (tab[1][a][2] + tab[0][k][1]) % M; tab[0][k][1] = 0; } else { tab[1][a][0] = (tab[1][a][0] + tab[0][k][2]) % M; tab[1][a][1] = (tab[1][a][1] + tab[0][k][2]) % M; tab[1][a][2] = (tab[1][a][2] + tab[0][k][2]) % M; tab[0][k][2] = 0; } } for(int j=0; j<=n; j++) { if(j > 600) break; for(int k=0; k<=2; k++) { tab[0][j][k] = (tab[0][j][k] + tab[1][j][k]) % M; tab[1][j][k] = 0; } } } cout<<tab[0][0][0]<<" "; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin>>n; for(int i=1; i<=n; i++) { cin>>s; for(int j=0; j<s.size(); j++) { if(s[j] == 'L') drb[i].pb(1); else drb[i].pb(-1); } } for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) solve(i, j); cout<<"\n"; } return 0; } |