답안 #390715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390715 2021-04-16T18:19:42 Z Vladth11 괄호 문자열 (CEOI16_match) C++14
0 / 100
1 ms 332 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;

class Hashes {
    ll base, mod;
public:
    ll value = 0;
    Hashes(ll _base, ll _MOD) {
        base = _base;
        mod = _MOD;
    }
    ll lgput(ll n, ll p) {
        ll r = 1;
        while(p) {
            if(p % 2) {
                r *= n;
                r %= mod;
            }
            n *= n;
            n %= mod;
            p /= 2;
        }
        return r;
    }
    void insert(ll x) {
        value = value * base;
        value += x;
        value %= mod;
    }
    void erase(ll x) {
        value -= x;
        if(value < 0) {
            value += mod;
        }
        value = (value * lgput(base, mod - 2)) % mod;
    }
} H(31, MOD);

int inainte[NMAX];
int dupa[NMAX];
set <int> v[28];
string rez;
string s;

void solve(int l, int r) {
    int pz;
    if(l > r)
        return;
    stack <int> st;
    int last = 8;
    if(st.empty() && s[r] == s[r - 1] && rez[r - 1] != '(' && rez[r - 1] != ')')
        last = r - 1;
    for(ll i = r - 1; i >= 1; i--) {
        ll c = s[i] - 'a' + 1;
        if(st.size() && st.top() == c) {
            st.pop();
        } else {
            st.push(c);
        }
        if(st.empty() && s[r] == s[i - 1] && rez[i - 1] != '(' && rez[i - 1] != ')')
            last = i - 1;
    }
    rez[last] = '(';
    rez[r] = ')';
    solve(l, last - 1);
    solve(last + 1, r - 1);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    rez = s;
    stack <ll> st;
    for(ll i = 0; i < s.size(); i++) {
        ll c = s[i] - 'a' + 1;
        v[c].insert(i);
        inainte[i] = H.value;
        if(st.size() && st.top() == c) {
            H.erase(st.top());
            st.pop();
        } else {
            H.insert(c);
            st.push(c);
        }
        dupa[i] = H.value;
    }
    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:63:9: warning: unused variable 'pz' [-Wunused-variable]
   63 |     int pz;
      |         ^~
match.cpp: In function 'int main()':
match.cpp:93: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]
   93 |     for(ll i = 0; i < s.size(); i++) {
      |                   ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Halted 0 ms 0 KB -