# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
916741 | sleepntsheep | Match (CEOI16_match) | C++17 | 2 ms | 604 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
#include <complex>
using u32 = unsigned;
using i32 = int;
using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
using namespace std;
using pt = complex<f80>;
#define ALL(x) begin(x), end(x)
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 100005
void setio(string s)
{
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int n;
string s;
deque<int> q[300];
int vis[N];
int h[N<<1], ap[N];
int qry(int l, int r)
{
int z = 1e9;
for(l+=n,r+=n+1;l<r;l/=2,r/=2)
{
if(l&1)z=min(z,h[l++]);
if(r&1)z=min(h[--r],z);
}
return z;
}
int main()
{
setio("match");
ShinLena;
cin >> s;
n = s.size();
for (int i = 0; i < n; ++i) q[s[i]].push_back(i);
auto t = s;
for (int i = 0; i < n; ++i)
{
if (vis[i]) continue;
auto x = s[i];
auto l = i, r = q[x].back();
q[x].pop_back();
t[l] = '('; t[r] = ')';
vis[r] = 1;
}
for (int i = 0; i < n; ++i)
h[i+n] = h[i+n-1] + (t[i] == '(' ? 1 : -1);
for (int i = n; i--;) h[i] = min(h[i<<1], h[i<<1|1]);
for (int i = 0; i < n; ++i)
{
if (ap[h[i+n]])
{
int y = ap[h[i+n]] - 1;
if (s[y] != s[i] or qry(y+1, i-1) < 0)
{
cout << -1;
return 0;
}
}
if(i)
{
ap[0] = 1;
}
else
{
ap[h[i+n-1]] = i+1;
}
}
cout << t;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |