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

ApNp
ApNq
0

Sample Output

tautology
not 考虑到运算符最多是二元的,将运算符和变量存进二叉树中,结构体中用一个val值来记录是否是变量,为了提高效率,用一个visit数组来记录用到了哪几个变量。此外在最后进行运算的时候,需要二叉树进行后序遍历。此外输入采用先序遍历。 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define eps 1e-10 using namespace std; struct node
{
char op;
int val;
node *left;
node *right;
}; bool a[5];
bool visit[5]; int Do(char op, int x, int y)
{
switch (op)
{
case 'K':
return x&&y;
case 'A':
return x||y;
case 'N':
return !x;
case 'C':
return !x || y;
case 'E':
return x == y;
}
} bool Input(node *p)
{
char ch;
ch = getchar();
if (ch == '0')
return 0;
p->op = ch;
p->val = -1;
switch (ch)
{
case 'p':
p->val = 0;
visit[0] = 1;
return 1;
case 'q':
p->val = 1;
visit[1] = 1;
return 1;
case 'r':
p->val = 2;
visit[2] = 1;
return 1;
case 's':
p->val = 3;
visit[3] = 1;
return 1;
case 't':
p->val = 4;
visit[4] = 1;
return 1;
case 'N':
p->left = (node *)malloc(sizeof(node));
return Input(p->left);
default:
p->left = (node *)malloc(sizeof(node));
p->right = (node *)malloc(sizeof(node));
Input(p->left);
return Input(p->right);
}
} bool caculate(node *p)
{
if (p->val != -1)
return a[p->val];
if (p->op == 'N')
return Do(p->op, caculate(p->left), 1);
else
return Do(p->op, caculate(p->left), caculate(p->right));
} bool dfs(int now, node *head)
{
if (now == 5)
return caculate(head);
if (visit[now] == 0)
return dfs(now+1, head);
int ii, jj;
a[now] = 0;
ii = dfs(now+1, head);
a[now] = 1;
jj = dfs(now+1, head);
return ii && jj;
} bool qt(node *head)
{
if (dfs(0, head))
printf("tautology\n");
else
printf("not\n");
} int main()
{
//freopen("test.txt", "r", stdin);
node *head;
for (;;)
{
memset(visit, 0, sizeof(visit));
head = (node *)malloc(sizeof(node));
if(!Input(head))
break;
getchar();
qt(head);
}
return 0;
}

最新文章

  1. mongo链接solr的过程与问题
  2. (翻译)正确实施DevOps-The Lay of the Land
  3. BZOJ 2727 双十字(树状数组)
  4. HTML表单元素中disabled的元素的值不会提交到服务器
  5. 第一个hibernate文件 xml配置方法
  6. Bigcommerce:安装的出错解决方案
  7. Linus Torvalds来自开发商的消息:成就,不定
  8. 【web开发学习笔记】Structs2 Action学习笔记(两)
  9. 【实验室笔记】C#上位机学习笔记
  10. 部署Cloudera Management for centos 7
  11. Spring+SpringMVC+MyBatis+easyUI整合优化篇(五)结合MockMvc进行服务端的单元测试
  12. 使用进程池规避Python的GIL限制
  13. 利用sklearn对MNIST手写数据集开始一个简单的二分类判别器项目(在这个过程中学习关于模型性能的评价指标,如accuracy,precision,recall,混淆矩阵)
  14. 使用java程序对oracle添加触发器时,报错:索引中丢失 IN 或 OUT 参数:: 1
  15. ajax请求导致status为canceled(无任何回调数据)的原因
  16. TZOJ 二分图练习
  17. 集群服务器下使用SpringBoot @Scheduled注解定时任务
  18. [转载]8 种提升 ASP.NET Web API 性能的方法
  19. maven(13)-安装nexus私服
  20. mac远程桌面Microsoft Remote Desktop for Mac的安装与使用

热门文章

  1. MySql(四):备份与恢复
  2. scrapy递归抓取网页数据
  3. C#中判断某个值是否存在于枚举
  4. ssh port forwarding
  5. c++ 通过数据流方式实现文件拷贝
  6. java 多参实现
  7. Trie树,又称单词查找树、字典
  8. EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:内部搜索功能的实现
  9. C#中反射type记录
  10. Azkaban_Oozie_action