#include <bits/stdc++.h> using namespace std; const int N = 601; const int MOD = 1e9+7; string s[N]; int n, dp[N][N]; int solve(int i, int j) { memset(dp, 0, sizeof(dp)); string k=s[i]+s[j]; int l=-1, p=-1, sz=k.size(); for (int i=0; i<sz; ++i) { if (k[i] == 'L') { dp[i][1]=1; break; } } if (k[0] == 'L') l=0; else p=0; for (int i=1; i<sz; ++i) { for (int j=0; j<N; ++j) { if (k[i] == 'L') { dp[i][j]+=dp[i-1][j]; if (j == 0) continue; (dp[i][j]+=dp[i-1][j-1])%=MOD; if (l-1 >= 0) dp[i][j]-=dp[l-1][j-1]; } else { dp[i][j]+=dp[i-1][j]; if (j+1 >= N) continue; (dp[i][j]+=dp[i-1][j+1])%=MOD; if (p-1 >= 0) dp[i][j]-=dp[p-1][j+1]; } if (dp[i][j] < 0) (dp[i][j]+=MOD)%=MOD; } if (k[i] == 'L') l=i; else p=i; } return dp[sz-1][0]; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin>>n; for (int i=1; i<=n; ++i) cin>>s[i]; for (int i=1; i<=n; ++i) { for (int j=1; j<=n; ++j) cout<<solve(i, j)<<" "; cout<<"\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 | #include <bits/stdc++.h> using namespace std; const int N = 601; const int MOD = 1e9+7; string s[N]; int n, dp[N][N]; int solve(int i, int j) { memset(dp, 0, sizeof(dp)); string k=s[i]+s[j]; int l=-1, p=-1, sz=k.size(); for (int i=0; i<sz; ++i) { if (k[i] == 'L') { dp[i][1]=1; break; } } if (k[0] == 'L') l=0; else p=0; for (int i=1; i<sz; ++i) { for (int j=0; j<N; ++j) { if (k[i] == 'L') { dp[i][j]+=dp[i-1][j]; if (j == 0) continue; (dp[i][j]+=dp[i-1][j-1])%=MOD; if (l-1 >= 0) dp[i][j]-=dp[l-1][j-1]; } else { dp[i][j]+=dp[i-1][j]; if (j+1 >= N) continue; (dp[i][j]+=dp[i-1][j+1])%=MOD; if (p-1 >= 0) dp[i][j]-=dp[p-1][j+1]; } if (dp[i][j] < 0) (dp[i][j]+=MOD)%=MOD; } if (k[i] == 'L') l=i; else p=i; } return dp[sz-1][0]; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin>>n; for (int i=1; i<=n; ++i) cin>>s[i]; for (int i=1; i<=n; ++i) { for (int j=1; j<=n; ++j) cout<<solve(i, j)<<" "; cout<<"\n"; } } |