Tautology(栈或者用数组模拟)
原题:
Tautology
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 32 Accepted Submission(s) : 22
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 ofp. 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.
知识背景:需要离散数学的知识(顿时感觉到了数学的强大)。
题目大意:
K, A, N, C, E 分别代表“且”,“或”,“非”,“蕴含”,“相等”五种运算,对p, q, r, s, t进行上述五种运算,若表达式为永真式,输出“tautology”,否则输出“not”。
解题思路:
枚举p, q, r, s, t的取值,共有2的5次方种情况。
解法一:栈
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
int p,q,r,s,t;
int charge(char c)
{
if(c=='p') return p;
if(c=='q') return q;
if(c=='r') return r;
if(c=='s') return s;
if(c=='t') return t;
}
int main()
{
char str[110];
int i,x,y;
while(scanf("%s",str)&&str[0]!='0')
{
int flag=1;
stack<int>st;
for(p=0;p<=1&&flag;p++)
for(q=0;q<=1&&flag;q++)
for(r=0;r<=1&&flag;r++)
for(s=0;s<=1&&flag;s++)
for(t=0;t<=1&&flag;t++)
{
for(i=strlen(str)-1;i>=0;i--)
{
if(str[i]=='p'||str[i]=='q'||str[i]=='r'
||str[i]=='s'||str[i]=='t')
st.push(charge(str[i]));
else if(str[i]=='K')
{
x=st.top();
st.pop();
y=st.top();
st.pop();
st.push(x&&y);
}
else if(str[i]=='A')
{
x=st.top();
st.pop();
y=st.top();
st.pop();
st.push(x||y);
}
else if(str[i]=='N')
{
x=st.top();
st.pop();
st.push(!x);
}
else if(str[i]=='C')
{
x=st.top();
st.pop();
y=st.top();
st.pop();
if(x==1&&y==0) st.push(0);
else st.push(1);
}
else if(str[i]=='E')
{
x=st.top();
st.pop();
y=st.top();
st.pop();
st.push((x==y));
}
else
flag=0; }
if(flag)
{
flag=st.top();
st.pop();
}
}
if(flag)
printf("tautology\n");
else
printf("not\n");
}
return 0;
}
解法二:用数组模拟栈
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int stack[1000];
char str[1000];
int put()
{
int pp,qq,rr,ss,tt,n,top,i;
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++)
{
top=0;
n=strlen(str);
for(i=n-1;i>=0;i--)
{
if(str[i]=='q')stack[top++]=qq;
if(str[i]=='p')stack[top++]=pp;
if(str[i]=='r')stack[top++]=rr;
if(str[i]=='t')stack[top++]=tt;
if(str[i]=='s')stack[top++]=ss;
if(str[i]=='K')top--,stack[top-1]=(stack[top-1]&&stack[top]);
if(str[i]=='A')top--,stack[top-1]=(stack[top-1]||stack[top]);
if(str[i]=='N')stack[top-1]=!stack[top-1];
if(str[i]=='C')top--,stack[top-1]=((!stack[top-1])||stack[top]);
if(str[i]=='E')top--,stack[top-1]=((stack[top-1])==stack[top]);
}
if(top!=1||stack[top-1]!=1)
return 0;
}
}
}
}
}
return 1;
}
int main()
{
while(gets(str)&&str[0]!='0')
{
if(put()==1)printf("tautology\n");
else
printf("not\n");
}
return 0;
}
浙公网安备 33010602011771号