답안 #699023

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699023 2023-02-15T10:38:43 Z cpLover22 괄호 문자열 (CEOI16_match) C++17
10 / 100
1 ms 340 KB
#include <bits/stdc++.h>
using namespace std;

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

const int N = 1e5 + 5;

int n, c[N][26];
vector<int> pos[26];
string st; char res[N];

bool flag = 1;
void mat(int l, int r) {
  if (l > r) return;
  int p, t = st[l] - 'a';
  for (p = siz(pos[t]) - 1; p >= 0; --p) {
    bool flag = 1; int cp = pos[t][p];
    if (cp > r || cp <= l) continue;
    for (int i = 0; i < 26; ++i)
      if ((c[cp][i] - c[l - 1][i]) % 2) {
//        cerr << i << ' ' << l << ' ' << cp << '\n';
        flag = 0; break;
      }

    if (flag) break;
  }
//  cerr << l << ' ' << r << ":\n";
  if (p == -1) return flag = 0, void();
  p = pos[t][p];
//  cerr << l << ' ' << r << ' ' << p << '\n';
  res[l] = '(', res[p] = ')';
//  cerr << l << ' ' << p << '\n';
  mat(l + 1, p - 1); mat(p + 1, r);
}

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;
  for (int i = 1; i <= n; ++i)
    pos[st[i] - 'a'].eb(i);

  for (int i = 1; i <= n; ++i)
    for (int j = 0; j < 26; ++j)
      c[i][j] = c[i - 1][j] + (st[i] - 'a' == j);
//  cerr << "? " << c[]
  mat(1, n);
  if (!flag) cout << -1;
  else {
    for (int i = 1; i <= n; ++i)
      cout << res[i];
  }
  return 0;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:40:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |     freopen("match.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:41:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   41 |     freopen("match.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
match.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   42 |     freopen("log.txt", "w", stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Halted 0 ms 0 KB -