이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int n;
string x;
string ans;
int dp[2010][2010];
bool check(int l,int r){
if(dp[l][r]!=-1) return dp[l][r];
if(l>r) return 1;
bool ok=0;
for(int i=l+1;i<=r;i++){
if(x[l]==x[i]){
ok|=(check(l+1,i-1)&check(i+1,r));
}
}
return dp[l][r]=ok;
}
void match(int l,int r){
if(l>=r){
return;
}
int id=-1;
for(int i=r;i>l;i--){
if(x[i]==x[l] && check(i+1,r)){
id=i;
break;
}
}
ans[l]='('; ans[id]=')';
match(l+1,id-1);
match(id+1,r);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
memset(dp,-1,sizeof dp);
cin>>x;
n=x.size();
if(check(0,n-1)){
ans.resize(n);
match(0,n-1);
cout<<ans<<endl;
}
else cout<<"-1\n";
}
컴파일 시 표준 에러 (stderr) 메시지
match.cpp: In function 'bool check(int, int)':
match.cpp:16:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
16 | return dp[l][r]=ok;
| ~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |