답안 #390724

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390724 2021-04-16T19:28:37 Z Vladth11 괄호 문자열 (CEOI16_match) C++14
100 / 100
11 ms 12248 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debug_with_space(x) cerr << #x << " " << x << " "

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> OST;

const ll NMAX = 100001;
const ll INF = (1LL << 60);
const ll MOD = 1000000007;
const ll BLOCK = 225;
const ll base = 31;
const ll nr_of_bits = 21;

int previous[NMAX][28];
string rez;
string s;
vector <int> v[28];

void solve(int l, int r) {
    int pz;
    if(l > r)
        return;
    if(s[l] == s[r]) {
        rez[l] = '(';
        rez[r] = ')';
        solve(l + 1, r - 1);
        return;
    }
    int last = previous[r][s[l] - 'a' + 1];
    rez[last] = ')';
    rez[l] = '(';
    solve(l + 1, last - 1);
    solve(last + 1, r);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    rez = s;
    stack <pii> st;
    for(ll i = 0; i < s.size(); i++) {
        ll c = s[i] - 'a' + 1;
        if(st.size() && st.top().first == c) {
            st.pop();
        } else {
            st.push({c, i});
        }
        int cn = 0;
        if(i > 0)
         cn = previous[i - 1][c];
        if(cn != 0) {
            for(int j = 1; j <= 26; j++) {
                    previous[i][j] = previous[cn - 1][j];
            }
        }
        previous[i][c] = i;
    }
    ///cum calculez pe previous[i][j] = x - de la x + 1...i e ok parantezarea, x e maxim si s[x] = j
    if(st.size()) {
        cout << -1;
        return 0;
    }
    solve(0, s.size() - 1);
    cout << rez;
    return 0;
}

Compilation message

match.cpp: In function 'void solve(int, int)':
match.cpp:27:9: warning: unused variable 'pz' [-Wunused-variable]
   27 |     int pz;
      |         ^~
match.cpp: In function 'int main()':
match.cpp:50:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |     for(ll i = 0; i < s.size(); i++) {
      |                   ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 1 ms 460 KB Output is correct
8 Correct 1 ms 972 KB Output is correct
9 Correct 2 ms 1100 KB Output is correct
10 Correct 1 ms 1104 KB Output is correct
11 Correct 1 ms 1100 KB Output is correct
12 Correct 6 ms 7244 KB Output is correct
13 Correct 6 ms 7884 KB Output is correct
14 Correct 7 ms 8656 KB Output is correct
15 Correct 9 ms 9420 KB Output is correct
16 Correct 10 ms 9436 KB Output is correct
17 Correct 9 ms 10060 KB Output is correct
18 Correct 9 ms 10572 KB Output is correct
19 Correct 10 ms 11468 KB Output is correct
20 Correct 8 ms 7500 KB Output is correct
21 Correct 11 ms 12248 KB Output is correct