POJ 3295 Tautology(构造法)

来源:互联网 发布:天猫耐克官网抢鞋软件 编辑:程序博客网 时间:2024/06/01 07:54

Time Limit: 1000MS Memory Limit: 65536K
题目地址:http://poj.org/problem?id=3295


Problem 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

Source

Waterloo Local Contest, 2006.9.30

解题思路

问题中的K,A,N,C,E可以看作是运算符,p, q, r, s, t可以看作是运算的元素,它们之间的运算关系在表格中已经清晰地给出。这道题的标签是“构造”,意思应该是构造出栈来进行计算,这种方法也运用于括号匹配等问题。解题方案就是从后往前扫描一遍字符串,遇到小写字母就压栈,遇到大写字母就运算。(具体做法见代码注释)由于题目保证给出的字符串是WWF,所以最后一定可以保证栈中只剩一个元素。总的来说只要逻辑清晰,解决类似问题并不难。

AC代码

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<string>#include<queue>#include<sstream>#include<list>#include<stack>#define ll long long#define ull unsigned long long#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define rrep(i,a,b) for(int i=(a),_ed=(b);i>=_ed;i--)#define fil(a,b) memset((a),(b),sizeof(a))#define cl(a) fil(a,0)#define PI 3.1415927#define inf 0x3f3f3f3fusing namespace std;int p, q, r, s, t;string in;stack<int> ex;int judge(){    rrep(i, in.size() - 1, 0)//从后向前读字符串    {        if (islower(in[i]))//元素入栈操作        {            if (in[i] == 'p') ex.push(p);            if (in[i] == 'q') ex.push(q);            if (in[i] == 'r') ex.push(r);            if (in[i] == 's') ex.push(s);            if (in[i] == 't') ex.push(t);        }        else//运算操作        {            if (in[i] == 'N') { if (ex.top()) { ex.pop(); ex.push(0); } else { ex.pop(); ex.push(1); } }            if (in[i] == 'E') { int temp = ex.top();ex.pop(); if (ex.top() == temp) { ex.pop();ex.push(1); } else {  ex.pop();ex.push(0);} }            if (in[i] == 'K') { int temp = ex.top();ex.pop(); if (ex.top() & temp) { ex.pop();ex.push(1); } else { ex.pop();ex.push(0); } }            if (in[i] == 'A') { int temp = ex.top();ex.pop(); if (ex.top() | temp) { ex.pop();ex.push(1); } else { ex.pop();ex.push(0); } }            if (in[i] == 'C') { int temp = ex.top();ex.pop(); if (ex.top() <= temp) { ex.pop();ex.push(1); } else { ex.pop();ex.push(0); } }        }    }    return ex.top();//返回0表示不是tautology}int main(void){    while (cin >> in && !(in[0] == '0'&&in.size() == 1))    {        int flag = 1;        rep(it, 0, 31)//由于只有5种运算元素,此处可枚举所有的32种情况        {            while(!ex.empty()) ex.pop();            int i = it;            p = i & 1;i >>= 1;            q = i & 1;i >>= 1;            r = i & 1;i >>= 1;            s = i & 1;i >>= 1;            t = i & 1;            flag = judge();            if (!flag) break;        }        if (flag) cout << "tautology\n";        else cout << "not\n";    }    return 0;}

2016 年 09月 21日

0 0
原创粉丝点击