標籤:
主題串連:http://codevs.cn/problem/1107/
一道非常奇妙的題目。
對於算術運算式一類的問題,能夠採用編譯原理裡的尾碼運算式的方式來做。詳細做法是分別維護兩個棧,一個棧裡儲存運算式裡的數字,還有一個棧裡儲存運算式裡的運算子,給每種運算子一個優先順序,我們要維護這個棧的單調性,每次讀入運算子中的數字或運算子,讀入的是運算子時,若這個運算子比棧頂的運算子優先順序低,就彈出棧頂元素。把棧頂的運算子和數字棧裡棧頂的兩個數字拿出來做一次運算,運算結果再入數字棧。直到運算子棧的棧頂元素優先順序比這個運算子低為止。
然後題目有坑點,一是讀入的運算式字串可能有空格,所以不能直接scanf一次搞定讀入資料操作。二是推斷運算式是否等價時,帶入的值假設不好可能會WA,所以為了避免這樣的情況的發生,我們代入的數字應該是個小數,用三態函數推斷運算式結果是否相等,多代入幾個小數計算。基本上不可能出現意外WA的發生。
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#include <map>#include <cmath>#define MAXN 1000#define EPS (1e-5)using namespace std;long long stackOfNum[MAXN];int topNum=0; //儲存數位棧和棧頂下標char stackOfSign[MAXN];int topSign=0; //儲存運算子號的棧和棧頂下標bool needPop[50][50]; //needPop[i][j]=true表示當前運算子為i,棧頂運算子為j時須要出棧bool isTrue[30];int dcmp(long long a,long long b) //a>b return 1; a=b return 0; a<b return -1{ if(fabs(a-b)<=EPS) return 0; if(a>b) return 1; return -1;}long long cal(long long a,long long b,char cmd){ switch(cmd) { case '^': { long long ans=1; for(int i=1;i<=(int)b;i++) ans*=a; return ans; } case '+': return a+b; case '-': return a-b; case '*': return a*b; case '/': return a/b; } return 0;}long long getAns(char s[],int len,long long a) //將運算式的值求出來,len=運算式長度,a=字母a相應的值{ int p=1; //指標指向當前的運算式下標 topNum=0; topSign=0; while(p<=len) {while(s[p]==' ') p++;if(p>len) break; if(s[p]>='0'&&s[p]<='9') //是數字 { int nowNum=0; while(p<=len) { if(!(s[p]>='0'&&s[p]<='9')) //如今的s[p]不是數字了 break; nowNum*=10; nowNum+=s[p]-'0'; p++; } stackOfNum[++topNum]=nowNum; //這個數字進棧 continue; } else if(s[p]=='a') stackOfNum[++topNum]=a; //假設是a,將a相應的數字壓入棧 else //s[p]是個運算子,將棧中全部比它優先順序 { while(topSign>0&&topNum>0) { if(needPop[s[p]][stackOfSign[topSign]]) { if(stackOfSign[topSign]=='(') //右括弧遇到左括弧 { topSign--; break; } stackOfNum[topNum-1]=cal(stackOfNum[topNum-1],stackOfNum[topNum],stackOfSign[topSign]); topNum--; topSign--; } else break; } if(s[p]!=')') stackOfSign[++topSign]=s[p]; } p++; } while(topSign>0&&topNum>1) { stackOfNum[topNum-1]=cal(stackOfNum[topNum-1],stackOfNum[topNum],stackOfSign[topSign]); topNum--; topSign--; } return stackOfNum[topNum];}int main(){ memset(isTrue,true,sizeof(isTrue)); //先打個巨表~! needPop['^']['^']=true; needPop['^']['+']=false; needPop['^']['-']=false; needPop['^']['*']=false; needPop['^']['/']=false; needPop['^']['(']=false; //---------------------- needPop['+']['^']=true; needPop['+']['+']=true; needPop['+']['-']=true; needPop['+']['*']=true; needPop['+']['/']=true; needPop['+']['(']=false; //---------------------- needPop['-']['^']=true; needPop['-']['+']=true; needPop['-']['-']=true; needPop['-']['*']=true; needPop['-']['/']=true; needPop['-']['(']=false; //---------------------- needPop['*']['^']=true; needPop['*']['+']=false; needPop['*']['-']=false; needPop['*']['*']=true; needPop['*']['/']=true; needPop['*']['(']=false; //---------------------- needPop['/']['^']=true; needPop['/']['+']=false; needPop['/']['-']=false; needPop['/']['*']=true; needPop['/']['/']=true; needPop['/']['(']=false; //---------------------- needPop['(']['^']=false; needPop['(']['+']=false; needPop['(']['-']=false; needPop['(']['*']=false; needPop['(']['/']=false; needPop['(']['(']=false; //---------------------- needPop[')']['^']=true; needPop[')']['+']=true; needPop[')']['-']=true; needPop[')']['*']=true; needPop[')']['/']=true; needPop[')']['(']=true; char s[MAXN]; int n; long long trueAns1,trueAns2,nowAns1,nowAns2; //trueAns=帶入a值後應該得到的答案,nowAns=選擇選項中帶入a值得到的答案 //scanf("%s",s+1);gets(s+1); trueAns1=getAns(s,strlen(s+1),1.4); trueAns2=getAns(s,strlen(s+1),2.8); scanf("%d",&n);gets(s+1); for(int i=0;i<n;i++) { //scanf("%s",s+1);gets(s+1);nowAns1=getAns(s,strlen(s+1),1.4);nowAns2=getAns(s,strlen(s+1),2.8); if(dcmp(trueAns1,nowAns1)!=0) //trueans==nowans isTrue[i]=false; if(dcmp(trueAns2,nowAns2)!=0) //trueans==nowans isTrue[i]=false; } for(int i=0;i<n;i++) if(isTrue[i]) printf("%c",'A'+i); printf("\n"); return 0;}
[Codevs 1107][NOIP 1107]等效表達