#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const short int inf=1000;
const short int MX=309;
char a[MX][MX];
short int n,m,dp[MX][MX][MX];
short int DP(short int x,short int y,short int open){
if(y<=0||y>m||open<0)return -inf;
if(x==0){
if(open)return -inf;
return 0;
}
if(a[x][y]=='*'){
if(open)return -inf;
return 0;
}
short int &ret=dp[x][y][open];if(ret!=-1)return ret;
pair<char,short int>v[3];
v[0]={a[x-1][y-1],y-1};
v[1]={a[x-1][y],y};
v[2]={a[x-1][y+1],y+1};
sort(v,v+3);
for(short int i=0;i<3;i++){
pair<char,short int>pp=v[i];
short int open1=open,add=0;
if(pp.first=='(')open1++;
if(pp.first==')')open1--,add=2;
short int x1=DP(x-1,pp.second,open1)+add;
if((pp.first=='('||pp.first==')'))ret=max(ret,x1);
ret=max(ret,DP(x-1,pp.second,open));
}
return ret;
}
void FDP(short int x,short int y,short int open){
if(y<=0||y>m||open<0)return;
if(x<=0)return;
if(a[x][y]=='*')return;
short int &ret=dp[x][y][open];
pair<char,short int>v[3];
v[0]={a[x-1][y-1],y-1};
v[1]={a[x-1][y],y};
v[2]={a[x-1][y+1],y+1};
sort(v,v+3);
for(short int i=0;i<3;i++){
pair<char,short int>pp=v[i];
short int open1=open,add=0;
if(pp.first=='(')open1++;
if(pp.first==')')open1--,add=2;
if(pp.first=='('||pp.first==')')if(ret==DP(x-1,pp.second,open1)+add){
printf("%c",pp.first);
FDP(x-1,pp.second,open1);
return;
}
if(ret==DP(x-1,pp.second,open)){
FDP(x-1,pp.second,open);
return;
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
memset(dp,-1,sizeof(dp));
int j;
for(int i=1;i<=m;i++){
if(a[n][i]=='M')j=i;
}
cout<<DP(n,j,0)<<endl;
FDP(n,j,0);
puts("");
}
Compilation message
retro.cpp: In function 'int main()':
retro.cpp:76:13: warning: 'j' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout<<DP(n,j,0)<<endl;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
3 ms |
59736 KB |
Partially correct |
2 |
Correct |
0 ms |
59736 KB |
Output is correct |
3 |
Partially correct |
0 ms |
59736 KB |
Partially correct |
4 |
Partially correct |
3 ms |
59736 KB |
Partially correct |
5 |
Correct |
3 ms |
59736 KB |
Output is correct |
6 |
Partially correct |
23 ms |
59736 KB |
Partially correct |
7 |
Partially correct |
103 ms |
59736 KB |
Partially correct |
8 |
Partially correct |
303 ms |
59736 KB |
Partially correct |
9 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
10 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
11 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
12 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
13 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
14 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
15 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
16 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
17 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
18 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
19 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |
20 |
Execution timed out |
500 ms |
59736 KB |
Execution timed out |