Submission #309613

#TimeUsernameProblemLanguageResultExecution timeMemory
309613ant101Match (CEOI16_match)C++14
100 / 100
69 ms19320 KiB
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
#include <complex>
#include <array>
using namespace std;

#define ll long long

const ll k_Mods[2]{(ll)(1e9 + 7), (ll)(1e9 + 103)};
const ll k_Bases[2]{29, 43};

const int k_N = 1e5 + 7;

string s;
int n;

ll powers[2][k_N];

map<array<ll, 2>, vector<int>> c;
array<ll, 2> hash_arr[k_N];

bool check(string &curr, int pos, array<ll, 2> curr_hash){
    c[curr_hash].push_back(pos);
    hash_arr[pos] = curr_hash;
    
    if(curr == "" && pos == n)
        return true;
    if(curr.size() > (n - pos))
        return false;
    
    if(curr != "" && curr.back() == s[pos]){
        curr.pop_back();
        
        for(int i = 0; i <= 1; ++i){
            curr_hash[i] -= powers[i][curr.size()] * (s[pos] - 'a' + 1);
            curr_hash[i] %= k_Mods[i];
            if(curr_hash[i] < 0)
                curr_hash[i] += k_Mods[i];
        }
        
        bool answer = check(curr, pos + 1, curr_hash);
        curr += s[pos];
        
        return answer;
    }
    
    curr += s[pos];
    
    for(int i = 0; i <= 1; ++i){
        curr_hash[i] += powers[i][curr.size() - 1] * (s[pos] - 'a' + 1);
        curr_hash[i] %= k_Mods[i];
    }
    
    bool answer = check(curr, pos + 1, curr_hash);
    curr.pop_back();
    
    return answer;
}

char answer[k_N];
void solve(int l, int r){
    if(l > r)
        return;
    
    answer[l] = '(';
    
    vector<int> &v = c[hash_arr[l + 1]];
    int x = *(upper_bound(v.begin(), v.end(), r + 1) - 1);
    
    answer[x] = ')';
    
    solve(l + 1, x - 1);
    solve(x + 1, r);
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    
    cin >> s;
    n = s.size();
    
    powers[0][0] = 1;
    powers[1][0] = 1;
    
    for(int i = 0; i <= 1; ++i)
        for(int j = 1; j <= n; ++j)
            powers[i][j] = (powers[i][j - 1] * k_Bases[i]) % k_Mods[i];
    
    string curr = "";
    if(!check(curr, 0, {0, 0})){
        cout << "-1\n";
        return 0;
    }
    
    solve(0, n - 1);
    
    for(int i = 0; i < n; ++i)
        cout << answer[i];
    cout << "\n";
}

Compilation message (stderr)

match.cpp: In function 'bool check(std::string&, int, std::array<long long int, 2>)':
match.cpp:42:20: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   42 |     if(curr.size() > (n - pos))
      |        ~~~~~~~~~~~~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...