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

题意:

判断表达式是否为永真式。

思路:

把每种情况都枚举一下。

 #include<iostream>
#include<string>
#include<cstring>
using namespace std; const int MAXN = ; int sta[MAXN];
char str[MAXN];
int p, q, r, s, t; void judge()
{
int top = ;
int len = strlen(str);
for (int i = len - ; i >= ; i--)
{
if (str[i] == 'p') sta[top++] = p;
else if (str[i] == 'q') sta[top++] = q;
else if (str[i] == 'r') sta[top++] = r;
else if (str[i] == 's') sta[top++] = s;
else if (str[i] == 't') sta[top++] = t;
else if (str[i] == 'K')
{
int t1 = sta[--top];
int t2 = sta[--top];
sta[top++] = (t1&&t2);
}
else if (str[i] == 'A')
{
int t1 = sta[--top];
int t2 = sta[--top];
sta[top++] = (t1 || t2);
}
else if (str[i] == 'N')
{
int t1 = sta[--top];
sta[top++] = (!t1);
}
else if (str[i] == 'C')
{
int t1 = sta[--top];
int t2 = sta[--top];
if (t1 == && t2 == )sta[top++] = ;
else sta[top++] = ;
}
else if (str[i] == 'E')
{
int t1 = sta[--top];
int t2 = sta[--top];
if ((t1 == && t2 == ) || (t1 == && t2 == )) sta[top++] = ;
else sta[top++] = ;
}
}
} bool solve()
{
for (p = ; p<; p++)
for (q = ; q<; q++)
for (r = ; r<; r++)
for (s = ; s<; s++)
for (t = ; t<; t++)
{
judge();
if (sta[] == )return false;
}
return true;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (gets(str) && str[] != '')
{
if (solve()) cout << "tautology" << endl;
else cout << "not" << endl;
}
return ;
}
05-11 14:48