[百練1686]等價表示式(棧的應用)
阿新 • • 發佈:2019-02-11
題目描述:
- 總時間限制:
- 1000ms
- 記憶體限制:
- 65536kB
- 描述
- 判斷兩個表示式在數學上是否是等價的。
- 輸入
- 第一行:N(1<=N<=20),表示測試資料組數。
接下來每組測試資料包括兩行,每行包括一個數學表示式,每個表示式的長度不超過80個字元。輸入資料沒有空行。
一個表示式可能包括:
單個英文字母表示的變數(區分大小寫)
數字(只有一位數)
配對的括號
運算子加+、減-、乘*
任意數量的空格或tab(可能出現在表示式中間的任何位置)
注意:表示式保證是語法正確的,且所有運算子的優先順序相同,運算次序從左至右。變數的係數和指數保證不超過16位整數。 - 輸出
- 對每個測試資料,輸出一行:等價則輸出“YES”,不等價則輸出“NO”。
- 樣例輸入
-
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)
- 樣例輸出
-
YES YES NO
兩個代數式值不相同則一定為“NO”
否則可能為“YES”
這裡我們預設都是“YES”至少是可以AC的,如果有其他方法歡迎指出
我們的任務:1.把中綴表示式轉換成字尾表示式。2.計算字尾表示式的代數值。
中綴表示式轉換成字尾表示式方法:
遍歷中綴表示式。
1如果訪問到字母或數字就直接放入字尾表示式中。
2.如果訪問到括號:1左括號:直接入棧。2.右括號:重複執行把當前棧頂元素放入字尾表示式中,並彈棧,直到棧頂元素為左括號。然後把左括號彈棧。
3.如果訪問到運算子。如果棧非空並且棧頂元素不是左括號,且當前運算子優先順序小於等於棧頂運算子,則把棧頂元素放入字尾表示式,彈棧,重複以上操作。當前述條件不滿足,則把當前運算子入棧。
4.如果訪問到空格或者tab,continue就好,不用管。
5.訪問字串結束,彈棧至棧為空,然後把彈出的元素放入字尾表示式
用程式碼描述為:
string change(char *p) { string cnt; int len=strlen(p); stack<char>astack; for(int i=0; i<=len; i++) { if((p[i]>='0' && p[i]<='9') || (p[i]>='a' && p[i]<='z')) cnt+=p[i]; else if(p[i]=='(') astack.push('('); else if(p[i]==')') { while(astack.top()!='(') { cnt+=astack.top(); astack.pop(); } astack.pop(); } else if(p[i]=='+' || p[i]=='-') { while(!astack.empty() && astack.top()!='(') { cnt+=astack.top(); astack.pop(); } astack.push(p[i]); } else if(p[i]=='*') { while(!astack.empty() && astack.top()!='(' && astack.top()=='*') { cnt+=astack.top(); astack.pop(); } astack.push(p[i]); } else if(p[i]==' ' || p[i]==' ') continue; } while(!astack.empty()) { cnt+=astack.top(); astack.pop(); } return cnt; }
以上,返回的cnt字串就是由中綴表示式字串p轉換而來的字尾字串
計算字尾表示式代數值:
遍歷字尾表示式,如果訪問到數字,把數字入棧。如果訪問到字母,把字母的ASCII碼入棧。如果訪問到運算子OP,從棧中依次彈出兩次棧頂元素m,n,n=n OP m;然後把n入棧。遍歷結束後,返回棧頂元素。
程式碼為:
int getsum(string num) {
int len=num.length();
stack<int>astack;
for(int i=0; i<len; i++) {
if(num[i]<='z' && num[i]>='a') {
int ascii=num[i];
astack.push(ascii);
}
else if(num[i]>='0' && num[i]<='9')
astack.push((int)num[i]-'0');
else if(num[i]=='+') {
int n=astack.top();
astack.pop();
int m=astack.top();
astack.pop();
m+=n;
astack.push(m);
}
else if(num[i]=='*') {
int n=astack.top();
astack.pop();
int m=astack.top();
astack.pop();
n*=m;
astack.push(n);
}
else if(num[i]=='-') {
int n=astack.top();
astack.pop();
int m=astack.top();
astack.pop();
m-=n;
astack.push(m);
}
}
int result=astack.top();
astack.pop();
return result;
}
彙總程式碼為
#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <stack>
using namespace std;
#define MAX 100
string change(char *p) {
string cnt;
int len=strlen(p);
stack<char>astack;
for(int i=0; i<=len; i++) {
if((p[i]>='0' && p[i]<='9') || (p[i]>='a' && p[i]<='z'))
cnt+=p[i];
else if(p[i]=='(')
astack.push('(');
else if(p[i]==')') {
while(astack.top()!='(') {
cnt+=astack.top();
astack.pop();
}
astack.pop();
}
else if(p[i]=='+' || p[i]=='-') {
while(!astack.empty() && astack.top()!='(') {
cnt+=astack.top();
astack.pop();
}
astack.push(p[i]);
}
else if(p[i]=='*') {
while(!astack.empty() && astack.top()!='(' && astack.top()=='*') {
cnt+=astack.top();
astack.pop();
}
astack.push(p[i]);
}
else if(p[i]==' ' || p[i]==' ')
continue;
}
while(!astack.empty()) {
cnt+=astack.top();
astack.pop();
}
return cnt;
}
int getsum(string num) {
int len=num.length();
stack<int>astack;
for(int i=0; i<len; i++) {
if(num[i]<='z' && num[i]>='a') {
int ascii=num[i];
astack.push(ascii);
}
else if(num[i]>='0' && num[i]<='9')
astack.push((int)num[i]-'0');
else if(num[i]=='+') {
int n=astack.top();
astack.pop();
int m=astack.top();
astack.pop();
m+=n;
astack.push(m);
}
else if(num[i]=='*') {
int n=astack.top();
astack.pop();
int m=astack.top();
astack.pop();
n*=m;
astack.push(n);
}
else if(num[i]=='-') {
int n=astack.top();
astack.pop();
int m=astack.top();
astack.pop();
m-=n;
astack.push(m);
}
}
int result=astack.top();
astack.pop();
return result;
}
int main() {
int N;
scanf("%d", &N);
cin.get();
while(N--) {
char s1[MAX], s2[MAX];
cin.getline(s1, MAX);
string p1=change(s1);
int a=getsum(p1);
cin.getline(s2, MAX);
string p2=change(s2);
int b=getsum(p2);
if(a==b)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
歡迎批評指正
最後大小128kb,時間1ms