#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define speed ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define sz(x) (int)x.size()
#define len(x) (int)strlen(x)
#define all(x) x.begin(), x.end()
#define debug cerr << "OK\n";
#define ub upper_bound
#define lb lower_bound
#define make_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())
mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rofl(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef set<int> si;
typedef set<ll> sll;
typedef set<pii> spii;
typedef set<pll> spll;
typedef multiset <int> msi;
typedef multiset <ll> msll;
typedef map <int, int> mi;
typedef map <ll, ll> mll;
const int N = 1e5 + 2;
const int M = 1e5;
const int inf = 2e9 + 3;
const ll INF = 1e18;
const ld pi2 = 2.0 * 3.141592653589793;
const ld pi = 3.141592653589793;
const ld eps = 1e-12;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
string s, t;
int n;
int dp[N][26];
void solve(int l, int r) {
if (l > r) return;
t[l] = '(';
int ch = (s[l] - 'a');
if (dp[r][ch] < l) return;
int p = dp[r][ch];
t[p] = ')';
solve(l + 1, p - 1);
solve(p + 1, r);
}
int main() {
speed;
cin >> s;
n = sz(s);
t = string(n, '0');
stack<pair<int, int>> st;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 26; ++j) {
dp[i][j] = (i > 0 ? dp[i - 1][j] : -1);
}
int ch = (s[i] - 'a');
if (sz(st) && st.top().F == ch) {
int p = st.top().S;
for (int j = 0; j < 26; ++j) {
if (p > 0) {
if (s[p - 1] - 'a' == j)
dp[i][j] = p - 1;
else
dp[i][j] = dp[p - 1][j];
}
}
st.pop();
}
else {
st.push({ch, i});
}
dp[i][ch] = i;
}
solve(0, n - 1);
bool F = 1;
for (auto c : t) if (c == '0') F = 0;
if (!F) cout << -1 << '\n';
else cout << t << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |