poj 3295
2012-06-20 15:25
399 查看
大意是给定一个前缀布尔表达式,最多有5个变量,判断其是否恒为真。 这个确实不难,0到31地枚举5个变量的所有取值,计算表达式的值即可。 从右到左扫描前缀表达式,遇操作数则入栈,遇n元操作符则弹出n个操作数进行运算并将运算结果入栈。 最终在栈中的唯一操作数即为值。 #include<iostream> #include<cstring> #include<stack> using namespace std; int p,q,r,s,t,slen; char inStr[101]; stack<int> sta; bool Solve() { while(!sta.empty()) sta.pop(); int i,fir,sec; for(i=slen-1;i>=0;--i){ switch(inStr[i]){ case 'p': sta.push(p); break; case 'q': sta.push(q); break; case 'r': sta.push(r); break; case 's': sta.push(s); break; case 't': sta.push(t); break; case 'K': fir=sta.top(); sta.pop(); sec=sta.top(); sta.pop(); sta.push(fir && sec); break; case 'A': fir=sta.top(); sta.pop(); sec=sta.top(); sta.pop(); sta.push(fir || sec); break; case 'N': fir=sta.top(); sta.pop(); sta.push(!fir); break; case 'C': fir=sta.top(); sta.pop(); sec=sta.top(); sta.pop(); sta.push( sec<= fir); break;
// sec<= fir 等价于 sec||!fir
case 'E': fir=sta.top(); sta.pop(); sec=sta.top(); sta.pop(); sta.push( sec== fir); break; break; } } return sta.top(); } int main() { int okn; while(cin>>inStr,inStr[0]!='0'){ slen=strlen(inStr); okn=0; for(p=0;p<2;++p) for(q=0;q<2;++q) for(r=0;r<2;++r) for(s=0;s<2;++s) for(t=0;t<2;++t){ if(Solve()==0) break; else ++okn; } if(okn==32) cout<<"tautology"<<endl; else cout<<"not"<<endl; } return 0; }
相关文章推荐
- poj 3295 Tautology
- POJ 3295 Tautology(模拟)
- poj 3295
- poj 3295 Tautology
- POJ 3295
- POJ 3295 *** Tautology
- POJ-3295-Tautology
- POJ 3295 Tautology(构造)(栈)
- poj 3295 Tautology
- [ACM] POJ 3295 Tautology (构造)
- POJ 3295 Tautology解题报告
- POJ3295--Tautology(模拟)
- POJ3295 Tautology(栈和枚举的应用)
- poj 3295 Tautology(构造法)
- POJ 3295 -Tautology
- POJ 3295 Tautology(构造法)
- poj-3295-Tautology
- poj 3295 Tautology【构造法】【刷题计划】
- POJ-3295-Tautology-2013-12-04 20:44:18
- POJ 3295 Tautology (构造题)