Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9580 | Accepted: 3640 |
Description
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, 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, and equals 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 its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the value 0 for p=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 100 symbols. A line containing 0 follows the last case.
Output
For each test case, output a line containing tautology or not as appropriate.
Sample Input
ApNp
ApNq
0
Sample Output
tautology
not
这道题的意思:用字符串的形式给你一个逻辑表达式,判断是否为永真式,逻辑变量只有那五个小写字母
这是我的代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const char ch[]={'p','q','r','s','t'};
char a[];
int rep[];
int f[];
int len;
void printrep(){
for(int i=;i<;i++){
printf("%c%d ",ch[i],rep[ch[i]]);
}
puts("");
}
bool isnum(char ch1){
for(int i=;i<;i++)if(ch1==ch[i])return true;
return false;
}
bool dfs(int ind){
if(ind<){if(!dfs(ind+))return false;rep[ch[ind]]=;if(!dfs(ind+))return false;rep[ch[ind]]=;return true;}
memset(f,,sizeof(f));
int index=;
for(int i=len-;i>=;i--){
if(isnum(a[i])){
f[index++]=rep[a[i]];
}
else {
if(a[i]=='K'){
f[index-]=f[index-]&f[index-];
index--;
}
if(a[i]=='A'){
f[index-]=f[index-]|f[index-];
index--;
}
if(a[i]=='N'){
f[index-]=^f[index-];
}
if(a[i]=='C'){
f[index-]=((^f[index-])|f[index-]);
index--;
}
if(a[i]=='E'){
f[index-]=^(f[index-]^f[index-]);
index--;
}
}
}
if(f[index-]==)return false;
return true;
}
int main()
{
while(scanf("%s",a)==&&strcmp(a,"")){
memset(rep,,sizeof(rep));
len=strlen(a);
if(dfs()){
puts("tautology");
}
else {
puts("not");
}
}
return ;
}
int ind()
{
char ch=s[l++];//把整个堆栈过程和变量分开看了
printf(""); switch(ch)
{
case 'p':
case 'q':
case 'r':
case 's':
case 't':
return state[ch];
break;
case 'K':
return ind()&ind();
break;
case 'A':
return ind()|ind();
break;
case 'N':
return !ind();
break;
case 'C':
return !ind()|ind();
break;
case 'E':
return ind()==ind();
break;
}
}
还可以字符替换,不贴了,可以增强直观,反正数据不大