Tautology
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7716   Accepted: 2935

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

  1. ApNp
  2. ApNq
  3. 0

Sample Output

  1. tautology
  2. not

Source

题意:K  A  N  C  E 分别代表了不同的运算符,然后用栈模拟即可,,,构造法。。。。。。。。。。。。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<stack>
  4. #include<cstring>
  5.  
  6. using namespace std;
  7.  
  8. const int N=;
  9.  
  10. char str[N];
  11. int p,q,r,s,t;
  12. stack<int> st;
  13.  
  14. int isvariables(char ch){
  15. switch(ch){
  16. case 'p':st.push(p);return ;
  17. case 'q':st.push(q);return ;
  18. case 'r':st.push(r);return ;
  19. case 's':st.push(s);return ;
  20. case 't':st.push(t);return ;
  21. }
  22. return ;
  23. }
  24.  
  25. void operators(char op){
  26. switch(op){
  27. case 'K':{
  28. int x=st.top(); st.pop();
  29. int y=st.top(); st.pop();
  30. st.push(x && y);
  31. }break;
  32. case 'A':{
  33. int x=st.top(); st.pop();
  34. int y=st.top(); st.pop();
  35. st.push(x || y);
  36. }break;
  37. case 'N':{
  38. int x=st.top(); st.pop();
  39. st.push(!x);
  40. }break;
  41. case 'C':{
  42. int x=st.top(); st.pop();
  43. int y=st.top(); st.pop();
  44. st.push((!x) || y);
  45. }break;
  46. case 'E':{
  47. int x=st.top(); st.pop();
  48. int y=st.top(); st.pop();
  49. st.push(x==y);
  50. }break;
  51. }
  52. }
  53.  
  54. int main(){
  55.  
  56. //freopen("input.txt","r",stdin);
  57.  
  58. while(~scanf("%s",str) && str[]!=''){
  59. int len=strlen(str);
  60. int flag=;
  61. for(p=;p<= && flag;p++)
  62. for(q=;q<= && flag;q++)
  63. for(r=;r<= && flag;r++)
  64. for(s=;s<= && flag;s++)
  65. for(t=;t<= && flag;t++){
  66. for(int i=len-;i>=;i--)
  67. if(!isvariables(str[i]))
  68. operators(str[i]);
  69. int last=st.top();
  70. st.pop();
  71. if(last==)
  72. flag=;
  73. }
  74. if(flag)
  75. printf("tautology\n");
  76. else
  77. printf("not\n");
  78. }
  79. return ;
  80. }

POJ 3295 Tautology (构造法)的更多相关文章

  1. POJ 3295 Tautology(构造法)

    题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Su ...

  2. poj 3295 Tautology (构造)

    题目:http://poj.org/problem?id=3295 题意:p,q,r,s,t,是五个二进制数. K,A,N,C,E,是五个运算符. K:&& A:||N:! C:(!w ...

  3. POJ 3295 Tautology 构造 难度:1

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9580   Accepted: 3640 Descrip ...

  4. [ACM] POJ 3295 Tautology (构造)

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9302   Accepted: 3549 Descrip ...

  5. POJ 3295 Tautology(构造法)

    http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...

  6. 构造 + 离散数学、重言式 - POJ 3295 Tautology

    Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...

  7. POJ 3295 Tautology (构造题)

    字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...

  8. poj 3295 Tautology(栈)

    题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...

  9. poj3295 Tautology —— 构造法

    题目链接:http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为1(true)或 ...

随机推荐

  1. 计算Fisher vector和VLAD

    This short tutorial shows how to compute Fisher vector and VLAD encodings with VLFeat MATLAB interfa ...

  2. 第二章 企业项目开发--maven父子模块

    2.1.maven父子模块 在实际开发中,我们基本都会用maven父子分模块的方式进行项目的开发. 2.2.实际操作 2.2.1.手工建立一个ssmm0的文件夹,并在该文件夹中加入一个pom.xml文 ...

  3. C/C++ 获取目录下的文件列表信息

    在C/C++编程时,需要获取目录下面的文件列表信息. 1.数据结构 struct dirent {     long d_ino;                 /* inode number 索引 ...

  4. 关于DLL文件和EXE文件不在同一目录下的设置【转】

    https://www.cnblogs.com/chaosimple/archive/2012/08/13/2636181.html 关于DLL文件和EXE文件不在同一目录下的设置 在开发程序结束后, ...

  5. HTML标签 闭合还是不闭合?

    你在写 HTML5 代码的时候,是否纠结过应该写 <br /> 还是 <br>,是写 <input /> 还是写 <input>.写 <scrip ...

  6. SQL锁(转)

    说 明    Chaos 无法改写隔离级别更高的事务中的挂起的更改.   ReadCommitted 在正在读取数据时保持共享锁,以避免脏读,但是在事务结束之前可以更改数据,从而导致不可重复的读取或幻 ...

  7. JAVA 读取计算机中相关信息

    java读取 计算机 cup号 读取版本号 显卡 .. . . ........ .. . . . package com.swt.common.util; import java.io.Buffer ...

  8. lua接收图片并进行md5处理

    需要luacurl(http://luacurl.luaforge.net/)和MD5两个库函数 curl = require("luacurl") require("m ...

  9. (回溯法)和为n的所有不增正整数和式分解算法

    题目: 利用递归算法输出正整数和为n的所有不增的正整数和式.例如当n=5时,不增的和式如下: 5=5 5=4+1 5=3+2 5=3+1+1 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 ...

  10. 劣质代码评析——《写给大家看的C语言书(第2版)》附录B之21点程序(八)

    [重构](续) 牌的表示: 一副牌有52张,可用一整数数组描述.但是由于在游戏过程中牌数在不断减少,所以用一表示剩余张数的整数和一整数数组共同描述.C99支持一种变量长度数组,但用在这里并没有什么特别 ...