POJ-3295-Tautology-2013-12-04 20:44:18
来源:互联网 发布:php返回数据格式 编辑:程序博客网 时间:2024/05/17 21:45
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.
- 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.
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
ApNpApNq0
Sample Output
tautologynot
Source
# include<stdio.h># include<string.h># include<stdlib.h>char wff[210];int stack[210];int caller(char c,int w,int x){ switch(c) {case'K':return w & x;case'A':return w | x;case'C':return !w | x;case'E':return w == x;}}int getv(char c,int p,int q,int r,int s,int t) { switch (c) { case 'p': return p; case 'q': return q; case 'r': return r; case 's': return s; case 't': return t; } } int main(){//freopen("input.txt","r",stdin);//freopen("output.txt","w",stdout);int i,len,top,state;int p,q,r,s,t;memset(wff,0,sizeof(wff));memset(stack,0,sizeof(stack));while(scanf("%s",wff) && wff[0]!='0'){len = strlen(wff);state = 1;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++){top = 1;memset(stack,0,sizeof(stack));for(i=len-1;i>=0;i--){if(wff[i]>='p' && wff[i]<='t')stack[top++] = getv(wff[i],p,q,r,s,t);else if(wff[i]=='K'|| wff[i]=='A'|| wff[i]=='C'|| wff[i]=='E'){stack[top-2] = caller(wff[i],stack[top-1],stack[top-2]);top--;}else if(wff[i]=='N'){stack[top-1] = (1 - stack[top-1]);}}if(!stack[top-1]){state = 0;break;}}}}}}if(state==1)printf("tautology\n");elseprintf("not\n");memset(wff,0,sizeof(wff));}return 0;}
- POJ-3295-Tautology-2013-12-04 20:44:18
- POJ 3295 Tautology 模拟
- POJ-3295-Tautology
- POJ 3295 Tautology
- POJ 3295 Tautology
- POJ 3295 Tautology
- POJ 3295 Tautology(模拟)
- poj 3295 Tautology
- POJ 3295 Tautology
- POJ 3295 Tautology
- poj 3295 Tautology
- poj-3295 Tautology
- POJ 3295 -Tautology
- POJ 3295 Tautology
- POJ:题号 3295------tautology
- Poj 3295 Tautology
- Tautology POJ 3295
- POJ 3295 Tautology
- 表达树
- java反射机制基础
- Sqlite3 导出数据至文件
- 第三次。2014、1、11
- C之I/O
- POJ-3295-Tautology-2013-12-04 20:44:18
- ThoughtWorks郑大晔校的一天——最后的礼物
- 黑马程序员------------------多线程
- iOS Objective-C Json解析
- 黑马程序员-------面向对象-------
- POJ1753 Flip Game【DFS】
- TabControl闪烁的题目
- 长尾词和目标词的优化选择
- Android AIDL使用详解