zoukankan      html  css  js  c++  java
  • POJ-3295 Tautology---栈+表达式求值

    题目链接:

    https://vjudge.net/problem/POJ-3295

    题目大意:

    输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式WFF
          其中
         p、q、r、s、t的值为1(true)或0(false),即逻辑变量.

            K、A、N、C、E为逻辑运算符,含义如下:

          问WFF是否为【永真式】 (大前提:【输入格式保证是合法的】)

    思路:

     可以把上述打表,然后求表达式的时候用栈,从后往前来模拟。之后在网上看到各个大写字母转化成具体表达式:

             K --> and:  w && x
             A --> or:  w || x
             N --> not :  !w
             C --> implies :  (!w) || x
             E --> equals :  w == x

    总共5个小写字母,直接枚举,然后每个表达式进行判断即可

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<map>
     7 #include<queue>
     8 #include<stack>
     9 using namespace std;
    10 typedef long long ll;
    11 bool a[10];
    12 map<char, int>Map;
    13 bool vis[5][5][5];
    14 void init()
    15 {
    16     Map['K'] = 0;
    17     Map['A'] = 1;
    18     Map['C'] = 2;
    19     Map['E'] = 3;
    20     //K w x
    21     vis[0][1][1] = 1;
    22     vis[0][1][0] = 0;
    23     vis[0][0][1] = 0;
    24     vis[0][0][0] = 0;
    25     //A w x
    26     vis[1][1][1] = 1;
    27     vis[1][1][0] = 1;
    28     vis[1][0][1] = 1;
    29     vis[1][0][0] = 0;
    30     //C w x
    31     vis[2][1][1] = 1;
    32     vis[2][1][0] = 0;
    33     vis[2][0][1] = 1;
    34     vis[2][0][0] = 1;
    35     //E w x
    36     vis[3][1][1] = 1;
    37     vis[3][1][0] = 0;
    38     vis[3][0][1] = 0;
    39     vis[3][0][0] = 1;
    40 }
    41 string s;
    42 bool solve()
    43 {
    44     stack<bool>q;
    45     for(int i = s.size() - 1; i >= 0; i--)
    46     {
    47         if(islower(s[i]))q.push(a[s[i] - 'p']);
    48         else
    49         {
    50             if(s[i] == 'N')
    51             {
    52                 bool w = q.top();
    53                 q.pop();
    54                 w = !w;
    55                 q.push(w);
    56             }
    57             else
    58             {
    59                 bool w = q.top();
    60                 q.pop();
    61                 bool x = q.top();
    62                 q.pop();
    63                 q.push(vis[Map[s[i]]][w][x]);
    64             }
    65         }
    66     }
    67     return q.top();
    68 }
    69 int main()
    70 {
    71 
    72     init();
    73     while(cin >> s)
    74     {
    75         if(s.size() == 1 && s[0] == '0')break;
    76         bool ok = 1;
    77         for(int i = 0; i < (1<<5); i++)
    78         {
    79             for(int j = 0; j < 5; j++)
    80             {
    81                 if(i & (1 << j))a[j] = 1;
    82                 else a[j] = 0;
    83             }
    84             if(!solve()){ok = 0;break;}
    85         }
    86         if(ok)cout<<"tautology"<<endl;
    87         else cout<<"not"<<endl;
    88     }
    89     return 0;
    90 }
  • 相关阅读:
    android 权限及原理
    通讯协议的相关知识(备忘)
    MongoDB安装以及java开发入门<二>
    Struts2架构图
    Lucene查询对象笔记_TermQuery(笔记)
    mongodb指南(翻译)(二十) developer zone 索引(四)地理信息索引(转载)
    项目结尾公共模块WebService封装
    Redhat 5.5下安装MongoDB
    wsimport生成客户端出现的异常
    Hibernate关于空间表查询时的的一个异常
  • 原文地址:https://www.cnblogs.com/fzl194/p/8711416.html
Copyright © 2011-2022 走看看