pku 3295 Tautology 第一周训练——递归+枚举

时间:2022-09-03 13:59:22

http://poj.org/problem?id=3295

首先分别将p,q,r,s,t的值饭分别枚举出来,然后运用递归将问题不到缩小,并判断是否为永真式。

pku 3295 Tautology 第一周训练——递归+枚举pku 3295 Tautology 第一周训练——递归+枚举View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 107
using namespace std;
char str[maxn];
int flag[10];
int start;
int juage()
{
int p,q;
switch(str[++start])
{
case 'K': p = juage(); q = juage();return (p&&q);
case 'A': p = juage(); q = juage();return (p||q);
case 'N': return (!juage());
case 'C': p = juage(); q = juage();return (!p||q);
case 'E': return (juage() == juage());
default : return (flag[str[start] - 'p']);
}
}
int main()
{
//freopen("d.txt","r",stdin);
int i,j,k,s,t;
while (gets(str))
{
if (str[0] == '0') break;
int ans = 1;
for (i = 0; i < 2; ++i)
{
flag[0] = i;
for (j = 0; j < 2; ++j)
{
flag[1] = j;
for (k = 0; k < 2; ++k)
{
flag[2] = k;
for(s = 0; s < 2; ++s)
{
flag[3] = s;
for (t = 0; t < 2; ++t)
{
flag[4] = t;
start = -1;
ans = (ans && juage());
if (!ans) break;
}
}
}
}
}
if (!ans) printf("not\n");
else printf("tautology\n");
}
return 0;
}