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 <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 (stderr)
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++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |