POJ 3295 Tautology (构造法)
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:
The meaning of a WFF is defined as follows:
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
|
题意:K A N C E 分别代表了不同的运算符,然后用栈模拟即可,,,构造法。。。。。。。。。。。。
- #include<iostream>
- #include<cstdio>
- #include<stack>
- #include<cstring>
- using namespace std;
- const int N=;
- char str[N];
- int p,q,r,s,t;
- stack<int> st;
- int isvariables(char ch){
- switch(ch){
- case 'p':st.push(p);return ;
- case 'q':st.push(q);return ;
- case 'r':st.push(r);return ;
- case 's':st.push(s);return ;
- case 't':st.push(t);return ;
- }
- return ;
- }
- void operators(char op){
- switch(op){
- case 'K':{
- int x=st.top(); st.pop();
- int y=st.top(); st.pop();
- st.push(x && y);
- }break;
- case 'A':{
- int x=st.top(); st.pop();
- int y=st.top(); st.pop();
- st.push(x || y);
- }break;
- case 'N':{
- int x=st.top(); st.pop();
- st.push(!x);
- }break;
- case 'C':{
- int x=st.top(); st.pop();
- int y=st.top(); st.pop();
- st.push((!x) || y);
- }break;
- case 'E':{
- int x=st.top(); st.pop();
- int y=st.top(); st.pop();
- st.push(x==y);
- }break;
- }
- }
- int main(){
- //freopen("input.txt","r",stdin);
- while(~scanf("%s",str) && str[]!=''){
- int len=strlen(str);
- int flag=;
- for(p=;p<= && flag;p++)
- for(q=;q<= && flag;q++)
- for(r=;r<= && flag;r++)
- for(s=;s<= && flag;s++)
- for(t=;t<= && flag;t++){
- for(int i=len-;i>=;i--)
- if(!isvariables(str[i]))
- operators(str[i]);
- int last=st.top();
- st.pop();
- if(last==)
- flag=;
- }
- if(flag)
- printf("tautology\n");
- else
- printf("not\n");
- }
- return ;
- }
POJ 3295 Tautology (构造法)的更多相关文章
- POJ 3295 Tautology(构造法)
题目网址:http://poj.org/problem?id=3295 题目: Tautology Time Limit: 1000MS Memory Limit: 65536K Total Su ...
- poj 3295 Tautology (构造)
题目:http://poj.org/problem?id=3295 题意:p,q,r,s,t,是五个二进制数. K,A,N,C,E,是五个运算符. K:&& A:||N:! C:(!w ...
- POJ 3295 Tautology 构造 难度:1
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9580 Accepted: 3640 Descrip ...
- [ACM] POJ 3295 Tautology (构造)
Tautology Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9302 Accepted: 3549 Descrip ...
- POJ 3295 Tautology(构造法)
http://poj.org/problem?id=3295 题意: 判断表达式是否为永真式. 思路: 把每种情况都枚举一下. #include<iostream> #include< ...
- 构造 + 离散数学、重言式 - POJ 3295 Tautology
Tautology Description WFF 'N PROOF is a logic game played with dice. Each die has six faces represen ...
- POJ 3295 Tautology (构造题)
字母:K, A, N, C, E 表示逻辑运算 字母:p, q, r, s, t 表示逻辑变量 0 或 1 给一个字符串代表逻辑表达式,如果是永真式输出tautology 否则输出not 枚举每个逻辑 ...
- poj 3295 Tautology(栈)
题目链接:http://poj.org/problem?id=3295 思路分析:判断逻辑表达式是否为永真式问题.根据该表达式的特点,逻辑词在逻辑变量前,类似于后缀表达式求值问题. 算法中使用两个栈, ...
- poj3295 Tautology —— 构造法
题目链接:http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为1(true)或 ...
随机推荐
- 计算Fisher vector和VLAD
This short tutorial shows how to compute Fisher vector and VLAD encodings with VLFeat MATLAB interfa ...
- 第二章 企业项目开发--maven父子模块
2.1.maven父子模块 在实际开发中,我们基本都会用maven父子分模块的方式进行项目的开发. 2.2.实际操作 2.2.1.手工建立一个ssmm0的文件夹,并在该文件夹中加入一个pom.xml文 ...
- C/C++ 获取目录下的文件列表信息
在C/C++编程时,需要获取目录下面的文件列表信息. 1.数据结构 struct dirent { long d_ino; /* inode number 索引 ...
- 关于DLL文件和EXE文件不在同一目录下的设置【转】
https://www.cnblogs.com/chaosimple/archive/2012/08/13/2636181.html 关于DLL文件和EXE文件不在同一目录下的设置 在开发程序结束后, ...
- HTML标签 闭合还是不闭合?
你在写 HTML5 代码的时候,是否纠结过应该写 <br /> 还是 <br>,是写 <input /> 还是写 <input>.写 <scrip ...
- SQL锁(转)
说 明 Chaos 无法改写隔离级别更高的事务中的挂起的更改. ReadCommitted 在正在读取数据时保持共享锁,以避免脏读,但是在事务结束之前可以更改数据,从而导致不可重复的读取或幻 ...
- JAVA 读取计算机中相关信息
java读取 计算机 cup号 读取版本号 显卡 .. . . ........ .. . . . package com.swt.common.util; import java.io.Buffer ...
- lua接收图片并进行md5处理
需要luacurl(http://luacurl.luaforge.net/)和MD5两个库函数 curl = require("luacurl") require("m ...
- (回溯法)和为n的所有不增正整数和式分解算法
题目: 利用递归算法输出正整数和为n的所有不增的正整数和式.例如当n=5时,不增的和式如下: 5=5 5=4+1 5=3+2 5=3+1+1 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 ...
- 劣质代码评析——《写给大家看的C语言书(第2版)》附录B之21点程序(八)
[重构](续) 牌的表示: 一副牌有52张,可用一整数数组描述.但是由于在游戏过程中牌数在不断减少,所以用一表示剩余张数的整数和一整数数组共同描述.C99支持一种变量长度数组,但用在这里并没有什么特别 ...