#include <bits/stdc++.h> using namespace std; const int MOD=1e9+7; int solve(string s) { int n=s.length(); vector dp(n+2, vector(n, 0)); dp[0][0]=1; int l[2]={n+2, n+2}; for (int i=1; i<=n; i++) { if (s[i-1]=='L') { dp[i][0]=dp[i-1][0]; for (int j=1; j<=n/2; j++) { dp[i][j]=dp[i-1][j]+dp[i-1][j-1]-dp[l[0]-1][j-1]; if (dp[i][j]>=MOD) dp[i][j]-=MOD; if (dp[i][j]<0) dp[i][j]+=MOD; } l[0]=i; } else { for (int j=0; j<=n/2; j++) { dp[i][j]=dp[i-1][j]+dp[i-1][j+1]-dp[l[1]-1][j+1]; if (dp[i][j]>=MOD) dp[i][j]-=MOD; if (dp[i][j]<0) dp[i][j]+=MOD; } l[1]=i; } //~ for (int j=0; j<n; j++) cout<<dp[i][j]<<" "; //~ cout<<"\n"; } return (dp[n][0]==0? dp[n][0]-1+MOD : dp[n][0]-1); } int main() { //~ string s; cin>>s; //~ cout<<solve(s); ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; vector<string>st(n); for (auto &i : st) cin>>i; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) cout<<solve(st[i]+st[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 | #include <bits/stdc++.h> using namespace std; const int MOD=1e9+7; int solve(string s) { int n=s.length(); vector dp(n+2, vector(n, 0)); dp[0][0]=1; int l[2]={n+2, n+2}; for (int i=1; i<=n; i++) { if (s[i-1]=='L') { dp[i][0]=dp[i-1][0]; for (int j=1; j<=n/2; j++) { dp[i][j]=dp[i-1][j]+dp[i-1][j-1]-dp[l[0]-1][j-1]; if (dp[i][j]>=MOD) dp[i][j]-=MOD; if (dp[i][j]<0) dp[i][j]+=MOD; } l[0]=i; } else { for (int j=0; j<=n/2; j++) { dp[i][j]=dp[i-1][j]+dp[i-1][j+1]-dp[l[1]-1][j+1]; if (dp[i][j]>=MOD) dp[i][j]-=MOD; if (dp[i][j]<0) dp[i][j]+=MOD; } l[1]=i; } //~ for (int j=0; j<n; j++) cout<<dp[i][j]<<" "; //~ cout<<"\n"; } return (dp[n][0]==0? dp[n][0]-1+MOD : dp[n][0]-1); } int main() { //~ string s; cin>>s; //~ cout<<solve(s); ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin>>n; vector<string>st(n); for (auto &i : st) cin>>i; for (int i=0; i<n; i++) { for (int j=0; j<n; j++) cout<<solve(st[i]+st[j])<<" "; cout<<"\n"; } } |