#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int nax=1e5+5;
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
vector<char> ans(nax);
string t;
int dp[nax][26];
void rec(int l,int r){
if(l>r) return;
ans[l]='(';
ans[dp[r][t[l]-'a']]=')';
rec(l+1,dp[r][t[l]-'a']-1);
rec(dp[r][t[l]-'a']+1,r);
}
int main()
{
optimise;
cin>>t;
int n=t.size();
reverse(t.begin(),t.end());
t.pb(' ');
reverse(t.begin(),t.end());
stack<char> st;
for (int i = 0; i <= n; ++i)
{
if(!st.empty()&&st.top()==t[i]) st.pop();
else st.push(t[i]);
}
st.pop();
if(st.size()){
cout <<-1<<endl;
return 0;
}
memset(dp,0,sizeof dp);
for (int i = 1; i <= n; ++i)
{
dp[i][t[i]-'a']=i;
if(i==1||dp[i][t[i]-'a']==0) continue;
for (int j = 0; j < 26; ++j)
{
if(j!=(t[i]-'a')) dp[i][j]=dp[dp[i-1][t[i]-'a']-1][j];
}
}
rec(1,n);
for (int i = 1; i <= n; ++i)
{
cout <<ans[i];
}cout <<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
12 ms |
21336 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10588 KB |
Output is correct |
2 |
Correct |
4 ms |
10588 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
12 ms |
21336 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |