이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define N 100050
#define f first
#define s second
using namespace std;
typedef pair<int, int> pii;
int n, v[N];
int qtd[N][30];
string s;
vector<int> pos[N];
char resp[N];
int solve(int l, int r)
{
if(r < l) return 1;
if(r - l == 1)
{
resp[l] = '(', resp[r] = ')';
return (v[l] == v[r]);
}
if(l == r) return 0;
stack<int> open;
for(int j = r; j > l; j--)
{
if(!open.empty())
{
int t = open.top();
if(t == v[j]) open.pop();
else open.push(v[j]);
}
else
{
if(v[l] == v[j])
{
resp[l] = '(', resp[j] = ')';
return (solve(l + 1, j - 1) and solve(j + 1, r));
}
open.push(v[j]);
}
}
return 0;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>s;
n = s.size();
for(int i = 1; i <= n; i++) v[i] = s[i - 1] - 'a';
for(int i = 1; i <= n; i++) pos[v[i]].push_back(i);
for(int i = 0; i < 30; i++)
for(int j = 1; j <= n; j++) qtd[j][i] = qtd[j - 1][i] + (v[j] == i);
if(solve(1, n))
{
for(int i = 1; i<= n; i++) cout<<resp[i];
cout<<"\n";
}
else cout<<"-1\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |