POJ-3295 Tautology (构造)
https://vjudge.net/problem/POJ-3295
题意
有五种运算符和五个参数,现在给你一个不超过100字符的算式,问最后结果是否恒为1?
分析
首先明确各运算符的意义,K(&&),A(||),N(!),E(==),C特殊判断一下。计算的话肯定是从后往前的,遇到变量进栈,遇到运算符则出栈做运算。现在的问题是各个变量的数值未知,由于只有5个变量,总共只有32总可能,所以暴力跑一遍即可,每种取值都试一次。
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set> #define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
template <class T>
inline bool scan_d(T &ret){
char c;int sgn;
if(c=getchar(),c==EOF) return ;
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret = ret*+(c-'');
ret*=sgn;
return ;
}
const int N = 1e6+;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T; void testcase(){
printf("Case %d:",++T);
} const int MAXN = 5e5+ ;
const int MAXM = ;
const double eps = 1e-;
const double PI = acos(-1.0); int p,q,r,s,t;
int sta[];
char ss[]; void run(){
int top=;
int len = strlen(ss);
int a,b;
for(int i=len-;i>=;i--){
if(ss[i]=='p') sta[top++]=p;
else if(ss[i]=='q') sta[top++]=q;
else if(ss[i]=='r') sta[top++]=r;
else if(ss[i]=='s') sta[top++]=s;
else if(ss[i]=='t') sta[top++]=t;
else if(ss[i]=='K'){
a=sta[--top];
b=sta[--top];
sta[top++]=(a&&b);
}else if(ss[i]=='A'){
a=sta[--top];
b=sta[--top];
sta[top++]=(a||b);
}else if(ss[i]=='N'){
a=sta[--top];
sta[top++]=(!a);
}else if(ss[i]=='C'){
a=sta[--top];
b=sta[--top];
if(a==&&b==) sta[top++]=;
else sta[top++]=;
}else if(ss[i]=='E'){
a=sta[--top];
b=sta[--top];
sta[top++]=(a==b);
}
}
} bool work(){
for(p=;p<;p++)
for(q=;q<;q++)
for(r=;r<;r++)
for(s=;s<;s++)
for(t=;t<;t++){
run();
if(sta[]==) return false;
}
return true;
}
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
// init();
while(~scanf("%s",ss)){
if(ss[]=='') break;
if(work()) puts("tautology");
else puts("not");
}
return ;
}
POJ-3295 Tautology (构造)的更多相关文章
- poj 3295 Tautology (构造)
题目:http://poj.org/problem?id=3295 题意:p,q,r,s,t,是五个二进制数. K,A,N,C,E,是五个运算符. K:&& A:||N:! C:(!w ...
- POJ 3295 Tautology(构造法)
题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- POJ 3295 Tautology 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
- [ACM] POJ 3295 Tautology (构造)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3549 Descrip ...
- 构造 + 离散数学、重言式 - POJ 3295 Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- POJ 3295 Tautology(构造法)
http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...
- POJ 3295 Tautology (构造题)
字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...
- POJ 3295 Tautology (构造法)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7716 Accepted: 2935 Descrip ...
- poj 3295 Tautology(栈)
题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- poj 3295 Tautology 伪递归
题目链接: http://poj.org/problem?id=3295 题目描述: 给一个字符串,字符串所表示的表达式中p, q, r, s, t表示变量,取值可以为1或0.K, A, N, C, ...
随机推荐
- 微信小程序实现各种特效实例
写在前面 最近在负责一个微信小程序的前端以及前后端接口的对接的项目,整体上所有页面的布局我都已经搭建完成,里面有一些常用的特效,总结一下,希望对大家和我都能有所帮助 实例1:滚动tab选项卡 先看一下 ...
- stl源码剖析 详细学习笔记deque(3)
protected: typedef simple_alloc<value_type,Alloc> data_allocator; //用来配置元素的alloc typedef simpl ...
- 用Visual Studio2017写C++静态库
造轮子是一件有趣的事情,VS是一个强大的工具,能胜任超大规模的工程,但是讲真,对不那么大的项目配置起来不是那么友好(网上的其他教程也一点都不友好Orz).这里就展示一下构建一个简单的静态库的正确姿势. ...
- 软件工程第二次作业(JUnit的使用)
初次使用JUnit 感谢学习资源Junit使用的超简单介绍源 一.开发环境及界面截图: 系统 Windows 10 编辑器 eclipse 语言 ...
- Frida----安装
介绍 它是本机应用程序的 Greasemonkey,或者更多技术术语,它是一个动态代码检测工具包.它允许您将JavaScript或您自己的库的片段注入Windows,macOS,GNU / Linux ...
- C++学习 内存模型和名称空间
1.单独编译 C++鼓励程序员将组件函数放在独立的文件中,如果只修改了一个文件,则可以只重新编译该文件,然后将它与其他文件的编译版本链接. 一般非常有用的组织程序的策略是把程序分成三部分: 头文件:包 ...
- PAT甲题题解-1077. Kuchiguse (20)-找相同后缀
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 11.7 Daily Scrum(周末暂停两天Daily Scrum)
由于APEC放假,有些成员离校了,他们那部分的任务会暂时拖后一些,之后会加班加点赶工. 另外,每个人的任务还是相对独立,离校成员的任务进度不会对其他成员的进度造成很大影响. Today's tas ...
- 《Linux内核分析》--扒开系统调用的三层皮 20135311傅冬菁
扒开系统调用的三层皮 20135311傅冬菁 一.内容分析 寄存器上下文(从用户态切换到内核态) 中断/int指令会在堆栈上保存一些寄存器的值(用户态栈顶地址..当时的状态字.当下 ...
- 《Linux内核分析与设计实现》读书笔记一
第一章 Linux内核简介 1.1 Unix的历史 Unix的特点: Unix很简洁,仅仅提供几百个系统调用并且有一个非常明确的设计目的: 在Unix中,所有的东西都被当做文件对待. Unix的内核和 ...