Tautology(POJ3295-构造法)

来源:互联网 发布:abp源码 编辑:程序博客网 时间:2024/06/01 12:53
Tautology
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13386 Accepted: 5123

Description

WFF 'N PROOF is a logic(逻辑的) game played withdice(骰子). Each die has six faces representing somesubset(子集) of the possiblesymbols(象征) K, A, N, C, E, p, q, r, s, t. A Well-formedformula(公式) (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(意味), andequals 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 itsvariables(变量). For example,ApNp is a tautology(同义反复) because it is true regardless of the value ofp. On the other hand, ApNq is not, because it has the value 0 forp=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 100symbols(象征). A line containing 0 follows the last case.

Output

For each test case, output(输出) a line containingtautology or not as appropriate.

Sample Input

ApNpApNq0

Sample Output

tautologynot

Source

Waterloo Local Contest, 2006.9.30

刚开始没看懂题,懵逼了好一阵子。后来还是靠前辈们的博客的指点才得以顺利解题。大神们的代码通常很精炼,有着谈笑间樯橹灰飞烟灭的神韵。

算字符串WWF的值要从后往前解,因为每一个操作都是操作后面的表达式的,要想从前面开始必须先算出后面的表达式,所以从后面往前解才是正确的做法。

#include<cstdio>#include<iostream>#include<cstring>using namespace std;const int MAXN = 120;int stack[MAXN];//数组实现堆栈char WWF[MAXN];int p, q, r, s, t;void  DoIt()//用一个堆栈从字符串末尾进行操作{int top = 0;int len = strlen(WWF);for (int i = len - 1; i >= 0; i--){if (WWF[i] == 'p') stack[top++] = p;else if (WWF[i] == 'q') stack[top++] = q;else if (WWF[i] == 'r') stack[top++] = r;else if (WWF[i] == 's') stack[top++] = s;else if (WWF[i] == 't') stack[top++] = t;else if (WWF[i] == 'K'){int t1 = stack[--top];int t2 = stack[--top];stack[top++] = (t1&&t2);}else if (WWF[i] == 'A'){int t1 = stack[--top];int t2 = stack[--top];stack[top++] = (t1 || t2);}else if (WWF[i] == 'N'){int t1 = stack[--top];stack[top++] = (!t1);}else if (WWF[i] == 'C'){int t1 = stack[--top];int t2 = stack[--top];if (t1 == 1 && t2 == 0)stack[top++] = 0;else stack[top++] = 1;}else if (WWF[i] == 'E'){int t1 = stack[--top];int t2 = stack[--top];if (t1 == t2) stack[top++] = 1;else stack[top++] = 0;}}}bool solve()//p,q,r,s,t枚举所有可能取值{for (p = 0; p<2; p++)for (q = 0; q<2; q++)for (r = 0; r<2; r++)for (s = 0; s<2; s++)for (t = 0; t<2; t++){DoIt();if (stack[0] == 0)return false;}return true;}int main(){while (scanf("%s", &WWF)){if (strcmp(WWF, "0") == 0)break;if (solve())printf("tautology\n");//判断是否是永真式else printf("not\n");}return 0;}
转载地址:http://www.cnblogs.com/kuangbin/archive/2012/08/13/2636855.html

参考博客:優YoU  http://user.qzone.qq.com/289065406/blog/1309062835

原创粉丝点击