您的位置:首页 > 其它

poj 3295

2018-04-02 19:02 302 查看
Tautology
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 13843 Accepted: 5343
DescriptionWFF '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.
InputInput 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.
OutputFor each test case, output a line containing tautology or not as appropriate.
Sample InputApNp
ApNq
0Sample Outputtautology
not题意:变量有p, q, r, s, t可取0、1真值,运算符有K, A, N, C, E真值表给出。输入为表达式,问表达式是否为永真。构造加模拟,一开始死活在全为0的时候便不能解,良久输出了栈的大小才发现switch没有写break,导致每次碰到一个变量就压栈进去五个。思路仍是从后向前操作数压栈,遇到运算符弹栈运算再将结果压栈。由于没有括号存在所以只需一个操作数栈即可。大一上课写的表达式计算器简化版,由于变量只有5个取值2^5种,所以能直接枚举。#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<iomanip>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
char a[105], c;
int p, q, r, s, t;
stack<int> n;
bool isn(char c){
return (c=='p'||c=='q'||c=='r'||c=='s'||c=='t');
}
bool iss(char c){
return (c=='K'||c=='A'||c=='C'||c=='E'||c=='N');
}
bool solve()
{
for(int i=strlen(a)-1;i>=0;i--){
c=a[i];
if(isn(c)){
switch(c){ //压操作数入栈
case 'p': n.push(p); break;
case 'q': n.push(q); break;
case 'r': n.push(r); break;
case 's': n.push(s); break;
case 't': n.push(t); break;
}
}
else if(iss(c)){   //若为运算符
if(c=='N'){    //处理Not(一元)
int x=n.top();
n.pop();
if(x==0)
n.push(1);
else n.push(0);
}
else {        //处理二元运算符
int w,x;
w=n.top();
n.pop();
x=n.top();
n.pop();
if(c=='K'){        //K
if(w&&x)
n.push(1);
else n.push(0);
}
else if(c=='A'){    //A
if(w||x)
n.push(1);
else n.push(0);
}
else if(c=='C'){    //C
if(x==0&&w)
n.push(0);
else n.push(1);
}
else if(c=='E'){    //E
if(w==x)
n.push(1);
else n.push(0);
}
}
}
}
return n.
4000
top();
}

int main()
{
bool tau;
while(gets(a)&&a[0]!='0')
{
tau=true;
for(p=0;p<2&τp++){ //测试所有真值组合
for(q=0;q<2&τq++){
for(r=0;r<2&τr++){
for(s=0;s<2&τs++){
for(t=0;t<2&τt++){
if(!solve()){
tau=false;
}
}
}
}
}
}
if(tau)
cout<<"tautology"<<endl;
else cout<<"not"<<endl;
while(!n.empty())
n.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: