Tautology

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

 

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

就是离散数学的一个模拟,判断所给的式子是不是永真公式
用栈逆序推还不算难
 1 #include<stdio.h>

 2 #include<iostream>

 3 #include<string.h>

 4 #include<stack>

 5 using namespace std;

 6 stack<int>sta;

 7 int p,q,r,s,t,a,b;

 8 char str[102];

 9 int f()

10 {

11     int len=strlen(str);

12     for(int i=len-1; i>-1; i--)

13     {

14         if(str[i]=='p')sta.push(p);//是字符直接入栈

15         else if(str[i]=='q')sta.push(q);

16         else if(str[i]=='r')sta.push(r);

17         else if(str[i]=='s')sta.push(s);

18         else if(str[i]=='t')sta.push(t);

19         else if(str[i]=='K')//是运算就进行运算

20         {

21             a=sta.top();

22             sta.pop();

23             b=sta.top();

24             sta.pop();

25             sta.push(a&&b);

26         }

27         else if(str[i]=='A')

28         {

29             a=sta.top();

30             sta.pop();

31             b=sta.top();

32             sta.pop();

33             sta.push(a||b);

34         }

35         else if(str[i]=='C')

36         {

37             a=sta.top();

38             sta.pop();

39             b=sta.top();

40             sta.pop();

41             if(a&&!b)sta.push(0);

42             else sta.push(1);

43         }

44         else if(str[i]=='E')

45         {

46             a=sta.top();

47             sta.pop();

48             b=sta.top();

49             sta.pop();

50             if(a==b)sta.push(1);

51             else sta.push(0);

52         }

53         else if(str[i]=='N')

54         {

55             a=sta.top();

56             sta.pop();

57             sta.push(!a);

58         }

59     }

60     return sta.top();

61 }

62 int sf()//枚举各种情况

63 {

64     for(p=0; p<2; p++)

65         for(q=0; q<2; q++)

66             for(r=0; r<2; r++)

67                 for(s=0; s<2; s++)

68                     for(t=0; t<2; t++)

69                         if(!f())

70                             return 0;

71     return 1;

72 }

73 int main()

74 {

75     while(gets(str))

76     {

77         if(!strcmp(str,"0"))

78             break;

79         else

80         {

81             if(sf())

82                 printf("tautology\n");

83             else

84                 printf("not\n");

85         }

86     }

87     return 0;

88 }
View Code

 

你可能感兴趣的:(auto)