Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12068 | Accepted: 4597 |
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
Source
题意(摘自優YoU):
输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式,
其中p、q、r、s、t的值为1(true)或0(false),即逻辑变量;
K、A、N、C、E为逻辑运算符,
K --> and: x && y
A --> or: x || y
N --> not : !x
C --> implies : (!x)||y
E --> equals : x==y
问这个逻辑表达式是否为永真式。
PS:输入格式保证是合法的
思路:共有五个数,最多有2^5种情况。从后向前遍历,就像数据结构上的表达式求值得做法类似了。
如果是值得话就压入栈,是运算符的话就将栈顶元素pop出来再求值,将值再压入栈。
不得不说这个题是很难的,以前碰到了不会看别人的做法是直接构造的,挺复杂的,代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int state[5];
char s[205];
int l=0;
int ind()
{
char ch=s[l++];
//printf("%d\n",l);
printf("");
switch(ch)
{
case 'p':
case 'q':
case 'r':
case 's':
case 't':
return state[ch-'p'];
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;
}
}
int main()
{
scanf("%s", s);
while(s[0]!='0')
{
int len=strlen(s);
int mark=1;
for(state[0]=0; state[0]<=1 && mark; state[0]++)
{
for(state[1]=0; state[1]<=1 && mark; state[1]++)
{
for(state[2]=0; state[2]<=1 && mark; state[2]++)
{
for(state[3]=0; state[3]<=1 && mark; state[3]++)
{
for(state[4]=0; state[4]<=1 && mark; state[4]++)
{
l=0;
if(ind()==0)
mark=0;
}
}
}
}
}
if(mark==1)
printf("tautology\n");
else
printf("not\n");
scanf("%s", s);
}
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;
}