H - 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.
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
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:
Sample Output:
题目大意:
这道题真的看哭了,这道题给给出了4个逻辑变量p,q,r,s,可以为0和1,还有5个表达式分别为N,C,A,E,K。
N—相当于非
C—相当于(非r||q)
A—相当于或
K—相当于且
E—相当于等于(==)
然后题目会给出字符串,叫你判断是否重定性,这个重定性(就是他这字符串为真),我是这样理解的。
思路分析:
他并没有明确给逻辑变量是1还是0所以有32种搭配方式,你要一个一个的去枚举,然后表达式与逻辑变量,应该从内部一个一个分,比如AqNp,这个可A(q(N(q)))这样分。
代码:
#include<stdio.h>
#include<string.h>
char str[1000];
int a[32];
int lemon1[1000];
int lemon()
{
int flag=0;
int i,j,k;
for(i=0;i<32;i++)//这里是枚举32种情况
{
int num=0;
for(j=0;j<5;j++)
{
if(i & (1 << j))//这是位运算,如果实在不懂可以百度和自己运行一下
{
a[j]=1;
}
else
a[j]=0;
}
for(k=strlen(str)-1;k>=0;k--)//开始从内部来分
{
if(str[k]>='p' && str[k]<='t')
{
lemon1[num++]=a[str[k]-'p'];
}
else
{
switch(str[k])
{
case 'K':
num--;
lemon1[num-1]=lemon1[num-1] && lemon1[num];
break;
case 'A':
num--;
lemon1[num-1]=lemon1[num-1] || lemon1[num];
break;
case 'N':
lemon1[num-1]=(!lemon1[num-1]);
break;
case 'C':
num--;
lemon1[num-1]=((!lemon1[num-1])|| lemon1[num]);
break;
case 'E':
num--;
lemon1[num-1]=(lemon1[num-1]==lemon1[num]);
break;
}
}
}
if(!lemon1[0])
{
flag=1;
break;
}
}
return flag;
}
void lemon2(int flag)//判断重定式
{
if(flag)
{
printf("not\n");
}
else
printf("tautology\n");
}
int main()
{
while(scanf("%s",str)!=EOF)
{
int flag;
if(str[0]=='0')
{
break;
}
flag=lemon();
lemon2(flag);
}
}