
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.
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
Source
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int p , q , r , s , t ;
int K[][] = { , , , } , A[][] = { , , , } , N[] = { , } , C[][] = { , , , } , E[][] = { , , , } ;
string st ;
int now ;
bool flag ; int calc ()
{
now++ ;
switch (st[now])
{
case 'K' : return K[calc()][calc()] ;
case 'A' : return A[calc()][calc()] ;
case 'N' : return N[calc()] ;
case 'C' : return C[calc()][calc()] ;
case 'E' : return E[calc()][calc()] ;
case 'p' : return p ;
case 'q' : return q ;
case 'r' : return r ;
case 's' : return s ;
case 't' : return t ;
}
}
int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
while (cin >> st && st != "") {
flag = ;
for (p = ; p < && !flag ; p++)
for (q = ; q < && !flag ; q++)
for (r = ; r < && !flag ; r++)
for (s = ; s < && !flag ; s++)
for (t = ; t < && !flag ; t++) {
now = - ;
if ( !calc() )
flag = true ;
}
if (flag)
puts ("not") ;
else
puts ("tautology") ;
}
return ;
}
漂亮的使用了回溯。
转载:http://blog.csdn.net/allenlsy/article/details/4885948
tautology : 中文叫套套理论 , 或 永真式 , 就是无论位运算中的variable怎么变,最后答案都为1
题目里的implies 指 蕴涵 , 当且仅当 (条件q = 1) ----> (结论s = 0) 时为假 ,其余都为真