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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章