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

A 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
  3.  
  4. 考虑到运算符最多是二元的,将运算符和变量存进二叉树中,结构体中用一个val值来记录是否是变量,为了提高效率,用一个visit数组来记录用到了哪几个变量。此外在最后进行运算的时候,需要二叉树进行后序遍历。此外输入采用先序遍历。
  5.  
  6. 代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <vector>
  10. #include <queue>
  11. #include <string>
  12. #define inf 0x3fffffff
  13. #define eps 1e-10
  14.  
  15. using namespace std;
  16.  
  17. struct node
  18. {
  19. char op;
  20. int val;
  21. node *left;
  22. node *right;
  23. };
  24.  
  25. bool a[5];
  26. bool visit[5];
  27.  
  28. int Do(char op, int x, int y)
  29. {
  30. switch (op)
  31. {
  32. case 'K':
  33. return x&&y;
  34. case 'A':
  35. return x||y;
  36. case 'N':
  37. return !x;
  38. case 'C':
  39. return !x || y;
  40. case 'E':
  41. return x == y;
  42. }
  43. }
  44.  
  45. bool Input(node *p)
  46. {
  47. char ch;
  48. ch = getchar();
  49. if (ch == '0')
  50. return 0;
  51. p->op = ch;
  52. p->val = -1;
  53. switch (ch)
  54. {
  55. case 'p':
  56. p->val = 0;
  57. visit[0] = 1;
  58. return 1;
  59. case 'q':
  60. p->val = 1;
  61. visit[1] = 1;
  62. return 1;
  63. case 'r':
  64. p->val = 2;
  65. visit[2] = 1;
  66. return 1;
  67. case 's':
  68. p->val = 3;
  69. visit[3] = 1;
  70. return 1;
  71. case 't':
  72. p->val = 4;
  73. visit[4] = 1;
  74. return 1;
  75. case 'N':
  76. p->left = (node *)malloc(sizeof(node));
  77. return Input(p->left);
  78. default:
  79. p->left = (node *)malloc(sizeof(node));
  80. p->right = (node *)malloc(sizeof(node));
  81. Input(p->left);
  82. return Input(p->right);
  83. }
  84. }
  85.  
  86. bool caculate(node *p)
  87. {
  88. if (p->val != -1)
  89. return a[p->val];
  90. if (p->op == 'N')
  91. return Do(p->op, caculate(p->left), 1);
  92. else
  93. return Do(p->op, caculate(p->left), caculate(p->right));
  94. }
  95.  
  96. bool dfs(int now, node *head)
  97. {
  98. if (now == 5)
  99. return caculate(head);
  100. if (visit[now] == 0)
  101. return dfs(now+1, head);
  102. int ii, jj;
  103. a[now] = 0;
  104. ii = dfs(now+1, head);
  105. a[now] = 1;
  106. jj = dfs(now+1, head);
  107. return ii && jj;
  108. }
  109.  
  110. bool qt(node *head)
  111. {
  112. if (dfs(0, head))
  113. printf("tautology\n");
  114. else
  115. printf("not\n");
  116. }
  117.  
  118. int main()
  119. {
  120. //freopen("test.txt", "r", stdin);
  121. node *head;
  122. for (;;)
  123. {
  124. memset(visit, 0, sizeof(visit));
  125. head = (node *)malloc(sizeof(node));
  126. if(!Input(head))
  127. break;
  128. getchar();
  129. qt(head);
  130. }
  131. return 0;
  132. }
  1.  

ACM学习历程——POJ3295 Tautology(搜索,二叉树)的更多相关文章

  1. ACM学习历程—HDU5423 Rikka with Tree(搜索)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  2. ACM学习历程—POJ1088 滑雪(dp && 记忆化搜索)

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  3. ACM学习历程—ZOJ3471 Most Powerful(dp && 状态压缩 && 记忆化搜索 && 位运算)

    Description Recently, researchers on Mars have discovered N powerful atoms. All of them are differen ...

  4. ACM学习历程—广东工业大学2016校赛决赛-网络赛D 二叉树的中序遍历(数据结构)

    题目链接:http://gdutcode.sinaapp.com/problem.php?cid=1031&pid=3 这算是一个胡搞类型的题目.当然肯定是有其数据结构支撑的. 唯一的限制就是 ...

  5. ACM学习历程——POJ3321 Apple Tree(搜索,线段树)

          Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will ...

  6. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  7. ACM学习历程—UESTC 1222 Sudoku(矩阵)(2015CCPC H)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1226 题目大意就是构造一个行列和每个角的2*2都是1234的4*4矩阵. 用dfs暴力搜索,不过需要每一步进 ...

  8. ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n,  ...

  9. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

随机推荐

  1. PHP如何识别系统语言或浏览器语言

    preg_match('/^([a-z\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches); $lang = $matches[1]; switc ...

  2. 1. WPF学习之概述

    <深入浅出WPF> 前言: C#专业的朋友推荐的WPF入门书籍<深入浅出WPF>,没学过的朋友从今天开始和我一起开启WPF学习之旅吧! 什么是WPF? WPF 是windows ...

  3. ckdeitor的使用方法

    CKEditor 3 JavaScript API Documentation : http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.con ...

  4. python学习(一)运行第一个python脚本

    当然这里指的是在linux或者unix下,像写bash脚本那样 #!/usr/bin/python print('The Bright Side ' + 'of Life...') 反正我建议就算一开 ...

  5. SAP Sybase SQLAnywhere[ASA]数据库中数据行的存储机制

    SQLAnywhere[ASA]数据库(以下简称ASA)中的数据库文件,是如何存储普通的表的记录行呢?插入.更新.删除时,记录行的存储会有什么变化? 了解了这些,才能更好的理解如何对ASA数据库进行调 ...

  6. uva--10714+找规律

    题意: 一根长度为len的木棍上有n仅仅蚂蚁.蚂蚁们都以1cm/s的速度爬行;假设一仅仅蚂蚁爬到了木棍的端点,那么他就会掉下去;假设两仅仅蚂蚁碰到一起了,他们就会掉头往相反方向爬行.输入len和n仅仅 ...

  7. C#多线程学习(六) 互斥对象

    如何控制好多个线程相互之间的联系,不产生冲突和重复,这需要用到互斥对象,即:System.Threading 命名空间中的 Mutex 类. 我们可以把Mutex看作一个出租车,乘客看作线程.乘客首先 ...

  8. mongo-connector导入数据到Es

    要求 基于mongo-connector同步数据,必须要求mongodb为复制集架构,原因是此插件是基于oplog操作记录进行数据同步的:而oplog可以说是Mongodb Replication的纽 ...

  9. 九度OJ 1170:找最小数 (最值)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6451 解决:2843 题目描述: 第一行输入一个数n,1 <= n <= 1000,下面输入n行数据,每一行有两个数,分别是x ...

  10. Java for LeetCode 115 Distinct Subsequences【HARD】

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...