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, ...
随机推荐
- 洛谷 P2056 采花 - 莫队算法
萧芸斓是 Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了 n 朵花,花有 c 种颜色(用整数 1-c 表示) ,且花是排成一排的,以 ...
- UVa 12627 Erratic Expansion - 分治
因为不好复制题目,就出给出链接吧: Vjudge传送门[here] UVa传送门[here] 请仔细看原题上的那幅图,你会发现,在时间t(t > 0),当前的气球构成的一幅图,它是由三个时间为( ...
- 多态时最好将基类的析构函数设为virtual、 C++中两个类相互包含引用问题 (转载)
多态:http://blog.csdn.net/tmljs1988/article/details/8146521 C++中两个类相互包含引用问题:http://blog.csdn.net/leo11 ...
- assert函数用法总结【转】
本文转载自:http://blog.csdn.net/u014082714/article/details/45190505 assert宏的原型定义在<assert.h>中,其作用是如果 ...
- HDU1251 统计难题 (字典树模板)题解
思路:模板题,贴个模板 代码: #include<cstdio> #include<cstring> #include<cstdlib> #include<q ...
- BZOJ5293: [Bjoi2018]求和 树上差分
Description master 对树上的求和非常感兴趣.他生成了一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k 次方和,而且每次的k 可能是不同的.此处节点深度的定义是这个节点 ...
- caffe深度学习网络(.prototxt)在线可视化工具:Netscope Editor
http://ethereon.github.io/netscope/#/editor 网址:http://ethereon.github.io/netscope/#/editor 将.prototx ...
- Elasticsearch 原理
Elasticsearch简介 Elasticsearch是一个基于Apache lucene的实时分布式搜索.具有以下优点: 1.实时处理大规模数据.2.全文检索,能够做到结构化检索和聚合分析.3. ...
- LA 3266 田忌赛马
https://vjudge.net/problem/UVALive-3266 题意: 田忌赛马,赢一局得200两银子,输一局赔200两银子,平局不赔不赚,问最多能赚多少银子. 思路: 先排序,然后比 ...
- Z-score(Z值)的意义--转载
http://blog.sina.com.cn/s/blog_72208a6a0101cdt1.html http://www.docin.com/p-350677620.html http://we ...