构造法是指当解决某些数学问题使用通常方法按照定向思维难以解决问题时,应根据题设条件和结论的特征、性质,从新的角度,用新的观点去观察、分析、理解对象,牢牢抓住反映问题的条件与结论之间的内在联系,运用问题的数据、外形、坐标等特征,使用题中的已知条件为原材料,运用已知数学关系式和理论为工具,在思维中构造出满足条件或结论的数学对象,从而,使原问题中隐含的关系和性质在新构造的数学对象中清晰地展现出来,并借助该数学对象方便快捷地解决数学问题的方法。
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.
- 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.
|
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
题意 p q r s t 表示变量 K A N C E表示运算规则 K表与(&&) A表示或(||) N表示非(!) Cxy表示(!x)||y Exy表示x==y
然后注意一下细节 模拟一下
#include<iostream>
#include<stack>
#include<cstdio>
#include<cstring>
using namespace std;
int pp,qq,rr,ss,tt; //各个逻辑变量的值
stack<int> s;
bool isvariables(char ch) //判断ch是否为变量p q r s t,若是则把其当前值入栈
{
switch(ch)
{
case 'p':s.push(pp);return true;
case 'q':s.push(qq);return true;
case 'r':s.push(rr);return true;
case 's':s.push(ss);return true;
case 't':s.push(tt);return true;
}
return false;
}
//判断ch是否为变量p q r s t,若是则把其当前值入栈
void operators(char op)//根据操作符op对栈执行操作
{
switch(op)
{
case 'K':
{
int x=s.top();
s.pop();
int y=s.top();
s.pop();
s.push(x&&y);
break;
}
case 'A':
{
int x=s.top();
s.pop();
int y=s.top();
s.pop();
s.push(x||y);
break;
}
case 'C':
{
int x=s.top();
s.pop();
int y=s.top();
s.pop();
s.push((!x)||y);
break;
}
case 'E':
{
int x=s.top();
s.pop();
int y=s.top();
s.pop();
s.push(x==y);
break;
}
case 'N':
{
int x=s.top();
s.pop();
s.push(!x);
break;
}
}
return;
}
//根据操作符op对栈执行操作
int main(void)
{
char WFF[110];
while(cin>>WFF && WFF[0]!='0')
{
int len=strlen(WFF); //逻辑表达式的长度
bool flag=true; //标记逻辑表达式是否为永真式
for(pp=0;pp<=1;pp++) //枚举逻辑变量的值
{
for(qq=0;qq<=1;qq++)
{
for(rr=0;rr<=1;rr++)
{
for(ss=0;ss<=1;ss++)
{
for(tt=0;tt<=1;tt++)
{
for(int pw=len-1;pw>=0;pw--)
{
if(!isvariables(WFF[pw]))
operators(WFF[pw]);
}
int ans=s.top(); //最后栈剩一个值,即为逻辑表达式的值
s.pop(); //清空栈
if(!ans) //只要表达式有一个值为假,它就不是永真式
{
flag=false;
break;
}
}
if(!flag)
break;
}
if(!flag)
break;
}
if(!flag)
break;
}
if(!flag)
break;
}
if(flag)
cout<<"tautology"<<endl;
else
cout<<"not"<<endl;
}
return 0;
}