답안 #390720

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
390720 2021-04-16T18:44:59 Z Vladth11 괄호 문자열 (CEOI16_match) C++14
10 / 100
2000 ms 241716 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];
int nxt[NMAX];
map <pii, int > mp;
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[l] == s[l + 1] && rez[l + 1] != '(' && rez[l + 1] != ')')
        last = l + 1;
    int node = nxt[l];
    while(node <= r){
        if(s[node] == s[l])
            last = node;
        node = nxt[node];
    }
    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 <ll> st;
    for(ll i = 0; i < s.size(); i++) {
        ll c = s[i] - 'a' + 1;
        nxt[i] = 2e9;
        inainte[i] = H.value;
        nxt[mp[{inainte[i], c}]] = i;
        if(st.size() && st.top() == c) {
            H.erase(st.top());
            st.pop();
        } else {
            H.insert(c);
            st.push(c);
        }
        dupa[i] = H.value;
        mp[{dupa[i], c}] = i;
    }

    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:64:9: warning: unused variable 'pz' [-Wunused-variable]
   64 |     int pz;
      |         ^~
match.cpp: In function 'int main()':
match.cpp:90: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]
   90 |     for(ll i = 0; i < s.size(); i++) {
      |                   ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 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 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Execution timed out 2087 ms 241716 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Execution timed out 2087 ms 241716 KB Time limit exceeded
5 Halted 0 ms 0 KB -