<div class="ptt" lang="en-US" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue;">Tautology</div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 1000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 65536K</td></tr><tr><td><strong>Total Submissions:</strong> 11831</td><td width="10px"> </td><td><strong>Accepted:</strong> 4476</td></tr></tbody></table></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Description</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;"><p>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><ul><li>p, q, r, s, and t are WFFs</li><li>if <em>w</em> is a WFF, N<em>w</em> is a WFF</li><li>if <em>w</em> and <em>x</em> are WFFs, K<em>wx</em>, A<em>wx</em>, C<em>wx</em>, and E<em>wx</em> are WFFs.</li></ul>The meaning of a WFF is defined as follows:<ul><li>p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).</li><li>K, A, N, C, E mean <em>and, or, not, implies,</em> and <em>equals</em> as defined in the truth table below.</li></ul><center><table cols="1" width="80%" bgcolor="#ccffff" border="1" id="table1"><tbody><tr><td><center><strong><span size="+1" style="color:#000099;">Definitions of K, A, N, C, and E</span></strong></center></td></tr></tbody></table><table cols="6" width="80%" bgcolor="#ccffff" border="1" id="table2"><tbody><tr><td> <strong><span size="+1" style="font-family:Courier New,Courier;"><em>w x</em></span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> K<em>wx</em></span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> A<em>wx</em></span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> N<em>w</em></span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> C<em>wx</em></span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> E<em>wx</em></span></strong></td></tr><tr><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td></tr><tr><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1 0</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0</span></strong></td></tr><tr><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0</span></strong></td></tr><tr><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0 0</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 0</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td><td><strong><span size="+1" style="font-family:Courier New,Courier;"> 1</span></strong></td></tr></tbody></table></center><p></p><p>A <em>tautology</em> is a WFF that has value 1 (true) regardless of the values of its variables. For example, <em>ApNp</em> is a tautology because it is true regardless of the value of <em>p</em>. On the other hand, <em>ApNq</em> is not, because it has the value 0 for <em>p=0, q=1</em>.</p><p>You must determine whether or not a WFF is a tautology.</p></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Input</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;"><p>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.</p></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Output</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;"><p>For each test case, output a line containing <em>tautology</em> or <em>not</em> as appropriate.</p></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Sample Input</p><pre class="sio" style="font-family: "Courier New", Courier, monospace;font-size:14px;">ApNp
ApNq
0
Sample Output
tautology not
输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式,
其中p、q、r、s、t的值为1(true)或0(false),即逻辑变量;
K、A、N、C、E为逻辑运算符,
K --> and: x && y
A --> or: x || y
N --> not : !x
C --> implies : (!x)||y
E --> equals : x==y
问这个逻辑表达式是否为永真式。
PS:输入格式保证是合法的
#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int pp,qq,rr,ss,tt;
stack<int>s;
int panduan(char c)
{
if(c =='p')
{
s.push(pp);
return 1;
}
else if(c =='q')
{
s.push(qq);
return 1;
}
else if(c == 'r')
{
s.push(rr);
return 1;
}
else if(c == 's')
{
s.push(ss);
return 1;
}
else if(c=='t')
{
s.push(tt);
return 1;
}
return 0;
}
void operators(char ch)
{
if(ch == 'K')
{
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(x&&y);
}
else if(ch == 'A')
{
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(x||y);
}
else if(ch == 'N')
{
int x = s.top();
s.pop();
s.push(!x);
}
else if(ch == 'C')
{
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push((!x)||y);
}
else if(ch == 'E')
{
int x = s.top();
s.pop();
int y = s.top();
s.pop();
s.push(x==y);
}
return;
}
int main()
{
char ww[110];
int i;
while(~scanf("%s",ww)&&ww[0]!='0')
{
//if(strcmp(ww,"0")==0) break;
int flag=1;//放在里面;
int ll = strlen(ww);
for(pp=0;pp<=1;pp++)
{
for(qq=0;qq<=1;qq++)
{
for(rr=0;rr<=1;rr++)
{
for(ss=0;ss<=1;ss++)
{
for(tt=0;tt<=1;tt++)
{
for(i= ll - 1; i >=0 ;i--)
{
if(!panduan(ww[i]))
operators(ww[i]);
}
int ans = s.top();
s.pop();
if(ans==0)
{
flag=0;
break;
}
}
if(!flag)
break;
}
if(!flag)
break;
}
if(!flag)
break;
}
if(!flag)
break;
}
if(flag==1)
printf("tautology\n");
else
printf("not\n");
}
return 0;
}