#include <bits/stdc++.h> using namespace std; #define pb push_back const int MAXN=607; string words[MAXN]; int count(string s){ int n=(int)s.size(); //~ cout<<s<<"\n"; int N=(1<<n); vector<string> good; for(int mask=1;mask<N;++mask){ int curr=0; string str=""; for(int i=0;i<n;++i){ if((1<<i)&mask){ if(s[i]=='L') ++curr; else --curr; str+=s[i]; if(curr<0) break; } } if(curr!=0) continue; good.pb(str); } sort(good.begin(),good.end()); //~ for(auto &v:good) cout<<v<<"~\n"; int res=(!good.empty()); for(int i=1;i<(int)good.size();++i){ if(good[i]!=good[i-1]) ++res; } return res; } int main() { int n; cin>>n; for(int i=1;i<=n;++i) cin>>words[i]; for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ string s=words[i]+words[j]; cout<<count(s)<<" "; } cout<<"\n"; } //~ string s="(()()))"; //~ cout<<Balanced_Substring(s,7)<<"\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 | #include <bits/stdc++.h> using namespace std; #define pb push_back const int MAXN=607; string words[MAXN]; int count(string s){ int n=(int)s.size(); //~ cout<<s<<"\n"; int N=(1<<n); vector<string> good; for(int mask=1;mask<N;++mask){ int curr=0; string str=""; for(int i=0;i<n;++i){ if((1<<i)&mask){ if(s[i]=='L') ++curr; else --curr; str+=s[i]; if(curr<0) break; } } if(curr!=0) continue; good.pb(str); } sort(good.begin(),good.end()); //~ for(auto &v:good) cout<<v<<"~\n"; int res=(!good.empty()); for(int i=1;i<(int)good.size();++i){ if(good[i]!=good[i-1]) ++res; } return res; } int main() { int n; cin>>n; for(int i=1;i<=n;++i) cin>>words[i]; for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ string s=words[i]+words[j]; cout<<count(s)<<" "; } cout<<"\n"; } //~ string s="(()()))"; //~ cout<<Balanced_Substring(s,7)<<"\n"; } |