POJ 3295 Tautology (构造法)
Tautology
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:
The meaning of a WFF is defined as follows:
A 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 Sample Output tautology Source Waterloo Local Contest, 2006.9.30
|
题意:K A N C E 分别代表了不同的运算符,然后用栈模拟即可,,,构造法。。。。。。。。。。。。
#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring> using namespace std; const int N=; char str[N];
int p,q,r,s,t;
stack<int> st; int isvariables(char ch){
switch(ch){
case 'p':st.push(p);return ;
case 'q':st.push(q);return ;
case 'r':st.push(r);return ;
case 's':st.push(s);return ;
case 't':st.push(t);return ;
}
return ;
} void operators(char op){
switch(op){
case 'K':{
int x=st.top(); st.pop();
int y=st.top(); st.pop();
st.push(x && y);
}break;
case 'A':{
int x=st.top(); st.pop();
int y=st.top(); st.pop();
st.push(x || y);
}break;
case 'N':{
int x=st.top(); st.pop();
st.push(!x);
}break;
case 'C':{
int x=st.top(); st.pop();
int y=st.top(); st.pop();
st.push((!x) || y);
}break;
case 'E':{
int x=st.top(); st.pop();
int y=st.top(); st.pop();
st.push(x==y);
}break;
}
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%s",str) && str[]!=''){
int len=strlen(str);
int flag=;
for(p=;p<= && flag;p++)
for(q=;q<= && flag;q++)
for(r=;r<= && flag;r++)
for(s=;s<= && flag;s++)
for(t=;t<= && flag;t++){
for(int i=len-;i>=;i--)
if(!isvariables(str[i]))
operators(str[i]);
int last=st.top();
st.pop();
if(last==)
flag=;
}
if(flag)
printf("tautology\n");
else
printf("not\n");
}
return ;
}
POJ 3295 Tautology (构造法)的更多相关文章
- POJ 3295 Tautology(构造法)
题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- 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 构造 难度: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(构造法)
http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...
- 构造 + 离散数学、重言式 - POJ 3295 Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- POJ 3295 Tautology (构造题)
字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...
- poj 3295 Tautology(栈)
题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- 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)或 ...
随机推荐
- 【BZOJ】【2946】【POI2000】公共串
后缀数组 好感动,复习了下后缀数组居然写出来了……(感谢ykz大神) 求最长公共子串……WA了一发是因为:[不同字符串之间要用不同的特殊字符隔开]否则就会匹配到相同→_→比如都是aaa结尾,如果用相同 ...
- java读取文件并获得文件编码,转换为指定编码的工具类代码
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- Spark GraphX图处理编程实例
所构建的图如下: Scala程序代码如下: import org.apache.spark._ import org.apache.spark.graphx._ // To make some of ...
- OpenCV学习(28) 轮廓
OpenCV中可以方便的在一副图像中检测到轮廓,并把这些轮廓画出来.主要用到两个函数:一个是findContours( img, contours0, hierarchy, RETR_TREE, CH ...
- russian-doll-envelopes
https://leetcode.com/problems/russian-doll-envelopes/ // Use map (Russian doll number -> vector o ...
- JQuery模仿淘宝天猫魔盒抢购页面倒计时效果
1.效果及功能说明 通过对时间的控制来告诉用户一个活动还剩多少时间,精确到秒.2.实现原理 首先定义活动的截至的时间,要重年份精确到毫秒,在获得当前的年份到秒钟,在用截至时间,减去现在的时间,剩下的还 ...
- Vue路由scrollBehavior滚动行为控制锚点
使用前端路由,当切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. vue-router 能做到,而且更好,它让你可以自定义路由切换时页面如何滚动. 注意: 这个功能只 ...
- 使用swipemenulistview实现列表的左右滑动
今天从网上找到一个第三方控件swipemenulistview,封装好的一个控件,可以实现列表的左右滑动,模仿qq的列表效果 下载地址为:https://github.com/baoyongzhang ...
- Charles Proxy for Mac & Windows (4.1.3)破解激活工具
1.简介 2017年7月10日更新本博客,Charles已经更新到了4.1.3版本,并对应地给出破解jar包. Charles是一个Mac和Windows平台都可以使用的抓包工具,它的破解激活非常简单 ...
- (算法)两个有序数组的第k大的数
题目: 有两个数组A和B,假设A和B已经有序(从大到小),求A和B数组中所有数的第K大. 思路: 1.如果k为2的次幂,且A,B 的大小都大于k,那么 考虑A的前k/2个数和B的前k/2个数, 如果A ...