等价表达式

等价表达式

栈的经典题目,开两个栈,一个存符号,一个存数字;

分情况讨论:

1.如果当前读到的运算符优先级小于栈顶,就进行一次运算,直到大于等于;

2.如果读到数字用类似读入优化的方法读入进来;

3.如果当前符号为“(”则直接入栈;

4.如果当前符号为“)”则进行运算直到碰到“(”;

5.小技巧 在式子开头加“(”,末尾加“)”;

6.遇到a,让a等于质数;

7.多次取模,因为有可能爆long long,模不同的质数,避免冲突

最后,然而我并没A掉,在codevs上测的60分,WA4个点;

洛谷上全TLE........ 也许洛谷跟STL有仇吧,不管我事了

//等价表达式
#include<bits/stdc++.h>
#define MOD 99824435
#define M 991
using namespace std;
char a[51];
stack<int>sign;
stack<int>num;
int n;
int len;
long long ans;

void read()
{
char c=getchar();
int ll=0,rr=0;
while(c==' ')c=getchar();
while(c!=' ')
{
if(c!=' ')
a[++len]=c;
c=getchar();
}
a[0]='(';
a[++len]=')';
}

int POW(int x,int t)
{
if(t==0)
return 1;
if(t==1||x==0||x==1)
return x;
while(t%2==0)
{
x=(x*x)%MOD;
t>>=1;
}
int result=1;
while(t>0)
{
if(t%2==1)
{
result*=x;
t--;
result%=MOD;
}
else
{
x*=x;
x%=MOD;
t>>=1;
}
}
return result;
}

void work(int x)
{
while(sign.top()>=x)
{
if(sign.top()==2)
{
int t=num.top();
num.pop();
t=-t;
sign.pop();
t+=num.top();
num.pop();
num.push(t);
}
else
if(sign.top()==1)
{

int t=num.top();
num.pop();
sign.pop();
t+=num.top();
num.pop();
num.push(t);
}
else
if(sign.top()==4)
{
int t=num.top();
num.pop();
sign.pop();
t*=num.top();
num.pop();
num.push(t);
}
else
if(sign.top()==5)
{
int t=num.top();
num.pop();
sign.pop();
t=POW(num.top(),t)%MOD;
num.pop();
num.push(t);
}
}
sign.push(x);
}

long long deal()
{
int l=0;
while(l<=len)
{
if(a[l]=='+')
{
work(1);
l++;
}
else
if(a[l]=='-')
{
work(2);
l++;
}
else
if(a[l]=='*')
{
work(4);
l++;
}
else
if(a[l]=='^')
{
work(5);
l++;
}
else
if(a[l]=='(')
{
sign.push(-1);
l++;
}
else
if(a[l]==')')
{
l++;
while(sign.top()!=-1)
{
if(sign.top()==2)
{
int t=num.top();
num.pop();
t=-t;
sign.pop();
t+=num.top();
num.pop();
num.push(t);
}
else
if(sign.top()==1)
{
int t=num.top();
num.pop();
sign.pop();
t+=num.top();
num.pop();
num.push(t);
}
else
if(sign.top()==4)
{
int t=num.top();
num.pop();
sign.pop();
t*=num.top();
num.pop();
num.push(t);
}
else
if(sign.top()==5)
{
int t=num.top();
num.pop();
sign.pop();
t=POW(num.top(),t)%MOD;
num.pop();
num.push(t);
}
}
sign.pop();
}
else
if(a[l]=='a')
{
num.push(1609);
l++;
}
else
{
int t=0;
while(a[l]>='0'&&a[l]<='9')
{
t=t*10+a[l]-'0';
l++;
}
num.push(t);
}
}
return num.top()%MOD;
}

int cc[100];
int cnt;

int ANS;

int main()
{
read();
ans=deal();
ans%=MOD;
ANS=ans;
ANS%=M;
cin>>n;
int t=1;
while(t<=n)
{
len=0;
read();
int tt=deal();
int TT=tt;
tt%=MOD;
TT%=M;
if(ans==tt&&ANS==TT)
{
cc[++cnt]=64+t;
}
t++;
}
for(int i=1;i<=cnt;i++)
{
cout<<(char)cc[i];
}
return 0;
}

原文地址:https://www.cnblogs.com/war1111/p/7289316.html