#include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=605, INF=1e9+5, mod=1e9+7; struct modint{ int n=0; modint(){} modint(ll x){ n=x%mod; if(n<0)n+=mod; } operator int(){ return n; } modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;} modint operator+=(modint a){return (*this)=(*this)+a;} modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;} modint operator-=(modint a){return (*this)=(*this)-a;} modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;} modint operator*=(modint a){return (*this)=(*this)*a;} modint operator^(const ll &m)const{ modint a(1); if(m==0)return a; if(m==1)return (*this); a=(*this)^(m/2); a*=a; return a*((*this)^(m&1)); } modint odw(){ return (*this)^((ll)mod-2); } modint operator/(modint a){return (*this)*a.odw();} modint operator/=(modint a){return (*this)=(*this)/a;} bool operator==(modint a){return a.n==n;} friend ostream& operator<<(ostream& os, modint m) { return os << m.n; } }; modint dp[N][N]; modint ile[N][N][2], ile2[N][N][2]; modint pel[N]; int nxt[N][2]; int main(){ //BOOST; int n; cin>>n; vector<str> s(n); for(auto &t:s)cin>>t; for(int i=0; i<n; i++){ rev(s[i]); nxt[s[i].size()][0]=nxt[s[i].size()][1]=s[i].size(); //deb(s[i]); for(int j=s[i].size()-1; j>=0; j--){ nxt[j][s[i][j]=='P']=nxt[j+1][s[i][j]=='P']; nxt[j][s[i][j]=='L']=j; } dp[nxt[0][0]][1]=1; for(int j=0; j<s[i].size(); j++){ //cerr<<nxt[j][0]<<" "<<nxt[j][1]<<", "; for(int k=0; k<=j+1; k++){ //cerr<<dp[j][k]<<" \n"[k==j+1]; if(k)dp[nxt[j+1][1]][k-1]+=dp[j][k]; dp[nxt[j+1][0]][k+1]+=dp[j][k]; ile[i][k][s[i][j]=='L']+=dp[j][k]; } } for(int j=0; j<=s[i].size(); j++){ for(int k=0; k<=s[i].size()+1; k++){ dp[j][k]=0; } } //cerr<<"\n";for(int j=0; j<=s.size(); j++){cerr<<"("<<ile[i][j][0]<<" "<<ile[i][j][1]<<")"<<" \n"[j==s.size()];}cerr<<"\n"; rev(s[i]); } for(int i=0; i<n; i++){ //deb(s[i]); nxt[s[i].size()][0]=nxt[s[i].size()][1]=s[i].size(); for(int j=s[i].size()-1; j>=0; j--){ nxt[j][s[i][j]=='P']=nxt[j+1][s[i][j]=='P']; nxt[j][s[i][j]=='L']=j; } dp[nxt[0][1]][1]=1; if(nxt[0][1]==s[i].size())ile2[i][0][0]+=1; //deb(dp[1][2]); for(int j=0; j<s[i].size(); j++){ //cerr<<nxt[j][0]<<" "<<nxt[j][1]<<", "; for(int k=0; k<=j+1; k++){ //cerr<<dp[j][k]<<" \n"[k==j+1]; if(k){ if(nxt[j+1][0]!=s[i].size())dp[nxt[j+1][0]][k-1]+=dp[j][k]; else ile2[i][k][1]+=dp[j][k]; } else pel[i]+=dp[j][k]; if(nxt[j+1][1]!=s[i].size())dp[nxt[j+1][1]][k+1]+=dp[j][k]; else ile2[i][k][0]+=dp[j][k]; } } for(int j=0; j<=s[i].size(); j++){ for(int k=0; k<=s[i].size()+1; k++){ dp[j][k]=0; } } //cerr<<"\n";for(int j=0; j<=s.size(); j++){cerr<<"("<<ile2[i][j][0]<<" "<<ile2[i][j][1]<<")"<<" \n"[j==s.size()];}cerr<<"\n"; } for(int i=0; i<n; i++){ //deb(pel[i]); for(int j=0; j<n; j++){ modint ans=0; for(int k=0; k<=min(s[i].size(), s[j].size()); k++){ ans+=ile2[i][k][0]*ile[j][k][1]; ans+=ile2[i][k][1]*ile[j][k][0]; } cout<<ans+pel[i]<<" "; } 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 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include<bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define pp pop_back #define eb emplace_back #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=605, INF=1e9+5, mod=1e9+7; struct modint{ int n=0; modint(){} modint(ll x){ n=x%mod; if(n<0)n+=mod; } operator int(){ return n; } modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;} modint operator+=(modint a){return (*this)=(*this)+a;} modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;} modint operator-=(modint a){return (*this)=(*this)-a;} modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;} modint operator*=(modint a){return (*this)=(*this)*a;} modint operator^(const ll &m)const{ modint a(1); if(m==0)return a; if(m==1)return (*this); a=(*this)^(m/2); a*=a; return a*((*this)^(m&1)); } modint odw(){ return (*this)^((ll)mod-2); } modint operator/(modint a){return (*this)*a.odw();} modint operator/=(modint a){return (*this)=(*this)/a;} bool operator==(modint a){return a.n==n;} friend ostream& operator<<(ostream& os, modint m) { return os << m.n; } }; modint dp[N][N]; modint ile[N][N][2], ile2[N][N][2]; modint pel[N]; int nxt[N][2]; int main(){ //BOOST; int n; cin>>n; vector<str> s(n); for(auto &t:s)cin>>t; for(int i=0; i<n; i++){ rev(s[i]); nxt[s[i].size()][0]=nxt[s[i].size()][1]=s[i].size(); //deb(s[i]); for(int j=s[i].size()-1; j>=0; j--){ nxt[j][s[i][j]=='P']=nxt[j+1][s[i][j]=='P']; nxt[j][s[i][j]=='L']=j; } dp[nxt[0][0]][1]=1; for(int j=0; j<s[i].size(); j++){ //cerr<<nxt[j][0]<<" "<<nxt[j][1]<<", "; for(int k=0; k<=j+1; k++){ //cerr<<dp[j][k]<<" \n"[k==j+1]; if(k)dp[nxt[j+1][1]][k-1]+=dp[j][k]; dp[nxt[j+1][0]][k+1]+=dp[j][k]; ile[i][k][s[i][j]=='L']+=dp[j][k]; } } for(int j=0; j<=s[i].size(); j++){ for(int k=0; k<=s[i].size()+1; k++){ dp[j][k]=0; } } //cerr<<"\n";for(int j=0; j<=s.size(); j++){cerr<<"("<<ile[i][j][0]<<" "<<ile[i][j][1]<<")"<<" \n"[j==s.size()];}cerr<<"\n"; rev(s[i]); } for(int i=0; i<n; i++){ //deb(s[i]); nxt[s[i].size()][0]=nxt[s[i].size()][1]=s[i].size(); for(int j=s[i].size()-1; j>=0; j--){ nxt[j][s[i][j]=='P']=nxt[j+1][s[i][j]=='P']; nxt[j][s[i][j]=='L']=j; } dp[nxt[0][1]][1]=1; if(nxt[0][1]==s[i].size())ile2[i][0][0]+=1; //deb(dp[1][2]); for(int j=0; j<s[i].size(); j++){ //cerr<<nxt[j][0]<<" "<<nxt[j][1]<<", "; for(int k=0; k<=j+1; k++){ //cerr<<dp[j][k]<<" \n"[k==j+1]; if(k){ if(nxt[j+1][0]!=s[i].size())dp[nxt[j+1][0]][k-1]+=dp[j][k]; else ile2[i][k][1]+=dp[j][k]; } else pel[i]+=dp[j][k]; if(nxt[j+1][1]!=s[i].size())dp[nxt[j+1][1]][k+1]+=dp[j][k]; else ile2[i][k][0]+=dp[j][k]; } } for(int j=0; j<=s[i].size(); j++){ for(int k=0; k<=s[i].size()+1; k++){ dp[j][k]=0; } } //cerr<<"\n";for(int j=0; j<=s.size(); j++){cerr<<"("<<ile2[i][j][0]<<" "<<ile2[i][j][1]<<")"<<" \n"[j==s.size()];}cerr<<"\n"; } for(int i=0; i<n; i++){ //deb(pel[i]); for(int j=0; j<n; j++){ modint ans=0; for(int k=0; k<=min(s[i].size(), s[j].size()); k++){ ans+=ile2[i][k][0]*ile[j][k][1]; ans+=ile2[i][k][1]*ile[j][k][0]; } cout<<ans+pel[i]<<" "; } cout<<"\n"; } } |