Submission #699032

#TimeUsernameProblemLanguageResultExecution timeMemory
699032cpLover22Match (CEOI16_match)C++17
37 / 100
2080 ms616 KiB
#include <bits/stdc++.h>
using namespace std;

#define eb emplace_back
#define siz(x) (int)x.size()

const int N = 1e5 + 5;

int n;
string st; char res[N];

bool check(stack<int> s, int k) {
  stack<int> tmp = s;
  for (int i = k; i <= n; ++i)
    if (!siz(s)) s.emplace(i);
    else {
      if (st[i] == st[s.top()]) s.pop();
      else s.emplace(i);
    }
  return siz(s) == 0;
}

int main() {
  cin.tie(0)->sync_with_stdio(0); cout.tie(0);
  if (fopen("match.in", "r")) {
    freopen("match.in", "r", stdin);
    freopen("match.out", "w", stdout);
    freopen("log.txt", "w", stderr);
  }
  cin >> st;
  n = st.size(); st = ' ' + st;

  stack<int> cur;
  for (int i = 1; i <= n; ++i) {
    if (!cur.size()) {
      cur.emplace(i); res[i] = '(';
    } else {
      cur.emplace(i);
      if (check(cur, i + 1)) res[i] = '(';
      else {
        cur.pop();
        if (st[cur.top()] != st[i])
          return cout << -1, 0;
        else cur.pop(), res[i] = ')';
      }
    }
  }
  if (siz(cur)) return cout << -1, 0;
  for (int i = 1; i <= n; ++i)
    cout << res[i];
  return 0;
}

Compilation message (stderr)

match.cpp: In function 'int main()':
match.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     freopen("match.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     freopen("match.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     freopen("log.txt", "w", stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...