#include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) constexpr int mod = 1000000007; // based on: https://ideone.com/fkHhL8 and https://hdqdgqnpvfesazkq.quora.com/Solution-for-Brackets-Subsequences vector<vector<int>> f; vector<vector<int>> g; int solution(const std::string &s) { f.clear(); g.clear(); f.push_back(vector<int>(s.size()+1,0)); f.push_back(vector<int>(s.size()+1,0)); g.push_back(vector<int>(s.size()+1,0)); g.push_back(vector<int>(s.size()+1,0)); // f[0][x] denotes the number of distinct balanced // subsequences ending with '(' and having extra // (unmatched) open parenthesis equal to 'x'. for(int i=0;i<s.size();++i){ int x=(s[i]=='L')?1:-1; int y=(s[i]=='L')?0:1; for(int j=s.size()-1;j>=0;--j){ if (x==-1 and j==0) continue; f[y][j+x]=(g[0][j]+g[1][j]); } if (x==1) f[y][1]+=1; f[1-y]=g[1-y]; g = f; f[0].assign(s.size()+1,0); f[1].assign(s.size()+1,0); } return g[1][0]; } int main() { f.reserve(2000); g.reserve(2000); std::ios::sync_with_stdio(false); int t; cin >> t; vector<string> seqs(t); REP(i, t) { cin >> seqs[i]; } REP(i, t) { REP(j, t) { f.clear(); g.clear(); cout << solution(seqs[i]+seqs[j]); cout << ((j < t-1)? " " : "\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 | #include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <vector> #include <queue> #include <bitset> #include <utility> #include <stack> using namespace std; typedef long long LL; typedef pair<int,int> PII; typedef vector<int> VI; #define MP make_pair #define FOR(v,p,k) for(int v=(p);v<=(k);++v) #define FORD(v,p,k) for(int v=(p);v>=(k);--v) #define REP(i,n) for(int i=0;i<(n);++i) #define VAR(v,i) __typeof(i) v=(i) #define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i) #define PB push_back #define ST first #define ND second #define SIZE(x) (int)x.size() #define ALL(c) c.begin(),c.end() #define ODD(x) ((x)%2) #define EVEN(x) (!(ODD(x))) constexpr int mod = 1000000007; // based on: https://ideone.com/fkHhL8 and https://hdqdgqnpvfesazkq.quora.com/Solution-for-Brackets-Subsequences vector<vector<int>> f; vector<vector<int>> g; int solution(const std::string &s) { f.clear(); g.clear(); f.push_back(vector<int>(s.size()+1,0)); f.push_back(vector<int>(s.size()+1,0)); g.push_back(vector<int>(s.size()+1,0)); g.push_back(vector<int>(s.size()+1,0)); // f[0][x] denotes the number of distinct balanced // subsequences ending with '(' and having extra // (unmatched) open parenthesis equal to 'x'. for(int i=0;i<s.size();++i){ int x=(s[i]=='L')?1:-1; int y=(s[i]=='L')?0:1; for(int j=s.size()-1;j>=0;--j){ if (x==-1 and j==0) continue; f[y][j+x]=(g[0][j]+g[1][j]); } if (x==1) f[y][1]+=1; f[1-y]=g[1-y]; g = f; f[0].assign(s.size()+1,0); f[1].assign(s.size()+1,0); } return g[1][0]; } int main() { f.reserve(2000); g.reserve(2000); std::ios::sync_with_stdio(false); int t; cin >> t; vector<string> seqs(t); REP(i, t) { cin >> seqs[i]; } REP(i, t) { REP(j, t) { f.clear(); g.clear(); cout << solution(seqs[i]+seqs[j]); cout << ((j < t-1)? " " : "\n"); } } } |