POJ - 3295 - トートロジー= +列挙バイナリツリートラバーサル

http://poj.org/problem?id=3295

質問の意味:BOOL変数の大文字のブール演算の数小文字、表現を追求することは永久的な表現のために真です。

フォームの入力:

ApNp
ApNq

これは、接頭辞式です。

割り当て根が任意の出力0を持っていないようならば、それは木を構築してだから私は何かを書いて、次に列挙変数の割り当てと同じ名前を付け、本当に表現はなかったです。

バイナリツリー再帰的トラバーサルを調べます。

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;

char s[1005];
int cur;

struct TreeNode {
    char type;
    vector<int> son;
} t[1005];
int top;

//建树
int build(int u) {
    //cout<<"u="<<u<<endl;
    t[u].son.clear();
    if(islower(s[cur])) {
        t[u].type = s[cur];
        ++cur;
        return u;
    } else if(isupper(s[cur])) {
        t[u].type = s[cur];
        ++cur;
        t[u].son.push_back(build(++top));
        if(t[u].type != 'N')
            t[u].son.push_back(build(++top));
        return u;
    }
}

bool calc(int u, int val) {
    //cout<<"u="<<u<<endl;
    //cout<<"t[u].type="<<t[u].type<<endl;
    if(islower(t[u].type)) {
        //cout<<((val >> (t[u].type - 'p')) & 1)<<endl;
        return (val >> (t[u].type - 'p')) & 1;
    } else {
        bool val1 = calc(t[u].son[0], val);
        if(t[u].type == 'N')
            return !val1;
        else {
            bool val2 = calc(t[u].son[1], val);
            if(t[u].type == 'K')
                return val1 & val2;
            if(t[u].type == 'A')
                return val1 | val2;
            if(t[u].type == 'E')
                return val1 == val2;
            if(t[u].type == 'C') {
                if(val1 == 1 && val2 == 0)
                    return 0;
                else
                    return 1;
            }
            exit(-1);
        }
    }
}

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    while(~scanf("%s", s)) {
        top = cur = 0;
        if(s[0] == '0')
            break;
        build(++top);
        bool all1 = 1;
        for(int i = 0; i < (1 << 5); ++i) {
            if(calc(1, i) == 0) {
                all1 = 0;
                break;
            }
        }
        puts(all1 ? "tautology" : "not");
    }
}

おすすめ

転載: www.cnblogs.com/Inko/p/11718935.html