coder_new

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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, Nwis 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

 

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
32种情况,使用位运算效率很高

位运算,将整数的二进制某一位翻转可采用id^=(1<<x)(x代表要翻转的位置)

^异或   与0异或为本身 与1异或为取反

<<左移   >>右移

其他&按位与 |按位或 ~按位取反


View Code
 1 #include <iostream>
 2 using namespace std;
 3 char str[101];
 4 int pos;
 5 bool judge( char str[], int value )
 6 {
 7     pos++;
 8     switch ( str[pos] )
 9     {
10     case 'p': return value&1;
11     case 'q': return (value>>1)&1;
12     case 'r': return (value>>2)&1;
13     case 's': return (value>>3)&1;
14     case 't': return (value>>4)&1;
15     case 'K': return judge(str,value)&judge(str,value);
16     case 'A': return judge(str,value)|judge(str,value);
17     case 'N': return !judge(str,value);
18     case 'C': return (!judge(str,value))|judge(str,value);
19     case 'E': return judge(str,value)==judge(str,value);
20     default:;
21     }
22 }
23 int main()
24 {
25     bool mark;
26     while ( cin >> str && str[0] != '0' )
27     {
28         mark = true;
29         for ( int i = 0; i < 32; i++ )
30         {
31             pos = -1;
32             if ( !judge(str,i) ) 
33             {
34                 mark = false; break;
35             }
36         }
37         if ( mark ) cout << "tautology" << endl;
38         else cout << "not" << endl;
39     }
40     return 0;
41 }

 

posted on 2012-05-09 19:59  coder_new  阅读(282)  评论(1编辑  收藏  举报