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. css为什么叫层叠样式表

    ------------------------------------------------------------------------------------ 层叠就是浏览器对多个样式来源进 ...

  2. 【转】linux下vi命令大全

    转自:http://www.cnblogs.com/88999660/articles/1581524.html 进入vi的命令 vi filename :打开或新建文件,并将光标置于第一行首 vi ...

  3. 巧用Eclipse Java编辑器调试

    在使用Eclipse开发Java Web应用时,使用的编辑器不但能够为开发者提供代码编写.辅助提示和实时编译等常用功能,而且还能够对Java源代码进行快捷修改.重构和语法纠错等高级操作.通过Eclip ...

  4. mysql GTID主从复制(主库在线,添加新丛库)

    要求: 1.         主库上线,主库不停止服务的前提下做主从复制 2.         新添加一个丛库 操作: 1.         在主库导出数据(主库正常运行): 2.         将 ...

  5. 如何用java生成随机验证码

     1.VerifyCode 类:   1 package com.HRuinger.enity;                          ImageIO.write(image, " ...

  6. C# 调用win32API 获取进程句柄 有毛用???

    private void button2_Click(object sender, EventArgs e) { Process[] ProceddingCon = Process.GetProces ...

  7. C#斐波那契数列方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. Python 之re正则表达式

  9. CAD通过扩展记录实体向数据库读写用户自定义的全局数据(com接口VB语言)

    VB代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...

  10. [luogu4728 HNOI2009] 双递增序列 (dp)

    传送门 Solution 前几天刚做了类似题,这种将一个序列拆分为两个单调序列的题一般都是设\(dp[i]\)表示i为一个单调序列的末尾时,另一个序列的末尾是多少 然后应用贪心的思想,在这道题中就是让 ...