您的位置:首页 > 其它

poj 3295 Tautology [ 栈 ]

2015-03-21 17:31 288 查看
传送门

Tautology

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 10107Accepted: 3850
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

Source

Waterloo Local Contest, 2006.9.30

用栈处理计算过程,每种情况暴力跑一次

13990104njczy20103295Accepted552K47MSG++2592B2015-03-21 17:29:21
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>

#define ll long long
int const N = 105;
int const M = 205;
int const inf = 1000000000;
ll const mod = 1000000007;

using namespace std;

int l;
char ssss
;
int flag;
int ss
;

void ini()
{
flag=1;
l=strlen(ssss);
//printf(" l=%d\n",l);
}

int fun(int x,int a,int b)
{
if(x==2){
return a & b;
}
else if(x==3){
return a | b;
}
else if(x==4){
if(a==1 && b==0){
return 0;
}
else return 1;
}
else if(x==5){
return a==b ? 1 : 0;
}
return 0;
}

void cal(int p,int q,int r,int s,int t)
{
// printf(" p=%d q=%d flag=%d\n",p,q,flag);
int top;
map<char,int>mp;
mp['p']=p;
mp['q']=q;
mp['r']=r;
mp['s']=s;
mp['t']=t;
mp['K']=2;
mp['A']=3;
mp['N']=6;
mp['C']=4;
mp['E']=5;
top=0;
int te;
int a,b,x;
for(int i=0;i<l;i++){
te=mp[ ssss[i] ];
top++;
ss[top]=te;
if(te>=2){
continue;
}
else{
while(1)
{
if(top>=2 && ss[top-1]==6){
te=ss[top];
top--;
ss[top]=1-te;
}
else if(top>=3 && ss[top]<=1 && ss[top-1]<=1 && ss[top-2]>=2 && ss[top-2]<=5){
b=ss[top];
top--;
a=ss[top];
top--;
x=ss[top];
//top++;
ss[top]=fun(x,a,b);
// printf(" top=%")
}
else break;
}

}
}
if(ss[1]==0){
flag=0;
}
//printf(" p=%d q=%d ss=%d flag=%d\n",p,q,ss[1],flag);
}

void solve()
{
//printf("  sol\n");
int p,q,r,s,t;
for(p=0;p<=1;p++)
for(q=0;q<=1;q++)
for(r=0;r<=1;r++)
for(s=0;s<=1;s++)
for(t=0;t<=1;t++)
cal(p,q,r,s,t);
}

void out()
{
if(flag==1){
printf("tautology\n");
}
else{
printf("not\n");
}
}

int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
// for(cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%s",ssss)!=EOF)
{
if(strcmp(ssss,"0")==0) break;
ini();
solve();
out();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: