#pragma GCC optimize("O3,unroll-loops") #include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const int LIM=607, MOD=1e9+7; string S[LIM]; int dp[LIM][LIM], pref[LIM][LIM][2], suf[LIM][LIM][2], zero[LIM], same[LIM]; void licz(string s) { rep(i, s.size()) rep(j, s.size()+1) dp[i][j]=0; rep(i, s.size()) if(s[i]=='L') { dp[i][1]=1; break; } rep(i, s.size()) { if(s[i]=='L') { for(int j=1; j<=s.size(); ++j) { for(int l=i-1; l>=0; --l) { dp[i][j]+=dp[l][j-1]; if(dp[i][j]>=MOD) dp[i][j]-=MOD; if(s[l]=='L') break; } } } else { rep(j, s.size()) { for(int l=i-1; l>=0; --l) { dp[i][j]+=dp[l][j+1]; if(dp[i][j]>=MOD) dp[i][j]-=MOD; if(s[l]=='P') break; } } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i, n) { cin >> S[i]; same[i]=1; for(auto j : S[i]) if(j=='L') same[i]=0; licz(S[i]); rep(j, S[i].size()) { zero[i]+=dp[j][0]; if(zero[i]>=MOD) zero[i]-=MOD; } bool czyl=false, czyp=false; for(int j=S[i].size()-1; j>=0; --j) { rep(l, S[i].size()+1) { if(!czyl) { pref[i][l][0]+=dp[j][l]; if(pref[i][l][0]>=MOD) pref[i][l][0]-=MOD; } if(!czyp) { pref[i][l][1]+=dp[j][l]; if(pref[i][l][1]>=MOD) pref[i][l][1]-=MOD; } } if(S[i][j]=='L') czyl=true; else czyp=true; } reverse(all(S[i])); rep(j, S[i].size()) if(S[i][j]=='L') S[i][j]='P'; else S[i][j]='L'; licz(S[i]); rep(j, S[i].size()) rep(l, S[i].size()+1) { if(S[i][j]=='L') { suf[i][l][1]+=dp[j][l]; if(suf[i][l][1]>=MOD) suf[i][l][1]-=MOD; } else { suf[i][l][0]+=dp[j][l]; if(suf[i][l][0]>=MOD) suf[i][l][0]-=MOD; } } } rep(i, n) { rep(j, n) { ll ans=zero[i]; rep(l, LIM) ans=(ans+(ll)pref[i][l][0]*(ll)suf[j][l][0]+(ll)pref[i][l][1]*(ll)suf[j][l][1])%(ll)MOD; if(same[i]) ans=zero[j]; cout << ans << " "; } 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 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 | #pragma GCC optimize("O3,unroll-loops") #include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const int LIM=607, MOD=1e9+7; string S[LIM]; int dp[LIM][LIM], pref[LIM][LIM][2], suf[LIM][LIM][2], zero[LIM], same[LIM]; void licz(string s) { rep(i, s.size()) rep(j, s.size()+1) dp[i][j]=0; rep(i, s.size()) if(s[i]=='L') { dp[i][1]=1; break; } rep(i, s.size()) { if(s[i]=='L') { for(int j=1; j<=s.size(); ++j) { for(int l=i-1; l>=0; --l) { dp[i][j]+=dp[l][j-1]; if(dp[i][j]>=MOD) dp[i][j]-=MOD; if(s[l]=='L') break; } } } else { rep(j, s.size()) { for(int l=i-1; l>=0; --l) { dp[i][j]+=dp[l][j+1]; if(dp[i][j]>=MOD) dp[i][j]-=MOD; if(s[l]=='P') break; } } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; rep(i, n) { cin >> S[i]; same[i]=1; for(auto j : S[i]) if(j=='L') same[i]=0; licz(S[i]); rep(j, S[i].size()) { zero[i]+=dp[j][0]; if(zero[i]>=MOD) zero[i]-=MOD; } bool czyl=false, czyp=false; for(int j=S[i].size()-1; j>=0; --j) { rep(l, S[i].size()+1) { if(!czyl) { pref[i][l][0]+=dp[j][l]; if(pref[i][l][0]>=MOD) pref[i][l][0]-=MOD; } if(!czyp) { pref[i][l][1]+=dp[j][l]; if(pref[i][l][1]>=MOD) pref[i][l][1]-=MOD; } } if(S[i][j]=='L') czyl=true; else czyp=true; } reverse(all(S[i])); rep(j, S[i].size()) if(S[i][j]=='L') S[i][j]='P'; else S[i][j]='L'; licz(S[i]); rep(j, S[i].size()) rep(l, S[i].size()+1) { if(S[i][j]=='L') { suf[i][l][1]+=dp[j][l]; if(suf[i][l][1]>=MOD) suf[i][l][1]-=MOD; } else { suf[i][l][0]+=dp[j][l]; if(suf[i][l][0]>=MOD) suf[i][l][0]-=MOD; } } } rep(i, n) { rep(j, n) { ll ans=zero[i]; rep(l, LIM) ans=(ans+(ll)pref[i][l][0]*(ll)suf[j][l][0]+(ll)pref[i][l][1]*(ll)suf[j][l][1])%(ll)MOD; if(same[i]) ans=zero[j]; cout << ans << " "; } cout << '\n'; } } |