zoukankan      html  css  js  c++  java
  • Poj 3295 Tautology

    1.Link:

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

    2.content:

    Tautology
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9793   Accepted: 3712

    Description

    WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (WFF) is any string of these symbols obeying the following rules:

    • p, q, r, s, and t are WFFs
    • if w is a WFF, Nw is a WFF
    • if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.
    The meaning of a WFF is defined as follows:
    • p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).
    • K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.
    Definitions of K, A, N, C, and E
         w  x   Kwx   Awx    Nw   Cwx   Ewx
      1  1   1   1    0   1   1
      1  0   0   1    0   0   0
      0  1   0   1    1   1   0
      0  0   0   0    1   1   1

    tautology is a WFF that has value 1 (true) regardless of the values of its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the value 0 for p=0, q=1.

    You must determine whether or not a WFF is a tautology.

    Input

    Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.

    Output

    For each test case, output a line containing tautology or not as appropriate.

    Sample Input

    ApNp
    ApNq
    0

    Sample Output

    tautology
    not

    Source

    3.method:

    将p,q,r,s,t分别置入以0,1代入,使用五个for循环实现,这样会出现相同计算多次的情况,不过由于循环次数很少,所以不影响大的效率。

    将字符串从后向前置入栈,遇到运算符则进行运算,然后将结果置入栈。

    最后结果非1的话利用goto跳出多层循环

    4.code:

     1 #include <iostream>
     2 #include <string>
     3 #include <stack>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     //freopen("D://input.txt","r",stdin);
    10 
    11     string str;
    12     cin >> str;
    13 
    14     while(str.size() != 1 || str[0] != '0')
    15     {
    16         //cout << str << endl;
    17 
    18         int p,q,r,s,t;
    19         for(p = 0; p < 2; ++p)
    20         {
    21             for(q = 0; q < 2; ++q)
    22             {
    23                 for(r = 0; r < 2; ++r)
    24                 {
    25                     for(s = 0; s < 2; ++s)
    26                     {
    27                         for(t = 0; t < 2; ++t)
    28                         {
    29                             stack<int> s_str;
    30                             int w,x;
    31                             for(string::reverse_iterator r_iter = str.rbegin(); r_iter != str.rend(); ++r_iter)
    32                             //for(int str_i = str.size() - 1; str_i >= 0; --str_i)
    33                             {
    34                                 if(*r_iter == 'p') s_str.push(p);
    35                                 else if(*r_iter == 'q') s_str.push(q);
    36                                 else if(*r_iter == 'r') s_str.push(r);
    37                                 else if(*r_iter == 's') s_str.push(s);
    38                                 else if(*r_iter == 't') s_str.push(t);
    39                                 else
    40                                 {
    41                                     w = s_str.top();
    42                                     s_str.pop();
    43                                     if(*r_iter == 'N') s_str.push(!w);
    44                                     else
    45                                     {
    46                                         x = s_str.top();
    47                                         s_str.pop();
    48                                         if(*r_iter == 'K') s_str.push(w&&x);
    49                                         else if(*r_iter == 'A') s_str.push(w||x);
    50                                         else if(*r_iter == 'C') {if(w == 1 && x == 0) s_str.push(0); else s_str.push(1);}
    51                                         else {if(w == x) s_str.push(1); else s_str.push(0);}
    52                                     }
    53                                 }
    54                             }
    55                             if(s_str.top() != 1) goto end;
    56                         }
    57                     }
    58                 }
    59             }
    60         }
    61 
    62 end:
    63         if(p >= 2) cout << "tautology" << endl;
    64         else cout << "not" << endl;
    65 
    66         cin >> str;
    67     }
    68     return 0;
    69 }

     5.Reference:

    http://blog.csdn.net/lyy289065406/article/details/6642766

  • 相关阅读:
    数据库数据闪回设置
    Ajax
    Spring Framework & Spring Boot
    Maven与Ant比较
    Spring中控制反转(IoC)/依赖注入(DI)的区别
    C# 图片加水印、截取图片、压缩图片等。有个坑,往下看。当图片为jpg格式时,发现出来的图片结果异常的大
    json 递归
    Webservice 问题:system.argumentexception:找不到xxx定义。缺少命名空间为xxx的服务说明
    利用.net 自带的工具生成webservice服务(windows)
    sqlserver 查询 今天明天后天等数据
  • 原文地址:https://www.cnblogs.com/mobileliker/p/4054503.html
Copyright © 2011-2022 走看看