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

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

解题思路:

题意为依据输入的不同的字符串。来求这个逻辑表达式的值。

这里用到了栈,和表达式求值思想类似。从后往前扫面输入的字符串,假设是数(题中的p,q,r,s,t)就进栈。假设是操作符就从栈中取数。运算后再进栈。题中的数p,q,r,s,t仅仅有0,1取值,所以5重循环,对获得的表达式求值。

遇到0就退出。

代码:

#include <iostream>
#include <string.h>
#include <stack>
using namespace std; string wff;
int p,q,r,s,t;
bool ok; int compute(string str)//栈中的操作
{
stack<int>st;
int len=str.length();
for(int i=len-1;i>=0;i--)
{
if(str[i]=='p')
st.push(p);
else if(str[i]=='q')
st.push(q);
else if(str[i]=='r')
st.push(r);
else if(str[i]=='s')
st.push(s);
else if(str[i]=='t')
st.push(t);
else if(str[i]=='K')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(x&y);
}
else if(str[i]=='A')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(x||y);
}
else if(str[i]=='N')
{
int x=st.top();
st.pop();
st.push(!x);
}
else if(str[i]=='C')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(!x||y);
}
else if(str[i]=='E')
{
int x=st.top();
st.pop();
int y=st.top();
st.pop();
st.push(x==y);
}
}
return st.top();
} int main()
{
while(cin>>wff&&wff!="0")
{
ok=1;
for(p=0;p<2;p++)//枚举结果。遇到0就退出循环
for(q=0;q<2;q++)
for(r=0;r<2;r++)
for(s=0;s<2;s++)
for(t=0;t<2;t++)
{
if(compute(wff)==0)
{
ok=0;
goto label;
}
}
label:
if(ok)
cout<<"tautology"<<endl;
else
cout<<"not"<<endl;
}
return 0;
}

[ACM] POJ 3295 Tautology (构造)的更多相关文章

  1. poj 3295 Tautology (构造)

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

  2. POJ 3295 Tautology(构造法)

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

  3. POJ 3295 Tautology 构造 难度:1

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

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

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

  5. POJ 3295 Tautology(构造法)

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

  6. POJ 3295 Tautology (构造题)

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

  7. POJ 3295 Tautology (构造法)

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

  8. poj 3295 Tautology(栈)

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

  9. poj 3295 Tautology 伪递归

    题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...

随机推荐

  1. Oracle(一)

    1.树形表,查询 所有的下边的记录 情景:根据当前记录的ID,要查询其所有子记录,每个子记录再查询当前子记录的所有子记录,如果有的话,一直迭代下去 当前表结构: CREATE TABLE " ...

  2. Js控制样式的诸多方法

    function TableCss(options){ //如果没参数,就退出 if(arguments.length < 1 || !document.getElementById(optio ...

  3. Cookie localStorage sessionStorage

    三者的异同 特性 Cookie localStorage sessionStorage 数据的生命期 可设置失效时间,默认是关闭浏览器后失效 除非被清除,否则永久保存 仅在当前会话下(tab标签页)有 ...

  4. python 根据数组生成图片

    array = np.asarray(allBigPng, dtype=np.uint8)image = Image.fromarray(array, 'RGBA') image.save(outpu ...

  5. SSH整合框架+mysql简单的实现

    SSH整合框架+mysql简单的实现 1. 框架整合原理: struts2整合Spring 两种: 一种struts2自己创建Action,自动装配Service : 一种 将Action交给Spri ...

  6. 【SQLite】select into 语句

    sqlite不支持类似sqlserver中的select into 语法 在SQL Server中,我们要将一个表中的数据复制到一个新表中,可以这样写: SELECT * INTO newtable ...

  7. 6、scala Map和Tuple

    1.  创建Map 2.访问Map元素 3.修改Map元素的值 4.遍历Map 5.SortedMap和LinkedHashMap 6.Map的元素类型Tuple 1.  创建Map 创建不可变的Ma ...

  8. 【sqli-labs】 less56 GET -Challenge -Union -14 queries allowed -Variation3 (GET型 挑战 联合查询 只允许14次查询 变化3)

    单引号括号闭合 http://192.168.136.128/sqli-labs-master/Less-56/?id=1')%23 http://192.168.136.128/sqli-labs- ...

  9. Nrpe 插件安装教程

    Nrpe 插件安装教程  blog地址: http://www.cnblogs.com/caoguo 一.nagios plugins的安装 [root@Nrpe ~]# yum install -y ...

  10. day02-操作系统、编程语言分类及python安装

    目录 操作系统 编程语言分类 安装python解释器 操作系统 操作系统有什么用 操作系统能接受外部指令转化成0和1,并把一些对硬件的复杂操作简化成一个个简单的接口,作为中间人连接硬件和软件 计算机三 ...