답안 #1117881

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1117881 2024-11-24T09:14:25 Z vjudge1 괄호 문자열 (CEOI16_match) C++17
10 / 100
1 ms 336 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define inf 0x3F3F3F3F3F3F3F3F

const int MXN = 3e6 + 5;
const int mod = 998244353;

int n;
string s, res;

void f(int l, int r)
{
  if (l > r) return;
  int sz = r - l + 1, last = -1;
  for (int i = l + 1; i <= r; i++)
  {
    if (s[i] == s[l]) last = i;
  }
  if (last == -1)
  {
    cout << -1 << '\n';
    exit(0);
  }
  res[l] = '(', res[last] = ')';
  f(l + 1, last - 1), f(last + 1, r);
}

void _()
{
  cin >> s;
  n = s.length();
  res = string(n, '#');
  f(0, n - 1);
  vector<int> v;
  for (int i = 0; i < n; i++)
  {
    if (res[i] == ')' && v.empty())
    {
      cout << -1 << '\n';
      exit(0);
    }
    if (res[i] == ')' && s[v.back()] != s[i]) 
    {
      cout << -1 << '\n';
      exit(0);
    } 
    if (res[i] == ')') v.pop_back();
    else v.push_back(i);
  }
  cout << res << '\n';
}

signed main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin >> t;
  while (t--) _();
}

Compilation message

match.cpp: In function 'void f(long long int, long long int)':
match.cpp:17:7: warning: unused variable 'sz' [-Wunused-variable]
   17 |   int sz = r - l + 1, last = -1;
      |       ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Incorrect 1 ms 336 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Incorrect 1 ms 336 KB Output isn't correct
5 Halted 0 ms 0 KB -