zoukankan      html  css  js  c++  java
  • topcoder SRM 628 DIV2 BracketExpressions

    先用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 = 0 ; 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 = 0 ; i < bracket.length();++i){
                    dst[cur]=bracket[i];
                    flag|=dfs(cur+1);
                }
                return flag;
            }else return dfs(cur+1);
        }
    
        string ifPossible(string expression)
        {
            src=dst=expression;
            return dfs(0)?"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
  • 相关阅读:
    [HNOI2013]切糕
    [POI2015]Kinoman
    「NOI2014」动物园
    [ZJOI2006]书架
    [HEOI2015]定价
    bzoj1833 数字计数
    bzoj2565 最长双回文子串
    bzoj4198 荷马史诗
    bzoj1193 马步距离
    bzoj3329 Xorequ
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3865034.html
Copyright © 2011-2022 走看看