Submission #51379

# Submission time Handle Problem Language Result Execution time Memory
51379 2018-06-17T18:42:48 Z spencercompton Match (CEOI16_match) C++17
10 / 100
3 ms 492 KB
#include <bits/stdc++.h>
using namespace std;
string str;
int dp[18][18];
bool go(int s, int e){
	if(s>e){
		return true;
	}
	if(dp[s][e]!=-1){
		return dp[s][e]==1;
	}
	bool ret = false;
	for(int i = s+1; i<=e; i++){
		ret |= (str[s]==str[i]) && go(s+1,i-1)&&go(i+1,e);
	}
	if(ret){
		dp[s][e] = 1;
	}
	else{
		dp[s][e] = 0;
	}
	return ret;
}
string build(int s, int e){
	if(s>e){
		return "";
	}
	for(int i = e; i>=s+1; i--){
		if(str[s]==str[i] && go(s+1,i-1) && go(i+1,e)){
			string ret = "(" + build(s+1,i-1) + ")" + build(i+1,e);
			return ret;
		}
	}
	return "ERROR";
}
int main(){
	cin >> str;
	for(int a = 0; a<18; a++){
		for(int b = 0; b<18; b++){
			dp[a][b] = -1;
		}
	}
	if(go(0,str.length()-1)){
		cout << build(0,str.length()-1) << endl;
	}
	else{
		cout << -1 << endl;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 480 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 480 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Incorrect 2 ms 492 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 480 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Incorrect 2 ms 492 KB Output isn't correct
5 Halted 0 ms 0 KB -