Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12321 | Accepted: 4695 |
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.
- 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.
|
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
Source
Waterloo Local Contest, 2006.9.30
题意:
有p、q、r、s、t五个自变量(取0或1),根据题目给的K, A, N, C, E是作用于自变量的操作。操作结果通过查题目给的真值表得出(好离散啊。。)
现在输入多行数据,让我们判断这个句子是不是永真式
这题的分类是构造,然而并不是很懂构造是个啥玩意儿
抱着学习构造的方法差了这题。。。。不小心瞄到用stack来处理就秒懂了。。。。
有种被剧透的感觉。。。。要多练习啊。。
对输入的句子从后往前操作,遇到小写字母将它如栈,大写字母取头两个或一个出栈操作,然后将计算结果入栈
对pqrst枚举所有可能性,因为是2^5所以数据量很小
不过还是在枚举的时候遇到问题,看来我还是递归用的不是很熟练,这个也是个记录路径的问题
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
using namespace std;
int cha[60][5];
stack<int> judge;
int num,len;
string in;
void init(int index){
int i;
if(index>4){
num++;
for(i=0;i<=4;i++){
cha[num][i] = cha[num-1][i];
}
return;
}
cha[num][index] = 1;
init(index+1);
cha[num][index] = 0;
init(index+1);
}
bool run(int index){
int i,j;
while(judge.size()){
judge.pop();
}
int w,x,res;
for(i=len-1;i>=0;i--){
if((int)in[i]<112){
if(in[i]=='K'){
w = judge.top();
judge.pop();
x = judge.top();
judge.pop();
if(w&&x){
res = 1;
}else{
res = 0;
}
judge.push(res);
}else if(in[i]=='A'){
w = judge.top();
judge.pop();
x = judge.top();
judge.pop();
if(w==0&&x==0){
res = 0;
}else{
res = 1;
}
judge.push(res);
}else if(in[i]=='N'){
w = judge.top();
judge.pop();
res = !w;
judge.push(res);
}else if(in[i]=='C'){
w = judge.top();
judge.pop();
x = judge.top();
judge.pop();
if(w==1&&x==0){
res = 0;
}else{
res = 1;
}
judge.push(res);
}else{//'E'
w = judge.top();
judge.pop();
x = judge.top();
judge.pop();
if(w==x){
res = 1;
}else{
res = 0;
}
judge.push(res);
}
}else{
judge.push(cha[index][in[i]-112]);
}
}
if(judge.top()){
return true;
}else{
return false;
}
}
int main(){
//int test = 'p'; //K is 75
//cout<<test<<endl;// -->r is 112
int i,j,k;
init(0);
/*for(i=0;i<=32;i++){
for(j=0;j<5;j++){
cout<<cha[i][j]<<" ";
}
cout<<endl;
}*/
while(cin>>in){
if(in=="0"){
break;
}
len = in.length();
num = 0;
//cout<<"num is "<<num<<endl;-->32
bool flag = true;
for(i=0;i<=32;i++){
if(!run(i)){
flag = false;
}
}
if(flag){
printf("tautology\n");
}else{
printf("not\n");
}
}
return 0;
}