- 等价表达式
- 2014-12-14 12:55:23 @
###inline Code
可代入几个数求表达式的值.
###Block code
#include<cstdio>
#include<cstring>
#include<cctype>
#include<string>
#include<stack>
#include<cmath>
#include<iostream>
#define oo 2147483647
using namespace std;
long long power(long long a, long long b)
{
long long ans = 1;
for(int i = 0; i < b; i++)
ans = (ans*a);
return ans;
}
bool can(char top, char ss)
{
bool flag = true;
switch(top)
{
case '+':
case '-':
switch(ss)
{
case '*':
case '^':
flag = false;
break;
}
case '*':
switch(ss)
{
case '^':
flag = false;
break;
}
break;
}
return flag;
}
string turn(string dan)
{
string gan;
// stack<char> temp;
int top=0;
char temp[257];
for(int i = 0; i < dan.size(); i++)
{
if(dan[i] == ' ')
continue;
if(dan[i] == '(')
// temp.push(dan[i]);
temp[++top]=dan[i];
else if(dan[i] == ')')
{
// while(!temp.empty() && temp.top() != '(')
while(top>0&&temp[top]!='(')
{
// gan += temp.top();
gan += temp[top];
// temp.pop();
top--;
}
// temp.pop();
top--;
}
else if(dan[i] == '+' || dan[i] == '-' || dan[i] == '*' || dan[i] == '^')
{
// while(!temp.empty())
while(top>0)
{
// if(temp.top()=='('||!can(temp.top(),dan[i]))
if(temp[top]=='('||!can(temp[top],dan[i]))
break;
gan += temp[top];
// gan += temp.top();
// temp.pop();
top--;
}
// temp.push(dan[i]);
temp[++top]=dan[i];
}
else
{
if(dan[i]>='0' && dan[i] <='9')
{
while(i < dan.size() && dan[i]>='0' &&dan[i]<='9')
{
gan += dan[i];
i++;
}
i--;
gan += "#";
}
else
gan += dan[i];
}
}
// while(!temp.empty())
while(top>0)
{
// gan += temp.top();
gan += temp[top];
// temp.pop();
top--;
}
return gan;
}
long long rand_me(string gan, int num)
{
int len = gan.length();
stack<long long> ans;
for(int i = 0; i < len; i++)
{
if(gan[i] == 'a')
ans.push(num);
else if(isdigit(gan[i]))
{
long long tmp = 0;
while(i < len && gan[i]>='0' && gan[i]<='9')
{
tmp = tmp*10 + (gan[i] - '0');
i++;
}
i--;
ans.push(tmp);
}
else if(gan[i] == '+')
{
long long tmp1 = ans.top();
ans.pop();
long long tmp2 = ans.top();
ans.pop();
ans.push((tmp1 + tmp2)%oo);
}
else if(gan[i] == '-')
{
long long tmp1 = ans.top();
ans.pop();
long long tmp2 = ans.top();
ans.pop();
ans.push(tmp2 - tmp1);
}
else if(gan[i] == '*')
{
long long tmp1 = ans.top();
ans.pop();
long long tmp2 = ans.top();
ans.pop();
ans.push((tmp1%oo * tmp2)%oo);
}
else if(gan[i] == '^')
{
long long tmp1 = ans.top();
ans.pop();
long long tmp2 = ans.top();
ans.pop();
ans.push(power(tmp2, tmp1)%oo);
}
}
return ans.top();
}
int main()
{
#ifndef DEBUG
freopen("equal.in","r",stdin);
freopen("equal.out","w",stdout);
#endif
string dan;
string gan;
getline(cin, dan);
gan = turn(dan);
string ss;
int n;
cin >> n;
getchar();
for(int i=0;i<n;i++)
{
getline(cin,ss);
string output = turn(ss);
int j;
for(j=0;j<=10;j++)
{
long long temp1 = rand_me(gan, j);
long long temp2 = rand_me(output, j);
// printf("temp1:%d temp2:%d\n",temp1,temp2);
if(temp1 != temp2)
break;
}
if(j > 10)
printf("%c", 'A'+i);
// printf("---------------------------------------------------------------\n");
}
printf("\n");
return 0;
}
1 条评论
-
方晨羽 LV 9 @ 2016-08-13 15:14:28
编的不太好
- 1