Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

递归降解

Posted by uuuouou at 2013-12-31 01:10:59 on Problem 2106
class LogicCalculator
{
private:
    string::size_type findSeparator(const string& s, char sep){
        string::size_type i = 0, parentheses = 0, len = s.size();
        for(; i < len; ++i){
            if(s[i] == '(') ++parentheses;
            else if(s[i] == ')') --parentheses;
            else if(s[i] == sep && parentheses == 0) return i;
        }
        return string::npos;
    }
public:
    bool evaluate(const string& s){
        if(s.size() == 1) return s[0] == 'V';

        string::size_type sep;
        if((sep = findSeparator(s, '|')) != string::npos)
            return evaluate(s.substr(0, sep)) || evaluate(s.substr(sep+1));
        else if((sep = findSeparator(s, '&')) != string::npos)
            return evaluate(s.substr(0, sep)) && evaluate(s.substr(sep+1));
        else if(s[0] == '(')
            return evaluate(s.substr(1, s.size()-2));
        else{
            for(sep = 0; s[sep] == '!'; ++sep);
            return (sep & 0x01) ^ evaluate(s.substr(sep));
        }
    }
};

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator