poj 2106

原题链接:http://poj.org/problem?id=2106

题意:或、与、 非的多元表达式的求值;

思路:中缀表达式变为后缀表达式;

代码:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<stack>
  4 #include<string>
  5
  6 using namespace std;
  7
  8 struct Pri{
  9     char op;
 10     int pri;
 11 }lpri[5]={{‘(‘,1},{‘|‘,3},{‘&‘,5},{‘!‘,6},{‘)‘,8}},
 12  rpri[5]={{‘(‘,8},{‘|‘,2},{‘&‘,4},{‘!‘,7},{‘)‘,1}};
 13
 14  int find_left(char op)
 15  {
 16      for(int i=0;i<5;i++)
 17          if(lpri[i].op==op)
 18              return lpri[i].pri;
 19  }
 20
 21  int find_right(char op)
 22  {
 23      for(int i=0;i<5;i++)
 24          if(rpri[i].op==op)
 25              return rpri[i].pri;
 26  }
 27
 28 int Precede(char op1,char op2)
 29 {
 30     int L=find_left(op1);
 31     int R=find_right(op2);
 32     if(L==R)
 33         return 0;
 34     if(L>R)
 35         return 1;
 36     return -1;
 37 }
 38
 39 int Run(stack<int>& num,char op)
 40 {
 41     int a=num.top();
 42     num.pop();
 43
 44     if(op==‘!‘)
 45         return !a;
 46     int b=num.top();
 47     num.pop();
 48     switch(op)
 49     {
 50         case ‘|‘:
 51             return a||b;
 52         case ‘&‘:
 53             return a&&b;
 54     }
 55 }
 56
 57 int main()
 58 {
 59     string s;
 60     int ca=1;
 61     while(getline(cin,s))
 62     {
 63         int i;
 64         stack<int> num;
 65         stack<char> op;
 66          for(i=0;i<s.length();i++)
 67         {
 68             if(s[i]==‘ ‘)
 69                 continue;
 70             else if(s[i]==‘V‘)
 71                 num.push(1);
 72             else if(s[i]==‘F‘)
 73                 num.push(0);
 74             else
 75             {
 76                 if(op.empty())
 77                 {
 78                     op.push(s[i]);
 79                 }
 80                 else
 81                 {
 82                     int judg=Precede(op.top(),s[i]);
 83                     int t;
 84                     switch(judg)
 85                     {
 86                         case 0:
 87                             op.pop();break;
 88                         case 1:
 89                             t=Run(num,op.top());
 90                             num.push(t);
 91                             op.pop();
 92                             i--;break;
 93                         case -1:
 94                             op.push(s[i]);break;
 95                     }
 96                 }
 97             }
 98         }
 99         while(!op.empty())
100         {
101             num.push(Run(num,op.top()));
102                 op.pop();
103         }
104         if(num.top()==1)
105             printf("Expression %d: V\n",ca++);
106         else
107             printf("Expression %d: F\n",ca++);
108     }
109
110     return 0;
111 }

注意:特殊数据:!!!!!!!!!!!!!!!F

ac与RE只在一念只差

-------------------------------------------欢迎评论提问-------------------------------------------

时间: 2024-10-29 04:06:20

poj 2106的相关文章

poj 2106 Boolean Expressions 课本代码

#include<cstdio> const int maxn=100 +10; int val[maxn],vtop; int op[maxn],otop; void insert(int b) { while(otop &&op[otop-1]==3) { b=!b; --otop; } val[vtop++]=b; } void calc(void) { int b=val[--vtop]; int a=val[--vtop]; int opr=op[--otop]; i

[poj 2106] Boolean Expressions 递归

Description The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next: Expression: ( V | V ) & F & ( F | V )where V is for True, and F is for False. The expressions may include the following ope

POJ 2106 Boolean Expression 表达式求值

题意:给出布尔表达式求值? 插入数字时,若有!则更新.遇到右括号弹出知道左括号,左括号前有'!'则更新, 其余和中缀表达式一样,遇到下一个运算符时 若操作栈中运算符优先级大,则先算. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <s

POJ 2106 Boolean Expressions

Boolean Expressions Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3665   Accepted: 1104 Description The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next: Expression: ( V | V ) &

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is