您的位置:首页 > 其它

Tautology(POJ_3295)

2016-01-21 21:22 387 查看

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.

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.

Definitions of K, A, N, C, and E
     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


代码                                   

//方法可能略挫,看到其他人都是逆序入栈,就我是顺序入栈= =

#include <iostream>
#include <stack>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
char str[1100];
while(scanf("%s", str) != EOF)
{
int flag[5];
memset(flag, -1, sizeof(-1));
if(strcmp(str, "0") == 0)
break;
int len = strlen(str);
stack<char> s;
while(s.size())
s.pop();
for(int i = 0; i < len; i++)
{
if(str[i] >= 'p' && str[i] <= 't')
flag[str[i]-'p'] = 0;
}
bool ans = 1;
while(1)
{
for(int i = 0; i < len; i++)
{
if(str[i] >= 'A' && str[i] <= 'N')
{
s.push(str[i]);
continue;
}
else
s.push(flag[str[i]-'p']+'0');
while(s.size() > 1)
{
char x = s.top();
s.pop();
char w = s.top();
s.pop();
if(w == 'N' && isdigit(x))
{
s.push((x-'0'+1)%2+'0');
}
else if(isdigit(x) && isdigit(w))
{
char c = s.top();
s.pop();
if(c == 'A')
{
if(w == '0' && x == '0')
s.push('0');
else
s.push('1');
}
else if(c == 'K')
{
if(w == '1' && x == '1')
s.push('1');
else
s.push('0');
}
else if(c == 'C')
{
if(w == '1' && x == '0')
s.push('0');
else
s.push('1');
}
else if(c == 'E')
{
if(w != x)
s.push('0');
else
s.push('1');
}
}
else
{
s.push(w);
s.push(x);
break;
}
}
}
if(s.size() == 1 && s.top() == '0')
{
ans = 0;
break;
}
while(s.size())
s.pop();
int i;
for(i = 4; i >= 0; i--)
{
if(flag[i] != -1)
flag[i] = (flag[i] + 1) % 2;
if(flag[i] == 1)
break;
}
if(i == -1)
break;
}
if(ans)
printf("tautology\n");
else
printf("not\n");
}
return 0;
}


//听了QAQ巨巨的讲解疯狂优化了下我的代码= =正序也改成逆序惹,还是简洁明了的代码看起来舒服,仰慕高端QAQ。

#include <iostream>
#include <stack>
#include <cstdio>
#include <cstring>

using namespace std;

char str[110];

bool Cal(char c, bool a, bool b)
{
if(c == 'A') return a||b;
if(c == 'K') return a&&b;
if(c == 'C') return a==b || ((!a)&&b);
return a==b;
}

bool Judge()
{
bool flag[5], a, b;
for(int i = 0; i < (1 << 5); i++)
{
for(int j = 0, k = i; j < 5; j++)
{
flag[j] = k % 2;
k /= 2;
}
stack<bool> s;
int len = strlen(str);
for(int j = len-1; j >= 0; j--)
{
if(str[j] >= 'p' && str[j] <='t')
s.push(flag[str[j]-'p']);
else if(str[j] == 'N')
{
a = s.top();
s.pop();
s.push(!a);
}
else
{
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(Cal(str[j], a, b));
}
}
if(!s.top()) return 0;
}
return 1;
}

int main()
{
while(scanf("%s", str) && str[0] != '0')
{
printf(Judge() == 1 ? "tautology\n" : "not\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: