POJ 3295 Tautology(构造法)

题目链接

在POJ训练计划中构造法的一个题,题目还是挺有意思的,就是离散中真值表给构造出来,马上要考离散了,就当复习一下析取和合取。。。

思路:先搜一下5个命题的真假(枚举),然后从后往前推,用了栈的思想,判断复合命题是否为真。开始以为栈中只能有两个元素,WA了一次,改改OK了。。

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 int p[7],o[200],z;
  5 char str[5000];
  6 int find()
  7 {
  8     int i,j,k,l;
  9     l = strlen(str);
 10     j = 1;
 11     for(i = l-1; i >= 0; i --)
 12     {
 13         for(k = 0; k <= 5; k ++)
 14         {
 15             if(str[i] == 'p'+k)
 16             {
 17                 o[j] = p[k+1];
 18                 j ++;
 19                 break;
 20             }
 21         }
 22         if(k == 6)
 23         {
 24             if(str[i] == 'K')
 25             {
 26                 if(o[j-2] + o[j-1] == 2)
 27                 {
 28                     o[j-2] = 1;
 29                     j = j-1;
 30                 }
 31                 else
 32                 {
 33                     o[j-2] = 0;
 34                     j = j-1;
 35                 }
 36             }
 37             else if(str[i] == 'A')
 38             {
 39                 if(o[j-2] + o[j-1] >= 1)
 40                 {
 41                     o[j-2] = 1;
 42                     j = j-1;
 43                 }
 44                 else
 45                 {
 46                     o[j-2] = 0;
 47                     j = j-1;
 48                 }
 49             }
 50             else if(str[i] == 'N')
 51             {
 52                 if(o[j-1] == 1)
 53                 {
 54                     o[j-1] = 0;
 55                 }
 56                 else
 57                 {
 58                     o[j-1] = 1;
 59                 }
 60             }
 61             else if(str[i] == 'C')
 62             {
 63                 if(o[j-2] == 1&&o[j-1] == 0)
 64                 {
 65                     o[j-2] = 0;
 66                     j = j-1;
 67                 }
 68                 else
 69                 {
 70                     o[j-2] = 1;
 71                     j = j-1;
 72                 }
 73             }
 74             else if(str[i] == 'E')
 75             {
 76                 if(o[j-2]+o[j-1] == 0||o[j-2]+o[j-1] == 2)
 77                 {
 78                     o[j-2] = 1;
 79                     j = j-1;
 80                 }
 81                 else
 82                 {
 83                     o[j-2] = 0;
 84                     j = j-1;
 85                 }
 86             }
 87         }
 88     }
 89     if(o[1] == 1&&j == 2)
 90         return 1;
 91     else
 92         return 0;
 93 }
 94 void dfs(int x)
 95 {
 96     int i;
 97     if(!z) return ;
 98     if(x > 5)
 99     {
100         i = find();
101         if(i)
102             return ;
103         else
104             z = 0;
105     }
106     p[x] = 1;
107     dfs(x+1);
108     p[x] = 0;
109     dfs(x+1);
110 }
111 int main()
112 {
113     while(scanf("%s",str)!=EOF)
114     {
115         z = 1;
116         memset(p,0,sizeof(p));
117         if(str[0]=='0')
118             break;
119         dfs(1);
120         if(!z)
121         printf("not\n");
122         else
123         printf("tautology\n");
124     }
125     return 0;
126 }

 

posted @ 2012-06-09 09:51  Naix_x  阅读(210)  评论(0编辑  收藏  举报