#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;
if(rez[r] == '(' || rez[r] == ')'){
solve(l, r - 1);
return;
}
stack <int> st;
int last = 0;
for(ll i = r; i >= 0; i--) {
ll c = s[i] - 'a' + 1;
if(rez[i] == '(' || rez[i] == ')'){
continue;
}
if(st.size() && st.top() == c) {
st.pop();
} else {
st.push(c);
}
if(st.empty() && s[r] == s[i])
last = i;
}
rez[last] = '(';
rez[r] = ')';
solve(l, 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:97: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]
97 | for(ll i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 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 |
332 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 |
332 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |