PQJ  1686(栈栈栈)

用栈解决问题

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

A math instructor is too lazy to grade a question in the exam papers in which students are supposed to produce a complicated formula for the question asked. Students may write correct answers in different forms which makes grading very hard. So, the instructor needs help from computer programmers and you can help.

You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.

Input

The first line of the input contains an integer N (1 <= N <= 20) that is the number of test cases. Following the first line, there are two lines for each test case. A test case consists of two arithmetic expressions, each on a separate line with at most 80 characters. There is no blank line in the input. An expression contains one or more of the following:

  • 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

Your program must produce one line for each test case. If input expressions for each test data are arithmetically equivalent, "YES", otherwise "NO" must be printed as the output of the program. Output should be all in upper-case characters.

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

题意:

给出两个数学式子判断其结果是否相等。
解法:
1,用栈将表达式转换成为后缀式,然后计算后缀表达式的只判断其是否相等。
2,字母转换之后算其值来代表其字母的值,直接将其ASCII作为数值对待,这个题只是判断两个表达式是否在数值上是等价的而不是判断两个公式是否等价,一直很疑惑,查了一下发现比如说:(b-a+c)*2 与 (1+c)*2也相等,但是如果作为公式的话这两个是不相等的.

3.从程序看出,要先判断字符(juge),然后依次输入(in),然后计算出(put),最后输出(out)结果。

4.注意格式,空格或tab!

经过一番搜寻加完善的AC代码:

