POJ 3295 Tautology 构造 难度:1

Tautology
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

 

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[5]={'p','q','r','s','t'};

char a[120];

int rep[255];

 int f[120];

int len;

void printrep(){

    for(int i=0;i<5;i++){

        printf("%c%d ",ch[i],rep[ch[i]]);

    }

    puts("");

}

bool isnum(char ch1){

    for(int i=0;i<5;i++)if(ch1==ch[i])return true;

    return false;

}

bool dfs(int ind){

    if(ind<5){if(!dfs(ind+1))return false;rep[ch[ind]]=1;if(!dfs(ind+1))return false;rep[ch[ind]]=0;return true;}

    memset(f,0,sizeof(f));

    int index=0;

    for(int i=len-1;i>=0;i--){

        if(isnum(a[i])){

            f[index++]=rep[a[i]];

        }

        else {

            if(a[i]=='K'){

                f[index-2]=f[index-2]&f[index-1];

                index--;

            }

            if(a[i]=='A'){

                  f[index-2]=f[index-2]|f[index-1];

                index--;

            }

            if(a[i]=='N'){

                f[index-1]=1^f[index-1];

            }

            if(a[i]=='C'){

                f[index-2]=((1^f[index-1])|f[index-2]);

                index--;

            }

            if(a[i]=='E'){

                f[index-2]=1^(f[index-2]^f[index-1]);

                index--;

            }

        }

    }

    if(f[index-1]==0)return false;

    return true;

}

int main()

{

    while(scanf("%s",a)==1&&strcmp(a,"0")){

        memset(rep,0,sizeof(rep));

        len=strlen(a);

        if(dfs(0)){

            puts("tautology");

        }

        else {

            puts("not");

        }

    }

    return 0;

}
View Code
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;

    }

}

还可以字符替换,不贴了,可以增强直观,反正数据不大

 

你可能感兴趣的:(auto)