PQJ 1686(栈栈栈)

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[500],c2[500];
char s1[500],s2[500],s[500];
int a[100];
int juge(char c)                        //判断
{
    if(c>=‘0‘&&c<=‘9‘) return 1;
    if(c>=‘a‘&&c<=‘z‘) return 1;
    else return 0;
}
void in(char c[])                       //输入
{
    int i,j=0;
    stack<char> q;
    for(i=0;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=0;i<strlen(s1);i++)
    {
        if(juge(s1[i]))
        {
            if(s1[i]>=‘0‘&&s1[i]<=‘9‘)
                q.push(s1[i]-‘0‘);
            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[‘+‘]=1;
    a[‘-‘]=1;
    a[‘*‘]=2;
    a[‘(‘]=0;
    int i,j,t;
    cin>>t;
    getchar();
    while(t--){
        cin.getline(c1,500);
        cin.getline(c2,500);
        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 0;
}
时间: 2024-08-28 10:41:26

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

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

考题: 栈底至栈顶一次存放元素 ABCD 在第五个元素E入栈之前  栈中元素可以出栈,则出栈序列可能是_____a d___________. a.  ABCED b.  DBCEA   c.  CDABE   d.  DCBEA 分析: 1.假定进栈序列是从小到大排练的(即A<B<C<D<E),则出栈序列中不可能有  “大小中”这种序列,因为在“大数”出栈后,在栈中“中数”是在“小数”上面的,所以只能是先出“中数”再出“小数”2.出栈序列中如包含下列序列则是错误的:CAB,DAB

栈1--出栈序列

栈1--出栈序列 一.心得 二.题目及分析 进栈序列是123,求所有的出栈序列 用回溯法做 三.代码及结果 1 #include <iostream> 2 #include <stack> 3 using namespace std; 4 5 stack<int> sta; 6 int ans[4]; 7 int total=0; 8 9 void print(){ 10 total++; 11 cout<<total<<": &quo

压栈出栈遍历栈实例代码

#include<stdio.h> #include<stdlib.h> #include<malloc.h> typedef struct Node//定义一个链表结构体 { int data; struct Node* pNext; }NODE,*PNODE; typedef struct Stack//定义一个栈结构体 { PNODE pTop; PNODE pBottom; }STACK,*PSTACK; void initStack(PSTACK); void

栈及栈的链式存储结构(栈链)

栈:线性结构,后进先出.栈(Stack)是一种特殊的线性表(顺序表,链表)只在表尾进行删除和插入操作. 注意:对于栈来说,表尾称为栈的栈顶(top),表头称为栈底(bottom). 栈也是线性结构的一种特例.与队列不同,他只有一个口,只能从这里读或者写数据,这个口称为栈顶(top).栈是一种先进后出的数据结构.先进来的元素会放入栈底,而后进来的元素被放在它的上面,最后进来的元素的上面的位置,称为栈顶. 栈所提供的操作比一般的线性表要少很多,只提供:初始化.销毁.判断是否为空.求栈的长度.清空栈.

栈及栈的C++实现

栈:栈是一种数据结构,栈里元素的添加和删除只能在栈的末端进行.它是一种“后进先出”(LIFO)的数据结构. 栈的操作: initializeStack:初始化栈,使得为一个空栈. destroyStack:清空栈里所有的元素,使得为一个空栈. isEmptyStack:判断栈是否为空,如果为空,返回true,否则返回false. isFullStack  : 判断栈是否溢出,如果溢出,返回true,否则返回false. push : 添加一个新的元素到栈顶.前提是栈存在,且栈没有溢出. top

顺序栈——双栈(Dual Stack)

顺序栈--双栈(Dual Stack) 1. 双栈的概念 1.1 双栈的定义 双栈是指两个顺序栈,是一种特殊的顺序栈. 1.2 双栈中各元素的逻辑及存储关系 双栈共享一个地址连续的存储单元.即程序同时需要两个栈时,可以定义一个足够的栈空间,该空间的两端分别设为两个栈的栈底,用bottom[0]=-1和bottom[1]=maxSize指示. 压入数据时,让两个栈的栈顶top[0]和top[1]都向中间伸展,如果指示栈顶的指针top[0]+1等于另一个栈顶的指针top[1]时两栈已满. 每次进栈时

ACM/ICPC 之 用双向链表 or 模拟栈 解“栈混洗”问题-火车调度(Tshing Hua OJ - Train)

本篇用双向链表和模拟栈混洗过程两种解答方式具体解答“栈混洗”的应用问题 有关栈混洗的定义和解释在此篇:手记-栈与队列相关 列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In this station, A is the entrance for each train and B is the exit. S is the transfer end.

顺序栈的初始化入栈出栈以及打印栈的信息

使用的开发工具CLion CLion 2017.2.1 Build #CL-172.3544.40, built on August 2, 2017Licensed to CLion EvaluatorExpiration date: September 15, 2017JRE: 1.8.0_152-release-915-b6 x86_64JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.oMac OS X 10.12.4   1 #include

07.栈(一)栈的存储结构及操作

一.栈 1.栈(stack):是限定仅在表尾进行插入和删除操作的线性表.其中,允许插入和删除的一端被称为栈顶(top),另一端被称为栈底(bottom),不含任何数据元素的栈被称为空栈.栈又被称为后进先出(Last In First Out)的线性表,简称LIFO结构. 栈的插入操作为进栈,栈的删除操作为出栈. 2.栈的抽象数据类型 ADT 栈(stack) Data 同线性表.元素具有相同类型,相邻元素具有前驱和后继关系. Operation InitStack(*S):初始化操作,建立一个空