zoukankan      html  css  js  c++  java
  • POJ

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

    题意:给若干个小写字母表示bool变量,大写字母表示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");
        }
    }
    
  • 相关阅读:
    设计模式 go语言实践-5 外观模式
    .net 5 preview发布
    设计模式 Vs实践-4 桥接模式
    设计模式 Vs实践-3 装饰器模式
    PowerDesign字段和中文名切换显示
    设计模式 Vs实践-2 抽象工厂模式
    设计模式 Vs实践-1 工厂模式
    环境变量path的值大于1024的解决办法
    powshell 输出字符编码的问题,设置为utf-8
    模拟真实点击click,专门对付clickoutside
  • 原文地址:https://www.cnblogs.com/Inko/p/11718935.html
Copyright © 2011-2022 走看看