Problem K 栈
Description
You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.
Input
- Single letter variables (case insensitive).
- Single digit numbers.
- Matched left and right parentheses.
- Binary operators +, - and * which are used for addition, subtraction and multiplication respectively.
- Arbitrary number of blank or tab characters between above tokens.
Note: Expressions are syntactically correct and evaluated from left to right with equal precedence (priority) for all operators. The coefficients and exponents of the variables are guaranteed to fit in 16-bit integers.
Output
Sample Input
3
(a+b-c)*2
(a+a)+(b*2)-(3*c)+c
a*2-(a+c)+((a+c+e)*2)
3*a+c+(2*e)
(a-b)*(a-b)
(a*a)-(2*a*b)-(b*b)
Sample Output
YES
YES
NO
- #include <cstring>
- #include <string>
- #include <cstdio>
- #include <algorithm>
- #include <queue>
- #include <cmath>
- #include <vector>
- #include <cstdlib>
- #include <iostream>
- #include <stack>
- #include <map>
- #define max2(a,b) ((a) > (b) ? (a) : (b))
- #define min2(a,b) ((a) < (b) ? (a) : (b))
- using namespace std;
- map<char,int>m;
- string transform(string s) //转化为后缀表达式
- {
- int len=s.length();
- char c[100];
- int top=0;
- stack<char>exp;
- for(int i=0;i<len;i++)
- {
- if(isalnum(s[i])) c[top++]=s[i];
- else
- {
- switch(s[i])
- {
- case '(':
- exp.push(s[i]);
- break;
- case ')':
- while(exp.top()!='(')
- {
- c[top++]=exp.top();
- exp.pop();
- }
- exp.pop();
- break;
- case '+':
- case '-':
- case '*':
- while(!exp.empty()&&m[s[i]]<=m[exp.top()])
- {
- c[top++]=exp.top();
- exp.pop();
- }
- exp.push(s[i]);
- }
- }
- }
- while(!exp.empty())
- {
- c[top++]=exp.top();
- exp.pop();
- }
- c[top]='\0';
- string temp=c;
- return temp;
- }
- int cal(string s)
- {
- int len=s.length();
- stack<int>c;
- for(int i=0;i<len;i++)
- {
- if(isalnum(s[i]))
- {
- if(isdigit(s[i]))
- c.push(s[i]-'0');
- else
- c.push(s[i]);
- }
- else
- {
- int a=c.top();
- c.pop();
- int b=c.top();
- c.pop();
- switch(s[i])
- {
- case '+':c.push(b+a);
- break;
- case '-':c.push(b-a);
- break;
- case '*':c.push(b*a);
- }
- }
- }
- return c.top();
- }
- int main()
- {
- int t;
- string s1,s2;
- m['(']=0;
- m['+']=m['-']=1;
- m['*']=2;
- cin>>t;
- getchar();
- while(t--)
- {
- getline(cin,s1);
- getline(cin,s2);
- string t1=transform(s1);
- string t2=transform(s2);
- int ans1=cal(t1);
- int ans2=cal(t2);
- if(ans1==ans2)
- cout<<"YES"<<endl;
- else
- cout<<"NO"<<endl;
- }
- return 0;
- }
Problem K 栈的更多相关文章
- Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP
Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...
- Codeforces 1089K - King Kog's Reception - [线段树][2018-2019 ICPC, NEERC, Northern Eurasia Finals Problem K]
题目链接:https://codeforces.com/contest/1089/problem/K time limit per test: 2 seconds memory limit per t ...
- Gym 101981K - Kangaroo Puzzle - [玄学][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem K]
题目链接:http://codeforces.com/gym/101981/problem/K Your friend has made a computer video game called “K ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
Problem K. UTF-8 Decoder 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c702 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem K. KMC Attacks 交互题 暴力
Problem K. KMC Attacks 题目连接: http://codeforces.com/gym/100714 Description Warrant VI is a remote pla ...
- XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking
题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...
- 2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 200 ...
- HDU 6342.Problem K. Expression in Memories-模拟-巴科斯范式填充 (2018 Multi-University Training Contest 4 1011)
6342.Problem K. Expression in Memories 这个题就是把?变成其他的使得多项式成立并且没有前导零 官方题解: 没意思,好想咸鱼,直接贴一篇别人的博客,写的很好,比我的 ...
- 华农oj Problem K: 负2进制【有技巧构造/待补】
Problem K: 负2进制 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 51 Solved: 6 [Submit][Status][Web Boa ...
随机推荐
- ORACLE CentOS5.6安装
1 准备 CentOS 5.6企业版 oracle11g fs 安装.安装环境为vmware虚拟机.另外,本安装文档非常简洁,但关键步骤都指出来了,其他的都是默认选择,遇到不知该如何选择的操作或者问题 ...
- FATAL ha.BootstrapStandby: Unable to fetch namespace information from active NN at ***
This problem (Unable to fetch namespace information from active NN) occurs, because the active namen ...
- !!转!!java 简单工厂模式
举两个例子以快速明白Java中的简单工厂模式: 女娲抟土造人话说:“天地开辟,未有人民,女娲抟土为人.”女娲需要用土造出一个个的人,但在女娲造出人之前,人的概念只存在于女娲的思想里面.女娲造人,这就是 ...
- azure git 托管
azure git上传部署步骤:(首次提交)cd 至本地代码路径git initgit add .git commit –m "initial commit"git remote ...
- div 自适应高度 自动填充剩余高度
方案1: Html: <div class="outer"> <div class="A"> 头部DIV </div> &l ...
- 最火的Android开源项目整理
一.代码库 1.from 代码家 整理比较好的源码连接 ******************************************************************* ...
- Linux下修改系统时间并写入BIOS
我们一般使用“date -s”命令来修改系统时间.比如将系统时间设定成2005年7月26日的命令如下. #date -s 07/26/2005 将系统时间设定成下午11点12分0秒的命令如下. #da ...
- Java GC系列(3):垃圾回收器种类
本文由 ImportNew - 好好先生 翻译自 javapapers. 目录 垃圾回收介绍 垃圾回收是如何工作的? 垃圾回收的类别 垃圾回收监视和分析 在这篇教程中我们将学习几种现有的垃圾回收器.在 ...
- 联系 管理 Hibernate4+Spring JPA+SpringMVC+Volecity搭建web应用(三)
hibernate注解实体类示例 package cn.bdqn.smvc.entity; import java.io.Serializable; import javax.persistence. ...
- 2014---多校训练一(A Couple doubi)
Couple doubi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...