LeetCode Weekly Contest 143 4問目 - Parsing A Boolean Expression [1106]

Source

LeetCode Weekly Contest 143
問題文

問題概要

省略

解法

省略

cLayversion 20190630-1)のコード [C++に変換後]

#include<bits/stdc++.h>
using namespace std;
class Solution{
  public:
  bool parseBoolExpr(string S){
    bool res, t1, t2;
    char op;
    int N=S.size(), i, j, k;
    string in=S;
    if(S.size()==1){
      if(S=="t"){
        return true;
      }
      return false;
    }
    if(S[0]=='!'){
      res = !(parseBoolExpr(S.substr(2, N-3)));
      return res;
    }
    op = S[0];
    S = S.substr(2, N-3);
    N = S.size();
    k = 0;
    if(op == '|'){
      res = false;
    }
    if(op == '&'){
      res = true;
    }
    j = 0;
    for(i=0;i<N+1;i++){
      if(i<N && S[i]=='(') k++;

      if(i<N && S[i]==')'){
        k--;
      }
      if(k==0 && (i==N || S[i]==',')){
        t1 = parseBoolExpr(S.substr(j,i-j));
        j = i+1;
        if(op=='|'){
          res = (res||t1);
        }
        if(op=='&'){
          res = (res&&t1);
        }
      }
    }
    return res;
  }
}
;
// cLay varsion 20190630-1

// --- original code ---
// class Solution {
// public:
//   bool parseBoolExpr(string S) {
//     int i, j, k;
//     char op;
//     int N = S.size();
//     string in = S;
//     bool res, t1, t2;
// 
//     if(S.size()==1){
//       if(S=="t") return true;
//       return false;
//     }
// 
//     if(S[0]=='!'){
//       res = !(parseBoolExpr(S.substr(2, N-3)));
//       return res;
//     }
// 
//     op = S[0];
//     S = S.substr(2, N-3);
//     N = S.size();
//     k = 0;
//     if(op == '|') res = false;
//     if(op == '&') res = true;
//     j = 0;
//     rep(i,N+1){
//       if(i<N && S[i]=='(') k++;
//       if(i<N && S[i]==')') k--;
//       if(k==0 && (i==N || S[i]==',')){
//         t1 = parseBoolExpr(S.substr(j,i-j));
//         j = i+1;
//         if(op=='|') res = (res||t1);
//         if(op=='&') res = (res&&t1);
//       }
//     }
//     return res;
//   }
// };
// {
//   // main関数を適当に処理する
// }

Current time: 2024年04月27日01時15分31秒
Last modified: 2019年07月01日01時18分35秒 (by laycrs)
Tags: Competitive_Programming_Incomplete LeetCode
トップページに戻る

Logged in as: unknown user (not login)

ログイン: