POJ 3295 Tautology 构造 难度:1
| 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 |
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
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的更多相关文章
- 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 ...
- [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, ...
随机推荐
- 三种常用的js数组去重方法
第一种是比较常规的方法 思路: 1.构建一个新的数组存放结果 2.for循环中每次从原数组中取出一个元素,用这个元素循环与结果数组对比 3.若结果数组中没有该元素,则存到结果数组中 Array.pro ...
- HDU1043 Eight(八数码:逆向BFS打表+康托展开)题解
Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Linux CentOS 7 下 JDK 安装与配置
前言 简单记录一下在CentOS 7中安装配置JDK 1.7的全过程~ ( 安装别的版本或者jre一样) 下载 首先是jdk 1.7 64bit & 32bit的下载地址: jdk-7u79 ...
- Unity3D学习笔记(二十):Rect、Canvas、Toggle、Slider、ScrollBar
Rect Transform(锚点):图片中心的四个点,界面以雪花形式显示 当四个点在一起的时候组成锚点,当四个点分开的时候组成锚框(合则锚点,分则锚框) Anchors: ----Min x:控 ...
- Jmeter性能测试之一 性能测试的流程和步骤介绍
Step1: 知道在架构上,你要做的性能测试要cover几个部分,如下图,性能测试从用户角度,PC端之后都要要考虑进行的,例如网络,app server,Database等等 N1+N2+N3+N4 ...
- 解决MVC 时间序列化的方法
1.全局处理 处理代码 publict static void SetSerializationJsonFormat(HttpConfiguration config) { // Web API co ...
- [ios]ios tts的使用
参考:http://www.tekuba.net/program/327/ http://blog.sina.com.cn/s/blog_923fdd9b0101flx3.html iOS平台由于本身 ...
- mysql 超大数据/表管理技巧
如果你对长篇大论没有兴趣,也可以直接看看结果,或许你对结果感兴趣.在实际应用中经过存储.优化可以做到在超过9千万数据中的查询响应速度控制在1到20毫秒.看上去是个不错的成绩,不过优化这条路没有终点,当 ...
- angular5 路由传参的几种方式
此处介绍三种方式 方式一: 问号后面带的参数, 例如:/product?id=1&name=iphone还可以是: [routerLink]="['/books']" [q ...
- 解决jenkins运行selenium测试出错的问题
If you run Jenkins as a service in the background it won't open apps in the foreground. You may eith ...