답안 #1032662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1032662 2024-07-24T05:49:32 Z 정민찬(#10967) 괄호 문자열 (CEOI16_match) C++17
10 / 100
1 ms 604 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;

string s;

bool Check(vector<char> st, int idx) {
    for (int i=idx; i<s.size(); i++) {
        if (!st.empty() && st.back() == s[i]) st.pop_back();
        else st.push_back(s[i]);
    }
    return st.empty();
}

string Cal(int l, int r) {
    if (l > r) return "";
    int idx;
    for (int i=r; i>=l; i--) {
        if (s[i] == s[l]) {
            idx = i;
            break;
        }
    }
    assert(idx > l);
    return "(" + Cal(l+1, idx-1) + ")" + Cal(idx+1, r);
}

int main() {
    ios_base :: sync_with_stdio(false); cin.tie(NULL);
    cin >> s;
    if (!Check({}, 0)) {
        cout << "-1\n";
        return 0;
    }
    cout << Cal(0, s.size()-1) << '\n';
}

Compilation message

match.cpp: In function 'bool Check(std::vector<char>, int)':
match.cpp:11:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for (int i=idx; i<s.size(); i++) {
      |                     ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 452 KB Output is correct
4 Runtime error 1 ms 604 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 452 KB Output is correct
4 Runtime error 1 ms 604 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -