답안 #1117941

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1117941 2024-11-24T11:14:25 Z AtabayRajabli 괄호 문자열 (CEOI16_match) C++17
100 / 100
30 ms 26616 KB
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;

void f(int l, int r, string &s, string &t, vector<vector<int>> &last)
{
    if(l > r) return;
    int pos = last[r][s[l] - 'a'];
    t[l] = '(', t[pos] = ')';
    f(l + 1, pos - 1, s, t, last), f(pos + 1, r, s, t, last);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    string s;
    cin >> s;

    vector<vector<int>> last(s.size(), vector<int>(26, -1));
    stack<int> st;
    for(int i = 0; i < s.size(); i++)
    {
        if(!st.empty() && s[st.top()] == s[i]) st.pop();
        else st.push(i);
    }

    if(!st.empty())
    {
        cout << -1;
        return 0;
    }

    for(int i = 0; i < s.size(); i++)
    {
        last[i][s[i] - 'a'] = i;
        for(int j = 0; j < 26 && i > 0; j++)
        {
            int prv = last[i - 1][s[i] - 'a'];
            if(prv > 0 && s[i] - 'a' != j)
            {
                last[i][j] = last[prv - 1][j];
            }
        }
    }

    string t(s.size(), ' ');
    f(0, s.size() - 1, s, t, last);

    cout << t;
}

Compilation message

match.cpp: In function 'int main()':
match.cpp:24:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i = 0; i < s.size(); i++)
      |                    ~~^~~~~~~~~~
match.cpp:36:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for(int i = 0; i < s.size(); i++)
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 500 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 500 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 848 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 848 KB Output is correct
7 Correct 1 ms 720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 500 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 848 KB Output is correct
5 Correct 1 ms 592 KB Output is correct
6 Correct 1 ms 848 KB Output is correct
7 Correct 1 ms 720 KB Output is correct
8 Correct 3 ms 1872 KB Output is correct
9 Correct 2 ms 2128 KB Output is correct
10 Correct 3 ms 2124 KB Output is correct
11 Correct 2 ms 2128 KB Output is correct
12 Correct 17 ms 16208 KB Output is correct
13 Correct 19 ms 17488 KB Output is correct
14 Correct 30 ms 19156 KB Output is correct
15 Correct 23 ms 22088 KB Output is correct
16 Correct 26 ms 22096 KB Output is correct
17 Correct 27 ms 23392 KB Output is correct
18 Correct 22 ms 23376 KB Output is correct
19 Correct 24 ms 25168 KB Output is correct
20 Correct 16 ms 16464 KB Output is correct
21 Correct 27 ms 26616 KB Output is correct