zoukankan      html  css  js  c++  java
  • poj3295 Tautology

    哈哈,这个题目比较有意思。

    一开始丝毫没有头绪,后来我想用分治的想法行不行呢,切割,然后函数返回该段的结果和结束位置,还很惆怅怎么存储的问题,然后我去洗头寻思出去溜达溜达,突然想起来我可以用结构体返回啊,这样就很简洁了,哈哈,这样什么都有了思路也很清晰,函数代码还真的不长,哈哈。

    后来一个劲的RE,才发现有1个元素的情况,再交,这回是WA了!通过检查这是因为我讨论N的时候忘记了一种情况。。。

    不过这题写出来还是很有成就感的。

    ××××××××××××××××××××××××××××××××××××××××××××××××

    总体思路就是 分治 + 递归

    p,q,r,s,t的值需要在iff中通过遍历来初始化。然后调用haha()

    struct res haha( int begin )   // tmp[begin]  ------>  5种操作的一种

    函数的功能是返回一种操作的结果和该操作影响的位置。我在代码里面写的已经很明晰了。

    在求得该操作对象的值时候有两种情况,一种是该值是p,q,r,s,t中的一个,另一种是该值是5种操作得出的结果,这种情况就要递归调用haha。

    代码还是比较清晰的!

    ××××××××××××××××××××××××××××××××××××××××××××××××

      1 #include <stdio.h>
      2 #include <string.h>
      3 char tmp[1000000+10];
      4 int p,q,r,s,t;
      5 int l;
      6 struct res{
      7     int pos;
      8     int res;
      9 };
     10 struct res haha(int begin){
     11     char SIGN=tmp[begin];
     12     struct res rr;
     13     int a,b;   /*   操作的值  */
     14     int pos;
     15     struct res tt;
     16     if(SIGN=='N'){
     17         pos=begin+1;
     18         /* 
     19          * 找N操作的a值
     20          *
     21          * 其他操作寻值跟这个一样
     22          *
     23          * 
     24          */
     25         /* 情况 1 */
     26         if(tmp[pos]=='0'||tmp[pos]=='1'){
     27             a=tmp[pos]-'0';
     28             pos=pos;
     29         }else{
     30         /* 情况 2 */
     31             tt=haha(pos);
     32             a=tt.res;
     33             pos=tt.pos;
     34         }
     35         if(a==0) rr.res=1;
     36         else     rr.res=0;
     37         rr.pos=pos;
     38         return rr;
     39     }
     40     else{
     41         /*
     42          * 这里寻值跟上面不同的是,这个要寻找两个值,a,b
     43          *
     44          * 第一次寻找a,然后用得到的信息开始寻找b
     45          *
     46          * 思路是一样的 
     47          *
     48          */
     49         pos=begin+1;
     50         if(tmp[pos]=='0'||tmp[pos]=='1'){
     51             a=tmp[pos]-'0';
     52             pos=pos+1;
     53         }else{
     54             tt=haha(pos);
     55             a=tt.res;
     56             pos=tt.pos+1;
     57         }
     58         if(tmp[pos]=='0'||tmp[pos]=='1'){
     59             b=tmp[pos]-'0';
     60         }else{
     61             tt=haha(pos);
     62             b=tt.res;
     63             pos=tt.pos;
     64         }
     65     }
     66     rr.pos=pos;
     67     
     68     if(SIGN=='K'){
     69         if(a*b>0)
     70             rr.res=1;
     71         else rr.res=0;
     72     }else if(SIGN=='A'){
     73         if(a+b>0) rr.res=1;
     74         else      rr.res=0;
     75     }else if(SIGN=='C'){
     76         if(a<=b) rr.res=1;
     77         else     rr.res=0;
     78     }else if(SIGN=='E'){
     79         if(a==b) rr.res=1;
     80         else     rr.res=0;
     81     }
     82     return rr;
     83 
     84 }
     85 
     86 int iff(char str[]){
     87     l=strlen(str);
     88     int i;
     89     struct res sum;
     90     for(p=0;p<=1;++p)
     91     for(q=0;q<=1;++q)
     92     for(r=0;r<=1;++r)
     93     for(s=0;s<=1;++s)
     94     for(t=0;t<=1;++t){
     95         strcpy(tmp,str);
     96         for(i=0;i<l;++i){
     97             if(tmp[i]=='p') tmp[i]='0'+p;
     98             else if(tmp[i]=='q') tmp[i]='0'+q;
     99             else if(tmp[i]=='r') tmp[i]='0'+r;
    100             else if(tmp[i]=='s') tmp[i]='0'+s;
    101             else if(tmp[i]=='t') tmp[i]='0'+t;
    102         }
    103         sum=haha(0);
    104         if(sum.res==0) return 0;
    105     }
    106     return 1;
    107 }
    108 int main(){
    109     char str[1000000+10];
    110     int rres;
    111     while(~scanf("%s",str)){
    112         if(str[0]=='0') break;
    113         if(strlen(str)==1){ printf("not
    ");continue;}
    114         rres=iff(str);
    115         if(rres==1) printf("tautology
    ");
    116         else       printf("not
    ");
    117     }
    118     return 0;
    119 }
  • 相关阅读:
    Java生鲜电商平台-物流配送的设计与架构
    五分钟学Java:如何学习Java面试必考的网络编程
    五分钟学Java:如何学习Java面试必考的网络编程
    Java原来还可以这么学:如何搞定面试中必考的集合类
    五分钟学Java:如何学习Java面试必考的JVM虚拟机
    先搞清楚这些问题,简历上再写你熟悉Java!
    MySql/Oracle和SQL Server的分页查
    Java面试题之int和Integer的区别
    Java基本数据类型转换
    Shiro-Subject 分析
  • 原文地址:https://www.cnblogs.com/symons1992/p/3554792.html
Copyright © 2011-2022 走看看