Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13580 | Accepted: 5214 |
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.
|
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
题意:p,q,r,s,t为5个变量,取值为0和1,K,A,N,C,E是五种逻辑运算符,分别表示∧,∨, ┐,→, ↔,读取字符串后进行逻辑运算,最后判断运算结果是否为真,如果不是输出“no”。
思路:结合表中的运算方式, 此处直接用栈实现逻辑运算,依次枚举5个变量,当数组元素是变量时,将对应的枚举值压入栈,当数组元素是运算符时,取栈顶元素,进行逻辑运算后压入栈,最后只要栈中元素有假即为0,就跳出循环,输出no.
总结:收获了离散上的小知识点,比如用代码实现↔运算符的话,就用(!x||y),自己比较粗心的是,switch语句后面,自己总是忘记加上终止语句。
这道题也反映了自己对暴力枚举这类基础题还是不够熟悉,打算这两天写写算法入门经典上的枚举题,找找感觉,加油!!!
~~还有,poj不认识我的万能头文件啊~~显示CE
#include<iostream> #include<stack> #include<string.h> using namespace std; #define N 110 char WFF[N]; int pp,qq,tt,rr,ss; stack<int>s; int Judge1(char op) { switch(op) { case 'p':s.push(pp);return 1;//自己易忘记的地方 case 'q':s.push(qq);return 1; case 'r':s.push(rr);return 1; case 's':s.push(ss);return 1; case 't':s.push(tt);return 1; default :return 0; } } void Judge2(char op) { int x,y; switch(op) { case 'K': { x = s.top(); s.pop(); y = s.top(); s.pop(); s.push(x&&y); break; } case 'A': { x = s.top(); s.pop(); y = s.top(); s.pop(); s.push(x||y); break; } case 'C': { x = s.top(); s.pop(); y = s.top(); s.pop(); s.push((!x)||y); break; } case 'E': { x = s.top(); s.pop(); y = s.top(); s.pop(); s.push(x==y); break; } case 'N': { x = s.top(); s.pop(); s.push(!x); break;//自己错误的地方 } } return ; } int main() { int flag,l,i,ans; while(scanf("%s",WFF),WFF[0]!='0') { l = strlen(WFF); flag = 0; for(pp = 0; pp <= 1; pp ++) { for(qq = 0; qq <= 1; qq ++) { for(rr = 0; rr <= 1; rr ++) { for(tt = 0; tt <= 1; tt++) { for(ss = 0; ss <= 1; ss ++) { for(i = l - 1; i >= 0; i --) { if(!Judge1(WFF[i])) Judge2(WFF[i]); } ans = s.top(); s.pop(); if(!ans)//如果最后留下的值为真 { flag = 1; break; } } if(flag) break; } if(flag) break; } if(flag) break; } if(flag) break; } if(!flag) cout<<"tautology"<<endl; else cout<<"not"<<endl; } return 0; }