POJ 3295 Tautology 构造数列及STL中栈的运用

Tautology
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8184 Accepted: 3139

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
 
由于本人ACM还是很水,这道题刚开始看的时候木有半点思路,题目是一个运算题,我先手动打表,把这张运算表打出来,之后开始进行运算,由于5个数字,每个数字只有0和1,所以直接循环,5个数字分别从0循环到1进行计算,每次循环将5个数带入judge函数进行运算,运算具体方法是先建立一个栈,将数列从后往前读入字母,如果是数字,则将数字入栈,如果是运算符,则根据不同运算符分别令数字出栈后运算,将运算结果再次入栈操作,一直运算至数列字母读取完毕,此时,栈中剩下的唯一数字即为运算结果,判断为0还是1,如果为0,则不符合tautology,直接跳出循环,输出not,如果是1,则继续运算,当返回所有结果均为1,则输出tautology,这样,此题就水过了,此题解法中主页君使用了STL中的栈,其实此知识点并不难,只需要掌握栈中的pop函数,top函数以及push函数其实就可以熟练使用STL的栈了,难者不会,会者不难,就是这个道理。。。
 
本题目AC代码:
 
#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
char ch[105];
int num[105];
int a[5][2][2];
int judge(int p,int q,int r,int s,int t)
{
	stack<int> st;
	int l1=strlen(ch),p1,p2,i;
	for(i=l1-1;i>=0;i--)
	{
		if(ch[i]=='p')
			st.push(p);
		else if(ch[i]=='q')
			st.push(q);
		else if(ch[i]=='r')
			st.push(r);
		else if(ch[i]=='s')
			st.push(s);
		else if(ch[i]=='t')
			st.push(t);
		else if(ch[i]=='K')
		{
			p1=st.top();
			st.pop();
			p2=st.top();
			st.pop();
			st.push(a[0][p1][p2]);
		}
		else if(ch[i]=='A')
		{
			p1=st.top();
			st.pop();
			p2=st.top();
			st.pop();
			st.push(a[1][p1][p2]);
		}
		else if(ch[i]=='C')
		{
			p1=st.top();
			st.pop();
			p2=st.top();
			st.pop();
			st.push(a[2][p1][p2]);
		}
		else if(ch[i]=='E')
		{
			p1=st.top();
			st.pop();
			p2=st.top();
			st.pop();
			st.push(a[3][p1][p2]);
		}
		else if(ch[i]=='N')
		{
			p1=st.top();
			st.pop();
			if(p1==1)
			    st.push(0);
			else
				st.push(1);
		}
	}
	if(st.top()==1)
		return 1;
	else
		return 0;
}
int main()
{
	a[0][1][1]=1;   //k
	a[0][1][0]=0;
	a[0][0][1]=0;
	a[0][0][0]=0;
	a[1][1][1]=1;   //a
	a[1][1][0]=1;
	a[1][0][1]=1;
	a[1][0][0]=0;
	a[2][1][1]=1;   //c
	a[2][1][0]=0;
	a[2][0][1]=1;
	a[2][0][0]=1;
	a[3][1][1]=1;   //e
	a[3][1][0]=0;
	a[3][0][1]=0;
	a[3][0][0]=1;
	int p, q, r, s, t,flag;
	while(1)
	{
		flag=1;
		scanf("%s",ch);
		if(strcmp(ch,"0")==0)
			break;
	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++)
					{
						flag=judge(p,q,r,s,t);
						if(flag==0)
							break;
					}
					if(flag==0)
						break;
				}
				if(flag==0)
					break;
			}
			if(flag==0)
				break;
		}
		if(flag==0)
			break;
	}
	if(flag==0)
		printf("not\n");
	else
		printf("tautology\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值