1.Link:
http://poj.org/problem?id=3295
2.content:
Tautology
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9793 Accepted: 3712 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:
The meaning of a WFF is defined as follows:
- 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.
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 0Sample Output
tautology notSource
Waterloo Local Contest, 2006.9.30
3.method:
将p,q,r,s,t分别置入以0,1代入,使用五个for循环实现,这样会出现相同计算多次的情况,不过由于循环次数很少,所以不影响大的效率。
将字符串从后向前置入栈,遇到运算符则进行运算,然后将结果置入栈。
最后结果非1的话利用goto跳出多层循环
4.code:
1 #include <iostream> 2 #include <string> 3 #include <stack> 4 5 using namespace std; 6 7 int main() 8 { 9 //freopen("D://input.txt","r",stdin); 10 11 string str; 12 cin >> str; 13 14 while(str.size() != 1 || str[0] != '0') 15 { 16 //cout << str << endl; 17 18 int p,q,r,s,t; 19 for(p = 0; p < 2; ++p) 20 { 21 for(q = 0; q < 2; ++q) 22 { 23 for(r = 0; r < 2; ++r) 24 { 25 for(s = 0; s < 2; ++s) 26 { 27 for(t = 0; t < 2; ++t) 28 { 29 stack<int> s_str; 30 int w,x; 31 for(string::reverse_iterator r_iter = str.rbegin(); r_iter != str.rend(); ++r_iter) 32 //for(int str_i = str.size() - 1; str_i >= 0; --str_i) 33 { 34 if(*r_iter == 'p') s_str.push(p); 35 else if(*r_iter == 'q') s_str.push(q); 36 else if(*r_iter == 'r') s_str.push(r); 37 else if(*r_iter == 's') s_str.push(s); 38 else if(*r_iter == 't') s_str.push(t); 39 else 40 { 41 w = s_str.top(); 42 s_str.pop(); 43 if(*r_iter == 'N') s_str.push(!w); 44 else 45 { 46 x = s_str.top(); 47 s_str.pop(); 48 if(*r_iter == 'K') s_str.push(w&&x); 49 else if(*r_iter == 'A') s_str.push(w||x); 50 else if(*r_iter == 'C') {if(w == 1 && x == 0) s_str.push(0); else s_str.push(1);} 51 else {if(w == x) s_str.push(1); else s_str.push(0);} 52 } 53 } 54 } 55 if(s_str.top() != 1) goto end; 56 } 57 } 58 } 59 } 60 } 61 62 end: 63 if(p >= 2) cout << "tautology" << endl; 64 else cout << "not" << endl; 65 66 cin >> str; 67 } 68 return 0; 69 }
5.Reference:
http://blog.csdn.net/lyy289065406/article/details/6642766