답안 #1117599

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1117599 2024-11-24T06:35:21 Z vjudge1 괄호 문자열 (CEOI16_match) C++17
0 / 100
1 ms 592 KB
#include <bits/stdc++.h>
using namespace std;

bool isValid(const string &str) {
  stack<char> s;
  // cout << endl;
  for (const char &c : str) {
    if (c == '(') {
      s.push(c);
      // cout << "Pushed " << c << '\n';
    } else {
      if (s.top() == '(') {
        s.pop();
        // cout << "Popped (\n";
      } else {
        // cout << c << " doesn't match )\n";
        return false;
      }
    }
  }

  if (!s.empty()) {
    // cout << s.top() << "is left. " << '\n';
  }
  return s.empty();
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  string s;
  vector<pair<size_t, size_t>> m;
  cin >> s;

  while (true) {
    size_t p1 = s.find_first_of('a');
    size_t p2 = s.find_last_of('a');
    m.push_back({p1, p2});
    if (p1 == p2) {
      break;
    }
    s[p1] = '(';
    s[p2] = ')';
  }

  while (true) {
    size_t p1 = s.find_first_of('b');
    size_t p2 = s.find_last_of('b');
    m.push_back({p1, p2});
    if (p1 == p2) {
      break;
    }
    s[p1] = '(';
    s[p2] = ')';
  }

  if (!isValid(s)) {
    cout << "-1\n";
    return 0;
  }

  for (const auto &[x, y] : m) {
    if (x != y - 1 and !isValid(s.substr(x + 1, y - x - 1))) {
      // cout << s.substr(x + 1, y - x - 1) << " is invalid.\n";
      cout << "-1\n";
      return 0;
    }
  }

  cout << s << '\n';
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Runtime error 1 ms 592 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Runtime error 1 ms 592 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Runtime error 1 ms 592 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -