首页 > 其他 > 详细

POJ 3295 Tautology(构造法)

时间:2017-02-05 23:53:44      阅读:346      评论:0      收藏:0      [点我收藏+]

http://poj.org/problem?id=3295

题意:

判断表达式是否为永真式。

思路:

把每种情况都枚举一下。

 1 #include<iostream> 
 2 #include<string>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 const int MAXN = 120;
 7 
 8 int sta[MAXN];
 9 char str[MAXN];
10 int p, q, r, s, t;
11 
12 void  judge()
13 {
14     int top = 0;
15     int len = strlen(str);
16     for (int i = len - 1; i >= 0; i--)
17     {
18         if (str[i] == p) sta[top++] = p;
19         else if (str[i] == q) sta[top++] = q;
20         else if (str[i] == r) sta[top++] = r;
21         else if (str[i] == s) sta[top++] = s;
22         else if (str[i] == t) sta[top++] = t;
23         else if (str[i] == K)
24         {
25             int t1 = sta[--top];
26             int t2 = sta[--top];
27             sta[top++] = (t1&&t2);
28         }
29         else if (str[i] == A)
30         {
31             int t1 = sta[--top];
32             int t2 = sta[--top];
33             sta[top++] = (t1 || t2);
34         }
35         else if (str[i] == N)
36         {
37             int t1 = sta[--top];
38             sta[top++] = (!t1);
39         }
40         else if (str[i] == C)
41         {
42             int t1 = sta[--top];
43             int t2 = sta[--top];
44             if (t1 == 1 && t2 == 0)sta[top++] = 0;
45             else sta[top++] = 1;
46         }
47         else if (str[i] == E)
48         {
49             int t1 = sta[--top];
50             int t2 = sta[--top];
51             if ((t1 == 1 && t2 == 1) || (t1 == 0 && t2 == 0)) sta[top++] = 1;
52             else sta[top++] = 0;
53         }
54     }
55 }
56 
57 bool solve()
58 {
59     for (p = 0; p<2; p++)
60     for (q = 0; q<2; q++)
61     for (r = 0; r<2; r++)
62     for (s = 0; s<2; s++)
63     for (t = 0; t<2; t++)
64     {
65         judge();
66         if (sta[0] == 0)return false;
67     }
68     return true;
69 }
70 
71 int main()
72 {
73     //freopen("D:\\txt.txt", "r", stdin);
74     while (gets(str) && str[0] != 0)
75     {
76         if (solve())  cout << "tautology" << endl;
77         else          cout << "not" << endl;
78     }
79     return 0;
80 }

 

POJ 3295 Tautology(构造法)

原文:http://www.cnblogs.com/zyb993963526/p/6368821.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!