[POJ3295]Tautology

试题描述

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 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.

输出

For each test case, output a line containing tautology or not as appropriate.

输入示例

ApNp
ApNq

输出示例

tautology
not

数据规模及约定

见“输入

题解

枚举 p, q, r, s, t 的值,然后带进去递归求出这个串的值,如果都为真那么就是“tautology”,否则是“not”。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; #define maxn 110
#define maxal 300
char S[maxn];
int ord[maxal];
bool val[10]; struct Info {
int p, v;
Info() {}
Info(int _, int __): p(_), v(__) {}
} ;
Info check(int l) {
if(islower(S[l])) return Info(l + 1, val[ord[S[l]]]);
if(S[l] == 'N') {
Info t;
t = check(l + 1);
return Info(t.p, t.v ^ 1);
}
if(S[l] == 'K') {
Info t, t2;
t = check(l + 1);
t2 = check(t.p);
return Info(t2.p, t.v & t2.v);
}
if(S[l] == 'A') {
Info t, t2;
t = check(l + 1);
t2 = check(t.p);
return Info(t2.p, t.v | t2.v);
}
if(S[l] == 'C') {
Info t, t2;
t = check(l + 1);
t2 = check(t.p);
return Info(t2.p, (t.v && !t2.v) ? 0 : 1);
}
if(S[l] == 'E') {
Info t, t2;
t = check(l + 1);
t2 = check(t.p);
return Info(t2.p, t.v == t2.v);
}
return Info(0, 0);
} int main() {
ord['p'] = 0;
ord['q'] = 1;
ord['r'] = 2;
ord['s'] = 3;
ord['t'] = 4;
while(scanf("%s", S + 1) == 1) {
int all = (1 << 5) - 1, n = strlen(S + 1);
if(n == 1 && S[1] == '0') break;
bool ok = 1;
for(int i = 0; i <= all; i++) {
for(int j = 0; j < 5; j++)
val[j] = (i >> j & 1);
if(!check(1).v){ ok = 0; break; }
}
puts(ok ? "tautology" : "not");
} return 0;
}

[POJ3295]Tautology的更多相关文章

  1. POJ-3295 Tautology (构造)

    https://vjudge.net/problem/POJ-3295 题意 有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1? 分析 首先明确各运算符的意义,K(&a ...

  2. poj3295 Tautology —— 构造法

    题目链接:http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为1(true)或 ...

  3. POJ3295——Tautology

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

  4. POJ3295 Tautology(枚举)

    题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...

  5. poj3295 Tautology , 计算表达式的值

    给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...

  6. POJ3295 Tautology(栈+枚举)

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

  7. ACM学习历程——POJ3295 Tautology(搜索,二叉树)

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

  8. POJ3295 Tautology 解题报告

    直接上分析: 首先 弄清各种大写字母的操作的实质 K 明显 是 and   & A 是 or      | N 是 not   ! C  由表格注意到 当 w<=x 时 值为1 E  当 ...

  9. POJ3295 Tautology重言式

    Tautology 思路很简单,对于p.q.r.s.t暴力枚举是0还是1,判断即可.判断时像写表达式求值那样用栈.为了方便可以从后往前,因为最后一个肯定不是运算.那几个奇奇怪怪的函数可以找规律然后转为 ...

随机推荐

  1. StringBuilder 拼接sql语句比较快

    StringBuilder 拼接sql语句比较快StringBuilder strBuilder = new StringBuilder();strSql += "insert into t ...

  2. phpcms从表v9_news_data中字段content中用正则取出图片的地址输出

    preg_match ("<img.*src=[\"](.*?)[\"].*?>",$test,$match); echo "$match ...

  3. Spring MVC学习笔记——给Controller和视图传值

    一.给Controller传值,值将显示在控制台 1.第一种:使用@RequestParam,改HelloController.java //RequestMapping表示用哪一个url来对应 @R ...

  4. 使用node的插件UglifyJs来合并和压缩文件

    code: var fs = require('fs'); var jsp = require("./UglifyJS-master/uglify-js").parser; var ...

  5. IIS7/IIS7.5中目录执行权限的设置方法

    我们在建站的时候,通常有些目录必须给写入权限,这个时候这些目录就很可能被人写入脚本文件,为了将安全性维护得更好,我们可以关闭这些有写入权限的目录的脚本执行权限.IIS6的时候,我们很容易找到关闭的地方 ...

  6. JSP-Servlet的工作流程

    Servlet基础 1.Servlet概述 JSP的前身就是Servlet.Servlet就是在服务器端运行的一段小程序.一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问 ...

  7. IIS短文件名扫描工具

    #!/usr/bin/env python # -*- coding: utf-8 -*- import sys import httplib import urlparse import strin ...

  8. C-基本语法与运算

    编译: Technorati 标记: C 1, 编译compilers 命令make 将高级语言转换为低级语言. clang: 1,预处理(preprocessing) 2,编译(complition ...

  9. Yii2.0 实现的短信发送

    原文地址:http://www.phpxs.com/post/4245/ yii2-smserGithub项目主页 https://github.com/daixianceng/yii2-smser ...

  10. .NET4.0 __doPostBack未定义

    方法一.浏览器设置成兼容模式. 方法二.安装服务器版的.Net40的补丁.http://download.csdn.net/detail/5653325/6642051 方法三.点击VS的工具菜单-- ...