# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
874898 | 2023-11-18T04:45:28 Z | Muhammad_Aneeq | 괄호 문자열 (CEOI16_match) | C++17 | 0 ms | 348 KB |
/* بسم الله الرحمن الرحيم 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; map<char,vector<int>>d; bool check(string s) { map<char,pair<int,int>>z; string ans=string(n,' '); for (int i=0;i<s.size();i++) { ans[i]=s[i]; if (ans[i]=='(') z[x[i]].first++; else z[x[i]].second++; } for (int i=s.size();i<x.size();i++) { if (z[x[i]].first>=d[x[i]].size()/2) { ans[i]=')'; z[x[i]].second++; } else { ans[i]='('; z[x[i]].first++; } } vector<char>q; for (int i=0;i<n;i++) { if (ans[i]==')') { if (q.size()==0) return 0; if (q.back()!=x[i]) return 0; q.pop_back(); } else q.push_back(x[i]); } return (q.size()==0); } inline void solve() { cin>>x; n=x.size(); 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; } int z=0; for (auto j:i.second) { if (j%2) z++; else z--; } if (z) { cout<<-1<<endl; return; } } string ans=""; for (int i=0;i<n;i++) { if (check(ans+'(')) ans+='('; else ans+=')'; } cout<<ans<<endl; } int main() { ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); solve(); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Incorrect | 0 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Incorrect | 0 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Incorrect | 0 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |