답안 #1117879

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1117879 2024-11-24T09:14:09 Z vjudge1 괄호 문자열 (CEOI16_match) C++17
10 / 100
5 ms 20816 KB
#include <bits/stdc++.h>
#define int long long 
#define endl '\n' 
#define fi first
#define all(v) v.begin(), v.end()
#define se second
#define mp make_pair
#define pll pair<long long, long long>
#define mein ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
using namespace std; 
 
const int sz = 1e5 + 5;
string s, t;
int n, last[26][sz];
 
void f(int l, int r)
{
    if(l > r) return;
    int pos = last[s[l] - 'a'][r];
    assert(pos >= 0);
    t[l] = '(', t[pos] = ')';
    f(l + 1, pos - 1), f(pos + 1, r);
}
 
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> s;
    n = s.size();
    stack<int> st;
 
    for(int i = 0; i < n; i++)
    {
        if(!st.empty() && s[st.top()] == s[i])
        {
            st.pop();
        }
        else
        {
            st.push(i);
        }
    }
 
    if(!st.empty())
    {
        cout << -1;
        return 0;
    }
 
    memset(last, -1, sizeof(last));
    for(int i = 0; i < n; i++)
    {
        int l = s[i] - 'a';
        last[l][i] = i;
        if(i == 0) continue;
        for(int j = 0; j < 26; j++)
        {
            last[j][i] = max(last[j][i - 1], last[j][i]);
        }
    }
    
    t.resize(n);
    f(0, n - 1);
 
    cout << t;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 20816 KB Output is correct
2 Correct 4 ms 20560 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 20816 KB Output is correct
2 Correct 4 ms 20560 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Incorrect 5 ms 20816 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 20816 KB Output is correct
2 Correct 4 ms 20560 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Incorrect 5 ms 20816 KB Output isn't correct
5 Halted 0 ms 0 KB -