Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
递归降解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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator