Tautology(POJ_3295)
2016-01-21 21:22
387 查看
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 |
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
代码
//方法可能略挫,看到其他人都是逆序入栈,就我是顺序入栈= = #include <iostream> #include <stack> #include <cstdio> #include <cstring> using namespace std; int main() { char str[1100]; while(scanf("%s", str) != EOF) { int flag[5]; memset(flag, -1, sizeof(-1)); if(strcmp(str, "0") == 0) break; int len = strlen(str); stack<char> s; while(s.size()) s.pop(); for(int i = 0; i < len; i++) { if(str[i] >= 'p' && str[i] <= 't') flag[str[i]-'p'] = 0; } bool ans = 1; while(1) { for(int i = 0; i < len; i++) { if(str[i] >= 'A' && str[i] <= 'N') { s.push(str[i]); continue; } else s.push(flag[str[i]-'p']+'0'); while(s.size() > 1) { char x = s.top(); s.pop(); char w = s.top(); s.pop(); if(w == 'N' && isdigit(x)) { s.push((x-'0'+1)%2+'0'); } else if(isdigit(x) && isdigit(w)) { char c = s.top(); s.pop(); if(c == 'A') { if(w == '0' && x == '0') s.push('0'); else s.push('1'); } else if(c == 'K') { if(w == '1' && x == '1') s.push('1'); else s.push('0'); } else if(c == 'C') { if(w == '1' && x == '0') s.push('0'); else s.push('1'); } else if(c == 'E') { if(w != x) s.push('0'); else s.push('1'); } } else { s.push(w); s.push(x); break; } } } if(s.size() == 1 && s.top() == '0') { ans = 0; break; } while(s.size()) s.pop(); int i; for(i = 4; i >= 0; i--) { if(flag[i] != -1) flag[i] = (flag[i] + 1) % 2; if(flag[i] == 1) break; } if(i == -1) break; } if(ans) printf("tautology\n"); else printf("not\n"); } return 0; }
//听了QAQ巨巨的讲解疯狂优化了下我的代码= =正序也改成逆序惹,还是简洁明了的代码看起来舒服,仰慕高端QAQ。 #include <iostream> #include <stack> #include <cstdio> #include <cstring> using namespace std; char str[110]; bool Cal(char c, bool a, bool b) { if(c == 'A') return a||b; if(c == 'K') return a&&b; if(c == 'C') return a==b || ((!a)&&b); return a==b; } bool Judge() { bool flag[5], a, b; for(int i = 0; i < (1 << 5); i++) { for(int j = 0, k = i; j < 5; j++) { flag[j] = k % 2; k /= 2; } stack<bool> s; int len = strlen(str); for(int j = len-1; j >= 0; j--) { if(str[j] >= 'p' && str[j] <='t') s.push(flag[str[j]-'p']); else if(str[j] == 'N') { a = s.top(); s.pop(); s.push(!a); } else { a = s.top(); s.pop(); b = s.top(); s.pop(); s.push(Cal(str[j], a, b)); } } if(!s.top()) return 0; } return 1; } int main() { while(scanf("%s", str) && str[0] != '0') { printf(Judge() == 1 ? "tautology\n" : "not\n"); } return 0; }
相关文章推荐
- bzoj 2301: [HAOI2011]Problem b 【莫比乌斯反演】
- C语言:printf的执行顺序
- Java学习博客等收集
- HDU 1579 记忆化搜索(递归+动态规划)
- x265-1.7版本-x265.h代码注释
- Two Sum(找出数组中两个和等于指定数字的元素)
- 关于对“===”和“==”的思考
- 用AFNetworking实现断点续传,暂停,继续
- 01-SpringMVC Hello,World
- Node利用connect中间件 及bodyParser处理文件上传
- VMware克隆(复制)后网络配置、CentOS设置静态IP、找不到ipv4地址
- java String 类的replace 和replaceAll 方法替换“?”号
- Python实现Logistic 回归实例
- Mac OS安装java runtime环境
- 玩玩三维重建--Visual SFM介绍
- 在linux关机时执行某个脚本
- 用java -X 命令查看JVM的配置说明:
- 十大感动爱情
- Android 高德地图2.80新版地图开发详解(本地定位/地图显示/Mark标记/camera移动和缩放)
- 字节流与字符流的区别详解