#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<int,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<ll>::iterator sit;
typedef map<ll,ll>::iterator mit;
bool err;
string solve(string &s)
{
if(s.empty()) return "";
int n = s.length();
stack<char> S;
string ans;
int idx=-1;
if(s[0]==s[n-1])
{
string s2 = s.substr(1,int(s.length()) - 2);
s2=solve(s2);
if(err) return "";
return "("+s2+")";
}
for(int i = n - 1; i > 0; i--)
{
if(!S.empty()&&S.top()==s[i])
{
S.pop();
}
else
{
S.push(s[i]);
}
if(s[0]==s[i-1]&&S.empty())
{
idx=i;
break;
}
}
if(idx<=0)
{
err=true;
return "";
}
string l = s.substr(1,idx-2);
string r = s.substr(idx,int(s.length()) - idx);
l = solve(l);
if(err) return "";
r = solve(r);
if(err) return "";
return "("+l+")"+r;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
string s; cin>>s;
int n=s.length();
/*
for(int i=0;i<n;i++)
{
stack<char> S;
for(int j=i;j<n;j++)
{
if(!S.empty()&&S.top()==s[j])
{
S.pop();
}
else S.push(s[j]);
if(S.empty())
{
//cerr<<i<<' '<<j<<'\n';
dp[i][j]=1;
}
}
}
*/
string ans = solve(s);
if(err) cout<<-1<<'\n';
else cout<<ans<<'\n';
}
Compilation message
match.cpp: In function 'int main()':
match.cpp:73:6: warning: unused variable 'n' [-Wunused-variable]
int n=s.length();
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2184 KB |
Output is correct |
2 |
Correct |
0 ms |
2184 KB |
Output is correct |
3 |
Correct |
0 ms |
2184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2184 KB |
Output is correct |
2 |
Correct |
0 ms |
2184 KB |
Output is correct |
3 |
Correct |
0 ms |
2184 KB |
Output is correct |
4 |
Correct |
0 ms |
2316 KB |
Output is correct |
5 |
Correct |
0 ms |
2316 KB |
Output is correct |
6 |
Runtime error |
0 ms |
3316 KB |
Execution killed because of forbidden syscall writev (20) |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2184 KB |
Output is correct |
2 |
Correct |
0 ms |
2184 KB |
Output is correct |
3 |
Correct |
0 ms |
2184 KB |
Output is correct |
4 |
Correct |
0 ms |
2316 KB |
Output is correct |
5 |
Correct |
0 ms |
2316 KB |
Output is correct |
6 |
Runtime error |
0 ms |
3316 KB |
Execution killed because of forbidden syscall writev (20) |
7 |
Halted |
0 ms |
0 KB |
- |