poj 3295 Tautology [ 栈 ]
2015-03-21 17:31
288 查看
传送门
Tautology
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.
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
Sample Output
Source
Waterloo Local Contest, 2006.9.30
用栈处理计算过程,每种情况暴力跑一次
Tautology
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10107 | Accepted: 3850 |
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 |
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
用栈处理计算过程,每种情况暴力跑一次
13990104 | njczy2010 | 3295 | Accepted | 552K | 47MS | G++ | 2592B | 2015-03-21 17:29:21 |
#include <cstdio> #include <cstring> #include <stack> #include <vector> #include <algorithm> #include <map> #define ll long long int const N = 105; int const M = 205; int const inf = 1000000000; ll const mod = 1000000007; using namespace std; int l; char ssss ; int flag; int ss ; void ini() { flag=1; l=strlen(ssss); //printf(" l=%d\n",l); } int fun(int x,int a,int b) { if(x==2){ return a & b; } else if(x==3){ return a | b; } else if(x==4){ if(a==1 && b==0){ return 0; } else return 1; } else if(x==5){ return a==b ? 1 : 0; } return 0; } void cal(int p,int q,int r,int s,int t) { // printf(" p=%d q=%d flag=%d\n",p,q,flag); int top; map<char,int>mp; mp['p']=p; mp['q']=q; mp['r']=r; mp['s']=s; mp['t']=t; mp['K']=2; mp['A']=3; mp['N']=6; mp['C']=4; mp['E']=5; top=0; int te; int a,b,x; for(int i=0;i<l;i++){ te=mp[ ssss[i] ]; top++; ss[top]=te; if(te>=2){ continue; } else{ while(1) { if(top>=2 && ss[top-1]==6){ te=ss[top]; top--; ss[top]=1-te; } else if(top>=3 && ss[top]<=1 && ss[top-1]<=1 && ss[top-2]>=2 && ss[top-2]<=5){ b=ss[top]; top--; a=ss[top]; top--; x=ss[top]; //top++; ss[top]=fun(x,a,b); // printf(" top=%") } else break; } } } if(ss[1]==0){ flag=0; } //printf(" p=%d q=%d ss=%d flag=%d\n",p,q,ss[1],flag); } void solve() { //printf(" sol\n"); int p,q,r,s,t; for(p=0;p<=1;p++) for(q=0;q<=1;q++) for(r=0;r<=1;r++) for(s=0;s<=1;s++) for(t=0;t<=1;t++) cal(p,q,r,s,t); } void out() { if(flag==1){ printf("tautology\n"); } else{ printf("not\n"); } } int main() { //freopen("data.in","r",stdin); //scanf("%d",&T); // for(cnt=1;cnt<=T;cnt++) //while(T--) while(scanf("%s",ssss)!=EOF) { if(strcmp(ssss,"0")==0) break; ini(); solve(); out(); } }
相关文章推荐
- Poj 3295 Tautology
- poj 3295 Tautology
- POJ 3295:Tautology
- POJ-3295 Tautology 枚举+DFS
- POJ:题号 3295------tautology
- POJ3295 Tautology (模拟)
- POJ-3295 Tautology 构造法
- POJ 3295 Tautology
- poj 3295 Tautology
- Tautology--POJ 3295
- poj 3295 Tautology
- poj 3295 Tautology
- POJ 3295:Tautology
- POJ 3295 Tautology(构造法)
- POJ 3295 Tautology
- Tautology(POJ 3295编译原理)
- poj 3295 Tautology
- poj 3295 Tautology
- POJ 3295-Tautology(构造法+栈)
- POJ 3295 *** Tautology