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;
}

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. 【Servlet与JSP】请求转发与重定向

    假设一个登录系统,要求用户输入用户名和密码: 用户在上面表单当中输入了信息之后,点击登录按钮(type="submit")将表单作为请求参数进行提交. 这一提交就有两种形式:get ...

  2. 浅谈iOS中MVVM的架构设计与团队协作【转载】

    今天写这篇文章是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇文章的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...

  3. servletResponse outputStream输出数据

    package response; import java.io.IOException;import java.io.OutputStream; import javax.servlet.Servl ...

  4. CentOS 配置网络

    1.编辑ifcfg-eth0 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改NOBOOT=yes 3.重启服务 service network re ...

  5. python 基础 7.4 os 模块

    #/usr/bin/python #coding=utf8 #@Time   :2017/11/11 3:15 #@Auther :liuzhenchuan #@File   :os 模块.py im ...

  6. python 基础 6.1 异常处理方法

      一. Excepthion 异常类    Excepthion 是所有的异常基础类(),对于python 的标准异常,我们列出如下,以做参考:   异常名称                     ...

  7. 【BZOJ3771】Triple 生成函数+FFT

    [BZOJ3771]Triple Description 我们讲一个悲伤的故事. 从前有一个贫穷的樵夫在河边砍柴. 这时候河里出现了一个水神,夺过了他的斧头,说: “这把斧头,是不是你的?” 樵夫一看 ...

  8. HttpPost (URLConnection)传参数中文乱码

    client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000000); client.getParams( ...

  9. 九度OJ 1040:Prime Number(质数) (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5278 解决:2180 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...

  10. Regularization: how complicated the model is? Regularization, measures complexity of model 使预测准确、平稳 predictive stable

    http://www.kdd.org/kdd2016/papers/files/rfp0697-chenAemb.pdf https://homes.cs.washington.edu/~tqchen ...