借鉴别人的思路,dfs+栈  枚举每一个元素的真假值 然后利用栈,从后往前读入,,,

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 #include <cstdlib>
 6 
 7 using namespace std;
 8 bool num[30];
 9 char s[120];
10 int ans[26];
11 bool b[26];
12 int k;
13 bool dfs(int cur)
14 {
15     if(cur==k)
16     {
17         stack<bool>q;
18         for(int i=strlen(s)-1; i>=0; i--)
19         {
20             if(s[i]>='a' && s[i]<='z')
21                 q.push(b[ans[s[i]-'a']]);
22             else
23             {
24                 bool x,y;
25                 if(s[i]=='C')
26                 {
27                     x=q.top();
28                     q.pop();
29                     y=q.top();
30                     q.pop();
31                     q.push(x&&y);
32                 }
33                 else if(s[i]=='D')
34                 {
35                     x=q.top();
36                     q.pop();
37                     y=q.top();
38                     q.pop();
39                     q.push(x||y);
40                 }
41                 else if(s[i]=='I')
42                 {
43                     x=q.top();
44                     q.pop();
45                     y=q.top();
46                     q.pop();
47                     q.push((!x)||y);
48                 }
49                 else if(s[i]=='E')
50                 {
51                     x=q.top();
52                     q.pop();
53                     y=q.top();
54                     q.pop();
55                     q.push(((!x)||y) && ((!y)||x));
56                 }
57                 else if(s[i]=='N')
58                 {
59                     x=q.top();
60                     q.pop();
61                     q.push(!x);
62                 }
63             }
64         }
65         return q.top();
66     }
67     b[cur]=true;
68     if(dfs(cur+1))
69     {
70         b[cur]=false;
71         return dfs(cur+1);
72     }
73     return false;
74 }
75 int main()
76 {
77     int t;
78     scanf("%d",&t);
79     while(t--)
80     {
81         scanf("%s",s);
82         memset(num, false, sizeof(num));
83         for(int i=0; i<strlen(s); i++)
84         {
85             if(s[i]>='a' && s[i]<='z')
86                 num[s[i]-'a']=true;
87         }
88         k=0;
89         for(int i=0; i<26; i++)
90             if(num[i])
91                 ans[i]=k++;
92         if(dfs(0))
93             puts("YES");
94         else
95             puts("NO");
96     }
97     return 0;
98 }
View Code

 



 

posted on 2013-08-02 16:10  风流monkey  阅读(107)  评论(0)    收藏  举报