제출 #1117613

#제출 시각아이디문제언어결과실행 시간메모리
1117613vjudge1괄호 문자열 (CEOI16_match)C++17
0 / 100
1 ms336 KiB
#include <bits/stdc++.h> using namespace std; bool isValid(const string &str) { stack<char> s; for (const char &c : str) { if (c == '(') { s.push(c); } else { if (s.empty() or s.top() != '(') { return false; } s.pop(); } } return s.empty(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string s; size_t p1 = string::npos, p2 = string::npos; vector<pair<size_t, size_t>> m; cin >> s; while (true) { p1 = s.find_first_of('a'); p2 = s.find_last_of('a'); m.push_back({p1, p2}); if (p1 == p2) { if (p1 != string::npos) { goto fail; } break; } s[p1] = '('; s[p2] = ')'; } while (true) { p1 = s.find_first_of('b'); p2 = s.find_last_of('b'); m.push_back({p1, p2}); if (p1 == p2) { if (p1 != string::npos) { goto fail; } break; } s[p1] = '('; s[p2] = ')'; } if (!isValid(s)) { goto fail; } for (const auto &[x, y] : m) { if (x != y - 1 and !isValid(s.substr(x + 1, y - x - 1))) { goto fail; } } cout << s << '\n'; return 0; fail: cout << -1 << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...