#include <bits/stdc++.h>
#define N 2050
using namespace std;
int n, v[N], dp[N][N], prox[N][N];
string s;
char resp[N];
bool solve(int l, int r)
{
if(r < l) return true;
if(r - l == 1) return (v[l] == v[r]);
if(l == r) return false;
if(dp[l][r] != -1) return dp[l][r];
prox[l][r] = -1;
for(int j = l + 1; j <= r; j++)
{
if(v[l] == v[j])
{
if(solve(l + 1, j - 1) and solve(j + 1, r))
{
prox[l][r] = j;
}
break;
}
}
if(prox[l][r] == - 1) return dp[l][r] = false;
return dp[l][r] = true;
}
void getans(int l, int r)
{
if(r < l) return;
if(l == r - 1)
{
resp[l] = '(', resp[r] = ')';
return;
}
resp[l] = '(';
resp[prox[l][r]] = ')';
getans(l + 1, prox[l][r] - 1), getans(prox[l][r] + 1, r);
}
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';
memset(dp, -1, sizeof dp);
if(solve(1, n))
{
getans(1, n);
for(int i = 1; i<= n; i++) cout<<resp[i];
cout<<"\n";
}
else cout<<"-1\n";
}
Compilation message
match.cpp: In function 'bool solve(int, int)':
match.cpp:38:40: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(prox[l][r] == - 1) return dp[l][r] = false;
~~~~~~~~~^~~~~~~
match.cpp:40:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
return dp[l][r] = true;
~~~~~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
16760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
16760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
16760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |