제출 #874878

#제출 시각아이디문제언어결과실행 시간메모리
874878Muhammad_Aneeq괄호 문자열 (CEOI16_match)C++17
10 / 100
2062 ms1372 KiB
/* بسم الله الرحمن الرحيم Author: (:Muhammad Aneeq:) */ #pragma GCC optimize("O2") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include <iostream> #include <map> #include <vector> #include <algorithm> using namespace std; string ans=""; string x; int n; void dfs(int i,string s="") { if (i==n) { // cout<<s<<endl; vector<int>z; for (int j=0;j<n;j++) { if (s[j]==')') { if (z.size()==0) return; if (x[z.back()]!=x[j]) return; z.pop_back(); } else z.push_back(j); } if (z.size()) return; if (ans=="") ans=s; ans=min(ans,s); return; } dfs(i+1,s+'('); dfs(i+1,s+')'); } inline void solve() { cin>>x; n=x.size(); map<char,vector<int>>d; for (int i=0;i<n;i++) d[x[i]].push_back(i); for (auto i:d) { if (i.second.size()%2) { cout<<-1<<endl;return; } } for (auto i:d) { vector<int>x,y; for (auto j:i.second) if (j%2) x.push_back(j); else y.push_back(j); if (x.size()!=y.size()) { cout<<-1<<endl;return; } } dfs(0); cout<<ans<<endl; } int main() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...