topcoder SRM 628 DIV2 BracketExpressions

时间:2023-01-11 08:26:10

先用dfs搜索所有的情况,然后判断每种情况是不是括号匹配

#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <climits>
#include <queue> using namespace std; class BracketExpressions
{
public:
string src,dst;
const string bracket="()[]{}"; bool check(string& str){
stack<char> st;
for(int i = ; i < str.length(); ++ i){
if(st.empty()) st.push(str[i]);
else{
char ch = st.top();
if((str[i]==']'&& ch=='[')||(str[i] == '}' && ch=='{') || (str[i]==')'&& ch=='(')) st.top();
else st.push(str[i]);
}
}
cout<<st.size()<<endl;
return st.empty()?true:false;
} bool dfs(int cur){
if(cur==src.length()) return check(dst);
if(src[cur]=='X'){
bool flag = false;
for(int i = ; i < bracket.length();++i){
dst[cur]=bracket[i];
flag|=dfs(cur+);
}
return flag;
}else return dfs(cur+);
} string ifPossible(string expression)
{
src=dst=expression;
return dfs()?"possible":"impossible";
} }; // Powered by FileEdit
// Powered by TZTester 1.01 [25-Feb-2003]
// Powered by CodeProcessor // Powered by FileEdit
// Powered by TZTester 1.01 [25-Feb-2003]
// Powered by CodeProcessor