POJ3295 Tautology重言式
思路很简单,对于p、q、r、s、t暴力枚举是0还是1,判断即可。判断时像写表达式求值那样用栈。为了方便可以从后往前,因为最后一个肯定不是运算。那几个奇奇怪怪的函数可以找规律然后转为位运算,简洁又快。
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
const int MAXN=105;
char a[MAXN];
int n,p,q,r,s,t;
bool ans;
bool isc(char c) {
return c=='p'||c=='q'||c=='r'||c=='s'||c=='t';
}
void push(char c,stack<int>&st) {
if(c=='p')st.push(p);
if(c=='q')st.push(q);
if(c=='r')st.push(r);
if(c=='s')st.push(s);
if(c=='t')st.push(t);
}
int K(int a,int b) {
return a&b;
}
int A(int a,int b) {
return a|b;
}
int N(int a) {
return !a;
}
int C(int a,int b) {
return (!a)|b;
}
int E(int a,int b) {
return !(a^b);
}
void calc(char c,stack<int>&st) {
if(c=='K') {
int a,b;
b=st.top(),st.pop();
a=st.top(),st.pop();
st.push(K(a,b));
}
if(c=='A'){
int a,b;
b=st.top(),st.pop();
a=st.top(),st.pop();
st.push(A(a,b));
}
if(c=='N'){
int a;
a=st.top(),st.pop();
st.push(N(a));
}
if(c=='C'){
int a,b;
b=st.top(),st.pop();
a=st.top(),st.pop();
st.push(C(a,b));
}
if(c=='E'){
int a,b;
b=st.top(),st.pop();
a=st.top(),st.pop();
st.push(E(a,b));
}
}
int main() {
while(scanf("%s",a+1)!=EOF)
{
if(!strcmp(a+1,"0"))return 0;
n=strlen(a+1);
ans=true;
for(p=0; p<=1; ++p) {
for(q=0; q<=1; ++q) {
for(r=0; r<=1; ++r) {
for(s=0; s<=1; ++s) {
for(t=0; t<=1; ++t) {
stack<int>st;
for(int i=n; i>=1; --i) {
if(isc(a[i]))push(a[i],st);
else calc(a[i],st);
}
if(!st.top())
{
ans=false;
goto end;
}
}
}
}
}
}
end:{ans?puts("tautology"):puts("not");}
}
}
POJ3295 Tautology重言式的更多相关文章
- [POJ3295]Tautology
[POJ3295]Tautology 试题描述 WFF 'N PROOF is a logic game played with dice. Each die has six faces repres ...
- POJ-3295 Tautology (构造)
https://vjudge.net/problem/POJ-3295 题意 有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1? 分析 首先明确各运算符的意义,K(&a ...
- 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)或 ...
- POJ3295——Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- POJ3295 Tautology(枚举)
题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...
- poj3295 Tautology , 计算表达式的值
给你一个表达式,其包括一些0,1变量和一些逻辑运算法,让你推断其是否为永真式. 计算表达式的经常使用两种方法:1.递归: 2.利用栈. code(递归实现) #include <cstdio&g ...
- POJ3295 Tautology(栈+枚举)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- ACM学习历程——POJ3295 Tautology(搜索,二叉树)
Description WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some ...
- POJ3295 Tautology 解题报告
直接上分析: 首先 弄清各种大写字母的操作的实质 K 明显 是 and & A 是 or | N 是 not ! C 由表格注意到 当 w<=x 时 值为1 E 当 ...
随机推荐
- JavaWeb项目http请求报错:Error parsing HTTP request header
详细报错信息如下图: 原因:一一排查后,发现是http的请求中,包含了 “ | ” 竖线特殊符号:而并不是网上说的请求url太长了的原因. 解决方法:把 | 替换为 @
- 调用天气预报webservice接口
1.将 服务端的wsdl文档保存至 本地 http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?WSDL 2.将里面的 <s:element re ...
- Python 之并发编程之进程中(守护进程(daemon)、锁(Lock)、Semaphore(信号量))
五:守护进程 正常情况下,主进程默认等待子进程调用结束之后再结束守护进程在主进程所有代码执行完毕之后,自动终止kill -9 进程号 杀死进程.守护进程的语法:进程对象.daemon = True设置 ...
- 《iOS设计模式解析》书籍目录
1.你好,设计模式 2.案例分析:设计一个应用程序 3.原型 4.工厂方法 5.抽象工厂 6.生成器 7.单例 8.适配器 9.桥接 10.外观 11.中介者 12.观察者 13.组合 14.迭代器 ...
- python中sorted方法和列表的sort方法使用
一.基本形式 列表有自己的sort方法,器对列表进行原值排序,既然是原址排序,那显然元组不可能拥有这个方法,因为元组是不可修改的. 排序,数字.字符串按照ASCII,中文按照unicode从小到大排序 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:"text-success" 类的文本样式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 十七、java内存模型_JVM_JDK_类加载
1.Java内存模型 共享内存模型指的就是Java内存模型(简称JMM),JMM决定一个线程对共享变量的写入时,能对另一个线程可见.从抽象的角度来看,JMM定义了线程和主内存之间的抽象关系:线程之间的 ...
- AJAX的兼容处理方式
AJAX在网站服务中使用到频率很高,也需要考虑各个浏览器版本的兼容性,本示例中详细介绍简单快捷的处理兼容性问题. <!DOCTYPE HTML> <html> <head ...
- 使用Go语言一段时间的感受
作者 openkk 2012-03-04 18:26:58 文/Windstorm 有一段时间没更新了.最近在忙一个 Server+Client 的项目,Client 是 Android 手机,大概也 ...
- docker 日志清理
首先确认 docker 使用的存储引擎 docker info 如果使用 Logging Driver: json-file, 那么日志默认在 /var/lib/docker/contains/xxx ...