답안 #930253

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
930253 2024-02-19T07:56:49 Z ttamx 괄호 문자열 (CEOI16_match) C++14
10 / 100
1 ms 348 KB
#include<bits/stdc++.h>

using namespace std;

int n;
int tot[30],cnt[30];
string t,ans;
stack<int> s;

void compress(int i){
    ans[i]=')';
    while(!s.empty()&&t[s.top()]!=t[i]){
        int id=s.top();
        s.pop();
        compress(id);
    }
    if(s.empty())cout << -1,exit(0);
    int c=t[s.top()]-'a';
    cnt[c]-=2;
    tot[c]-=2;
    s.pop();
}

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> t;
    n=t.size();
    for(auto x:t)tot[x-'a']++;
    for(int i=0;i<26;i++)if(tot[i]&1)cout << -1,exit(0);
    for(int i=0;i<n;i++){
        int c=t[i]-'a';
        ans.push_back('(');
        if(++cnt[c]>tot[c]/2){
            compress(i);
        }else{
            s.emplace(i);
        }
    }
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Incorrect 1 ms 348 KB Output isn't correct
5 Halted 0 ms 0 KB -