Advertisement
nullzero

Equivalence

Aug 21st, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <cstdio>
  2. #include <stack>
  3. #include <string>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7. typedef long long ll;
  8.  
  9. const int N(100);
  10. const ll MOD(9999991);
  11.  
  12. char A[N], B[N];
  13. int val[128];
  14.  
  15. string postorder(string s){
  16.     string res;
  17.     stack<int> S;
  18.     for(int i = 0; i < s.size(); ++i){
  19.         if(s[i] == '*'){
  20.             while(not S.empty() and S.top() == '*'){
  21.                 res += S.top();
  22.                 S.pop();
  23.             }
  24.             S.push(s[i]);
  25.         }else if(s[i] == '+' or s[i] == '-'){
  26.             while(not S.empty() and (S.top() == '+' or S.top() == '-' or S.top() == '*')){
  27.                 res += S.top();
  28.                 S.pop();
  29.             }
  30.             S.push(s[i]);
  31.         }else if(s[i] == '('){
  32.             S.push('(');
  33.         }else if(s[i] == ')'){
  34.             while(S.top() != '('){
  35.                 res += S.top();
  36.                 S.pop();
  37.             }
  38.             S.pop();
  39.         }else{
  40.             res += s[i];
  41.         }
  42.     }
  43.     while(not S.empty()){
  44.         res += S.top();
  45.         S.pop();
  46.     }
  47.     return res;
  48. }
  49.  
  50. ll eval(const string& s){
  51.     stack<ll> S;
  52.     for(int i = 0; i < s.size(); ++i){
  53.         if(s[i] == '+'){
  54.             ll b = S.top();
  55.             S.pop();
  56.             ll a = S.top();
  57.             S.pop();
  58.             S.push((a + b) % MOD);
  59.         }else if(s[i] == '-'){
  60.             ll b = S.top();
  61.             S.pop();
  62.             ll a = S.top();
  63.             S.pop();
  64.             S.push((a - b + MOD) % MOD);
  65.         }else if(s[i] == '*'){
  66.             ll b = S.top();
  67.             S.pop();
  68.             ll a = S.top();
  69.             S.pop();
  70.             S.push((a * b) % MOD);
  71.         }else if('0' <= s[i] and s[i] <= '9'){
  72.             S.push(s[i] - '0');
  73.         }else{
  74.             S.push(val[s[i]]);
  75.         }
  76.     }
  77.     return S.top() % MOD;
  78. }
  79.  
  80. string trim(string s){
  81.     string t;
  82.     for(int i = 0; i < s.size(); ++i){
  83.         if(s[i] == '+' or s[i] == '-' or s[i] == '*' or s[i] == '(' or s[i] == ')')
  84.             t += s[i];
  85.         else if('A' <= s[i] and s[i] <= 'Z')
  86.             t += s[i];
  87.         else if('a' <= s[i] and s[i] <= 'z')
  88.             t += s[i];
  89.         else if('0' <= s[i] and s[i] <= '9')
  90.             t += s[i];
  91.     }
  92.     return t;
  93. }
  94.  
  95. void prob(){
  96.     gets(A);
  97.     gets(B);
  98.     string AA = postorder(trim(A));
  99.     string BB = postorder(trim(B));
  100.     for(int t = 0; t < 10; ++t){
  101.         for(int i = 'a'; i <= 'z'; ++i) val[i] = rand() % (MOD - 1) + 1;
  102.         for(int i = 'A'; i <= 'Z'; ++i) val[i] = rand() % (MOD - 1) + 1;
  103.         ll a = eval(AA);
  104.         ll b = eval(BB);
  105.         if(a != b){
  106.             printf("NO\n");
  107.             return;
  108.         }
  109.     }
  110.     printf("YES\n");
  111. }
  112.  
  113. int main(){
  114.     srand(101);
  115.     int n;
  116.     scanf("%d", &n);
  117.     char tmp[101];
  118.     gets(tmp);
  119.     for(int i = 0; i < n; ++i) prob();
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement