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

来源:互联网 发布:鉴衡认证中心 知乎 编辑:程序博客网 时间:2024/05/16 13:49
Tautology
Time Limit: 1000MS Memory Limit: 65536KTotal 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 ofp. On the other hand,ApNq is not, because it has the value 0 forp=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

ApNpApNq0

Sample Output

tautologynot
由于本人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);elsest.push(1);}}if(st.top()==1)return 1;elsereturn 0;}int main(){a[0][1][1]=1;   //ka[0][1][0]=0;a[0][0][1]=0;a[0][0][0]=0;a[1][1][1]=1;   //aa[1][1][0]=1;a[1][0][1]=1;a[1][0][0]=0;a[2][1][1]=1;   //ca[2][1][0]=0;a[2][0][1]=1;a[2][0][0]=1;a[3][1][1]=1;   //ea[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");elseprintf("tautology\n");}return 0;}


原创粉丝点击