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

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

这道题的意思:用字符串的形式给你一个逻辑表达式,判断是否为永真式,逻辑变量只有那五个小写字母

这是我的代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const char ch[]={'p','q','r','s','t'};
char a[];
int rep[];
int f[];
int len;
void printrep(){
for(int i=;i<;i++){
printf("%c%d ",ch[i],rep[ch[i]]);
}
puts("");
}
bool isnum(char ch1){
for(int i=;i<;i++)if(ch1==ch[i])return true;
return false;
}
bool dfs(int ind){
if(ind<){if(!dfs(ind+))return false;rep[ch[ind]]=;if(!dfs(ind+))return false;rep[ch[ind]]=;return true;}
memset(f,,sizeof(f));
int index=;
for(int i=len-;i>=;i--){
if(isnum(a[i])){
f[index++]=rep[a[i]];
}
else {
if(a[i]=='K'){
f[index-]=f[index-]&f[index-];
index--;
}
if(a[i]=='A'){
f[index-]=f[index-]|f[index-];
index--;
}
if(a[i]=='N'){
f[index-]=^f[index-];
}
if(a[i]=='C'){
f[index-]=((^f[index-])|f[index-]);
index--;
}
if(a[i]=='E'){
f[index-]=^(f[index-]^f[index-]);
index--;
}
}
}
if(f[index-]==)return false;
return true;
}
int main()
{
while(scanf("%s",a)==&&strcmp(a,"")){
memset(rep,,sizeof(rep));
len=strlen(a);
if(dfs()){
puts("tautology");
}
else {
puts("not");
}
}
return ;
}
int ind()
{
char ch=s[l++];//把整个堆栈过程和变量分开看了
printf(""); switch(ch)
{
case 'p':
case 'q':
case 'r':
case 's':
case 't':
return state[ch];
break;
case 'K':
return ind()&ind();
break;
case 'A':
return ind()|ind();
break;
case 'N':
return !ind();
break;
case 'C':
return !ind()|ind();
break;
case 'E':
return ind()==ind();
break;
}
}

还可以字符替换,不贴了,可以增强直观,反正数据不大

POJ 3295 Tautology 构造 难度:1的更多相关文章

  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. [ACM] POJ 3295 Tautology (构造)

    Tautology Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9302   Accepted: 3549 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. Postgresql数据库实用命令

    Postgresql 命令 pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start 启动数据库 cr ...

  2. UVa 1471 Defense Lines - 线段树 - 离散化

    题意是说给一个序列,删掉其中一段连续的子序列(貌似可以为空),使得新的序列中最长的连续递增子序列最长. 网上似乎最多的做法是二分查找优化,然而不会,只会值域线段树和离散化... 先预处理出所有的点所能 ...

  3. RS(纠删码)技术浅析及Python实现

    前言 在Ceph和RAID存储领域,RS纠删码扮演着重要的角色,纠删码是经典的时间换空间的案例,通过更多的CPU计算,降低低频存储数据的存储空间占用. 纠删码原理 纠删码基于范德蒙德矩阵实现,核心公式 ...

  4. hosts 位置和功能

    什么是HOST文件: Hosts是一个没有扩展名的系统文件,其基本作用就是将一些常用的网址域名与其对应的IP地址建立一个关联“数据库”,当用户在浏览器中输入一个需要登录的网址时,系统会首先自动从Hos ...

  5. 风景区的面积及道路状况分析问题 test

    参考文献:   https://wenku.baidu.com/view/b6aed86baf1ffc4ffe47ac92.html #include <bits/stdc++.h> us ...

  6. 深入理解Java面向对象三大特性 封装 继承 多态

    1.封装 封装的定义: 首先是抽象,把事物抽象成一个类,其次才是封装,将事物拥有的属性和动作隐藏起来,只保留特定的方法与外界联系 为什么需要封装: 封装符合面向对象设计原则的第一条:单一性原则,一个类 ...

  7. java进制转换代码

    定义十进制的数直接写,定义8进制的数以0开头,定义二进制的数以0b开头,定义十六进制的数以0x开头需要将十进制的数以二进制的数表示出来可以参照下例: int a = 10; System.out.pr ...

  8. UVa 12563 劲歌金曲(0-1背包)

    https://vjudge.net/problem/UVA-12563 题意: 在一定的时间内连续唱歌,最后一首唱11分钟18秒的劲歌金曲,问最多能长多长时间. 思路: 0-1背包问题,背包容量为t ...

  9. Jmeter Connect to Cassandra

    https://github.com/Netflix/CassJMeter/wiki https://stackoverflow.com/questions/40974407/connecting-c ...

  10. STL_算法_05_集合算法

    ◆ 常用的集合算法: 1. 1.1.第6讲 PPT.40 ◆ set_union() :  构造一个有序序列,包含两个有序序列的并集. 1.2.第6讲 PPT.40 ◆ set_intersectio ...