Tautology
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 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.
For each test case, output a line containing tautology or not as appropriate.
ApNp ApNq 0
tautology not题意:
#include<stdio.h>
#include<string.h>
int main()
{
int stack[200];
char str[200];
int p,q,r,s,t;
while(scanf("%s",&str)&&str[0]!='0')
{
int flag=1;
for(p=0;p<2&&flag;p++)
for(q=0;q<2&&flag;q++)
for(r=0;r<2&&flag;r++)
for(s=0;s<2&&flag;s++)
for(t=0;t<2&&flag;t++)
{
int top=0;
for(int i=strlen(str)-1;i>=0;i--)
{
if(str[i]=='p')stack[top++]=p;
else if(str[i]=='q')stack[top++]=q;
else if(str[i]=='r')stack[top++]=r;
else if(str[i]=='s')stack[top++]=s;
else if(str[i]=='t')stack[top++]=t;
else if(str[i]=='K')
{
int ta=stack[--top];
int tb=stack[--top];
stack[top++]=ta&&tb;
}
else if(str[i]=='A')
{
int ta=stack[--top];
int tb=stack[--top];
stack[top++]=ta||tb;
}
else if(str[i]=='N')
{
int ta=stack[--top];
stack[top++]=!ta;
}
else if(str[i]=='C')
{
int ta=stack[--top];
int tb=stack[--top];
if(ta==1&&tb==0)
stack[top++]=0;
else
stack[top++]=1;
}
else if(str[i]=='E')
{
int ta=stack[--top];
int tb=stack[--top];
if(ta==tb)
stack[top++]=1;
else
stack[top++]=0;
}
}
if(stack[0]==0)
flag=0;
}
if(flag)
printf("tautology\n");
else
printf("not\n");
}
return 0;
}
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
char str[200];
while(cin>>str&&str[0]!='0')
{
int p,q,r,s,t;
int flag=1;
for(p=0;p<2&&flag;p++)
for(q=0;q<2&&flag;q++)
for(r=0;r<2&&flag;r++)
for(s=0;s<2&&flag;s++)
for(t=0;t<2&&flag;t++)
{
stack<int >st;
for(int i=strlen(str)-1;i>=0;i--)
{
if(str[i]=='p')
st.push(p);
else if(str[i]=='q')
st.push(q);
else if(str[i]=='r')
st.push(r);
else if(str[i]=='s')
st.push(s);
else if(str[i]=='t')
st.push(t);
else if(str[i]=='K')
{
int ta=st.top();
st.pop();
int tb=st.top();
st.pop();
st.push(ta&&tb);
}
else if(str[i]=='A')
{
int ta=st.top();
st.pop();
int tb=st.top();
st.pop();
st.push(ta||tb);
}
else if(str[i]=='N')
{
int ta=st.top();
st.pop();
st.push(!ta);
}
else if(str[i]=='C')
{
int ta=st.top();
st.pop();
int tb=st.top();
st.pop();
if(ta==1&&tb==0)
st.push(0);
else
st.push(1);
}
else if(str[i]=='E')
{
int ta=st.top();
st.pop();
int tb=st.top();
st.pop();
if(ta==tb)
st.push(1);
else
st.push(0);
}
}
flag=st.top();
}
if(flag)
cout<<"tautology\n";
else
cout<<"not\n";
}
return 0;
}