Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10061 | Accepted: 3826 |
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
ApNpApNq0
Sample Output
tautologynot
Source
1 #include2 #include 3 #include 4 using namespace std; 5 int p , q , r , s , t ; 6 int K[2][2] = { 0 , 0 , 0 , 1} , A[2][2] = { 0 , 1 , 1 , 1} , N[2] = { 1 , 0} , C[2][2] = { 1 , 1 , 0 , 1} , E[2][2] = { 1 , 0 , 0 , 1} ; 7 string st ; 8 int now ; 9 bool flag ;10 11 int calc ()12 {13 now++ ;14 switch (st[now])15 {16 case 'K' : return K[calc()][calc()] ;17 case 'A' : return A[calc()][calc()] ;18 case 'N' : return N[calc()] ;19 case 'C' : return C[calc()][calc()] ;20 case 'E' : return E[calc()][calc()] ;21 case 'p' : return p ;22 case 'q' : return q ;23 case 'r' : return r ;24 case 's' : return s ;25 case 't' : return t ;26 }27 }28 int main ()29 {30 // freopen ("a.txt" , "r" , stdin) ;31 while (cin >> st && st != "0") {32 flag = 0 ;33 for (p = 0 ; p < 2 && !flag ; p++)34 for (q = 0 ; q < 2 && !flag ; q++)35 for (r = 0 ; r < 2 && !flag ; r++)36 for (s = 0 ; s < 2 && !flag ; s++)37 for (t = 0 ; t < 2 && !flag ; t++) {38 now = -1 ;39 if ( !calc() )40 flag = true ;41 }42 if (flag)43 puts ("not") ;44 else45 puts ("tautology") ;46 }47 return 0 ;48 }
漂亮的使用了回溯。
转载:http://blog.csdn.net/allenlsy/article/details/4885948tautology : 中文叫套套理论 , 或 永真式 , 就是无论位运算中的variable怎么变,最后答案都为1
题目里的implies 指 蕴涵 , 当且仅当 (条件q = 1) ----> (结论s = 0) 时为假 ,其余都为真