#include<iostream>
#include<fstream>
#include<stack>
#include<cstring>
#include<map>
using namespace std;
char c1[],c2[];
char s1[],s2[],s[];
int a[];
int juge(char c) //判断
{
if(c>=''&&c<='') return ;
if(c>='a'&&c<='z') return ;
else return ;
}
void in(char c[]) //输入
{
int i,j=;
stack<char> q;
for(i=;i<strlen(c);i++)
{
if(juge(c[i]))
s[j++]=c[i];
else if(c[i]=='(')
q.push(c[i]);
else if(c[i]==')')
{
while(q.top()!='(')
{
s[j++]=q.top();
q.pop();
}
q.pop();
}
else
if(c[i]=='+'||c[i]=='-'||c[i]=='*')
{
while(!q.empty()&&a[c[i]]<=a[q.top()])
{
s[j++]=q.top();
q.pop();
}
q.push(c[i]);
}
}
while(!q.empty())
{
s[j++]=q.top();
q.pop();
}
s[j]='\0'; //特别注意,很容易遗漏
} int put(char s1[]) //计算出值
{
int i,j,k;
stack<int> q;
for(i=;i<strlen(s1);i++)
{
if(juge(s1[i]))
{
if(s1[i]>=''&&s1[i]<='')
q.push(s1[i]-'');
else
q.push(s1[i]);
}
else
{
j=q.top();
q.pop();
k=q.top();
q.pop();
if(s1[i]=='+')
j=j+k;
if(s1[i]=='-')
j=k-j;
if(s1[i]=='*')
j=k*j;
q.push(j);
}
}
return q.top(); }
void out() //输出比较
{
a['+']=;
a['-']=;
a['*']=;
a['(']=;
int i,j,t;
cin>>t;
getchar();
while(t--){
cin.getline(c1,);
cin.getline(c2,);
in(c1);
strcpy(s1,s);
in(c2);
strcpy(s2,s);
i=put(s1);
j=put(s2);
if(i==j) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
} int main()
{
out();
return ;
}

PQJ 1686(栈栈栈)的更多相关文章

  1. C++中栈的出栈,入栈规则:A,B,C,D,E

    考题: 栈底至栈顶一次存放元素 ABCD 在第五个元素E入栈之前  栈中元素可以出栈,则出栈序列可能是_____a d___________. a.  ABCED b.  DBCEA   c.  CD ...

  2. 出栈入栈动画demo

    项目做了一个切换界面动画的功能,用到了出栈入栈的,写了一个demo package com.myron.stackview; import java.util.Stack; import androi ...

  3. C语言实现链栈的初始化&进栈&出栈&读取栈顶元素

    /*链表实现栈的一系列操作*/ #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typede ...

  4. C语言实现顺序栈的初始化&进栈&出栈&读取栈顶元素

    /*顺序表实现栈的一系列操作*/ #include<stdio.h> #include<stdlib.h> #define Stack_Size 50 //设栈中元素个数为50 ...

  5. 剑指Offer 20. 包含min函数的栈 (栈)

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 题目地址 https://www.nowcoder.com/practice/4c77 ...

  6. 顺序栈,链栈,队列java实现

    顺序栈 /** * 顺序栈 * */ public class SqStack { //栈的大小 private int maxSize; //栈顶指针 private int top; privat ...

  7. BZOJ1146[CTSC2008]网络管理——出栈入栈序+树状数组套主席树

    题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个 部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条 ...

  8. BZOJ3772精神污染——可持久化线段树+出栈入栈序

    题目描述 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区,是日本西部门户 ...

  9. c++实验4 栈及栈的应用+回文+中、后缀表达式

    栈及栈的应用+回文+中.后缀表达式 1.栈顺序存储结构的基本操作算法实现 (1)栈顺序存储结构的类定义: class SeqStack { private: int maxsize; DataType ...

随机推荐

  1. 芭比娃娃 Barbara

    芭比娃娃(芭比)是20世纪最广为人知及最畅销的玩偶,由Ruth Handler发明,于1959年3月9日举办的美国国际玩具展览会(American International Toy Fair)上首次 ...

  2. 《A First Course in Mathematical Modeling》-chaper1-差分方程建模

    从今天开始笔者将通过这个专栏可是对“数学建模”的学习.其实对于“数学建模”自身的内涵或者意义并不需要太多的阐释,下图简洁明了的阐释了数学建模的意义. 其实数学建模本身可以看成换一种角度去解读数学,将我 ...

  3. 《A First Course in Probability》-chaper5-连续型随机变量-正态分布

    古典统计学问题一开始起源于赌博,让我们看这样一道有关赌博的问题. Q:A.B两人进行n局赌博,A胜的概率是p,现在设置随机变量X表示A赢的局数,当X>np,A给赌场X-np元,否则B给赌场np- ...

  4. POJ 3280 Cheapest Palindrome(水题)

    题意:给定一个完全由小写字母组成的字符串s,对每个字母比如x(或a,b,c...z),在字符串中添加或者删除它分别需要花费c1['x']和c2['x']的代价,问将给定字符串变成回文串所需要的最少代价 ...

  5. JavaScript中bind、call、apply函数用法详解

    在给我们项目组的其他程序介绍 js 的时候,我准备了很多的内容,但看起来效果不大,果然光讲还是不行的,必须动手.前几天有人问我关于代码里 call() 函数的用法,我让他去看书,这里推荐用js 写服务 ...

  6. EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询

    场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join).外连接(left/right oute ...

  7. 技术型创业者easy遇到的三大问题

    关于创业这事儿.由于经历的事情实在是太多了,所以真是想到哪儿写到哪儿. 这一篇算是<杂记>的番外篇.我今天想说的东西太多了,实在是非常想和很多其它人讨论讨论关于创业的事情. 这样的感觉就像 ...

  8. [AngularJS] ng-if vs ng-show

    ng-show: ng-show element will stay in dom, just added a ng-hide attr, so it won't show. ng-if: It ha ...

  9. 在 foreach 里使用引用要注意的陷阱(转)

    从一道面试题开始 在开始本节内容前,我们先来看看一道还算比较常见的PHP面试题: 1 $arr = array('1','2','3'); 2   3 foreach($arr as &$v) ...

  10. Linux Kernel: buffers和cached的区别

    The page cache caches pages of files to optimize file I/O. The buffer cache caches disk blocks to op ...