zoukankan      html  css  js  c++  java
  • poj 3295 Tautology (构造)

    题目:http://poj.org/problem?id=3295

    题意:p,q,r,s,t,是五个二进制数。

    K,A,N,C,E,是五个运算符。

    K:&&

    A:||
    N:!

    C:(!w)||x

    E:w==x

    题意是让求如果对于五个数的所有情况一个式子总是恒为1,那么这个式子就是tautology。输出tautology。

    否则输出not。

    5个数,最多有2^5种情况。

    判断式子是不是恒为1,只需要从后往前判断即可。

    这题好长时间没看懂,代码也是看网上大神的

     1 #include<iostream>
     2  #include<cstring>
     3  #include<cstdio>
     4  #include<cstdlib>
     5  #include<algorithm>
     6  using namespace std;
     7  char str[110];
     8  int stack[110];
     9  
    10  int check()
    11  {
    12      int pp,qq,rr,ss,tt,n,i,top;
    13      n=strlen(str);
    14      for(pp = 0; pp < 2; pp++)
    15          for(qq = 0; qq < 2; qq++)
    16              for(rr = 0; rr < 2; rr++)
    17                  for(ss = 0; ss < 2; ss++)
    18                      for(tt = 0; tt < 2; tt++)
    19                      {
    20                          top = 0;
    21                          for(i = n-1; i >= 0; i--)
    22                          {
    23                              if(str[i]=='q') stack[top++]=qq;
    24                              if(str[i]=='p') stack[top++]=pp;
    25                              if(str[i]=='r') stack[top++]=rr;
    26                              if(str[i]=='t') stack[top++]=tt;
    27                              if(str[i]=='s') stack[top++]=ss;
    28                              if(str[i]=='K') top--,stack[top-1]=(stack[top-1]&&stack[top]);
    29                              if(str[i]=='A') top--,stack[top-1]=(stack[top-1]||stack[top]);
    30                              if(str[i]=='N') stack[top-1]=!stack[top-1];
    31                              if(str[i]=='C') top--,stack[top-1]=((!stack[top-1])||stack[top]);
    32                              if(str[i]=='E') top--,stack[top-1]=((stack[top-1])==stack[top]);
    33                          }
    34                          if(top!=1||stack[top-1]!=1)
    35                              return 0;
    36                      }
    37  
    38      return 1;
    39  };
    40  
    41  int main()
    42  {
    43      while(gets(str)&&str[0]!='0')
    44      {
    45          if(check())
    46              cout<<"tautology"<<endl;
    47          else
    48              cout<<"not"<<endl;
    49      }
    50      return 0;
    51  }
    52  
  • 相关阅读:
    zoj3299 Fall the Brick
    hdu4533 威威猫系列故事——晒被子
    FZU 1650 1752 a^b mod c
    Codeforces Round #136 (Div. 1) B. Little Elephant and Array
    Codeforces Round #292 (Div. 1) C. Drazil and Park
    Uva 12436 Rip Van Winkle's Code
    Codeforces Beta Round #19 D. Points
    hdu1513 Palindrome
    poj1160 Post Office
    zjnu1181 石子合并【基础算法・动态规划】——高级
  • 原文地址:https://www.cnblogs.com/bfshm/p/3225289.html
Copyright © 2011-2022 走看看