Problem:
Problem B:
Equals are Equals
Mr. Simpson got up with a slight feeling of tiredness. It was the start of another day of hard work. A bunch of papers were waiting for his inspection on his desk in his office. The papers contained his students' answers to questions in his Math class, but the answers looked as if they were just stains of ink.
His headache came from the ``creativity'' of his students. They provided him a variety of ways to answer each problem. He has his own answer to each problem, which is correct, of course, and the best from his aesthetic point of view.
Some of his students wrote algebraic expressions equivalent to the expected answer, but many of them look quite different from Mr. Simpson's answer in terms of their literal forms. Some wrote algebraic expressions not equivalent to his answer, but they look quite similar to it. Only a few of the students' answers were exactly the same as his.
It is his duty to check if each expression is mathematically equivalent to the answer he has prepared. This is to prevent expressions that are equivalent to his from being marked as ``incorrect'', even if they are not acceptable to his aesthetic moral.
He had now spent five days checking the expressions. Suddenly, he stood up and yelled, ``I've had enough! I must call for help.''
Your job is to write a program to help Mr. Simpson to judge if each answer is equivalent to the ``correct'' one. Algebraic expressions written on the papers are multi-variable polynomials over variable symbols
a
,
b
,...,
z
with integer coefficients, e.g.,

(
a
+
b
2
)(
a
-
b
2
),
ax
2
+2
bx
+
c
and (
x
2
+5
x
+ 4)(
x
2
+ 5
x
+ 6) + 1.
Mr. Simpson will input every answer expression as it is written on the papers; he promises you that an algebraic expression he inputs is a sequence of terms separated by additive operators `
+
' and `
-
', representing the sum of the terms with those operators, if any; a term is a juxtaposition of multiplicands, representing their product; and a multiplicand is either (a) a non-negative integer as a digit sequence in decimal, (b) a variable symbol (one of the lowercase letters `
a
' to `
z
'), possibly followed by a symbol `
^
' and a non-zero digit, which represents the power of that variable, or (c) a parenthesized algebraic expression, recursively. Note that the operator `
+
' or `
-
' appears only as a binary operator and not as a unary operator to specify the sing of its operand.
He says that he will put one or more space characters before an integer if it immediately follows another integer or a digit following the symbol `
^
'. He also says he may put spaces here and there in an expression as an attempt to make it readable, but he will never put a space between two consecutive digits of an integer. He remarks that the expressions are not so complicated, and that any expression, having its `
-
's replaced with `
+
's, if any, would have no variable raised to its 10th power, nor coefficient more than a billion, even if it is fully e
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

constexpr int W = 26;
using Var = std::array<int, W>;
std::ostream& operator<<(std::ostream& os, const Var& var) {
    os << '(';
    for (int i = 0; i < W; i++) {
        if (var[i] == 0) continue;
        os << '(' << (char)(i + 'a') << '^' << var[i] << ')';
    }
    os << ')';
    return os;
}

Var op(const Var& lhs, const Var& rhs) {
    Var res;
    res.fill(0);
    for (int i = 0; i < W; i++) {
        res[i] = lhs[i] + rhs[i];
    }
    return res;
}

struct Poly {
    // data[var] = coef;
    std::map<Var, long long> data;

    Poly(long long value = 0) {
        Var var;
        var.fill(0);
        data[var] = value;
    }
    Poly(char c, int exp = 1) {
        Var var;
        var.fill(0);
        var[c - 'a'] += exp;
        data[var] = 1;
    }

    friend Poly operator+(const Poly& lhs, const Poly& rhs) {
        if (lhs.data.size() > rhs.data.size()) return rhs + lhs;
        auto res = rhs;
        for (const auto& [var, coef]: lhs.data) {
            res.data[var] += coef;
        }
        return res;
    }

    friend Poly operator-(const Poly& lhs, const Poly& rhs) {
        auto res = lhs;
        for (const auto& [var, coef]: rhs.data) {
            res.data[var] -= coef;
        }
        return res;
    }

    friend Poly operator*(const Poly& lhs, const Poly& rhs) {
        Poly res;
        for (const auto& [var1, coef1]: lhs.data) {
            for (const auto& [var2, coef2]: rhs.data) {
                res.data[op(var1, var2)] += coef1 * coef2;
            }
        }
        return res;
    }

    friend bool operator==(Poly& lhs, Poly& rhs) {
        for (const auto& [var, coef]: lhs.data) {
            if (rhs.data[var] != coef) return false;
        }
        for (const auto& [var, coef]: rhs.data) {
            if (lhs.data[var] != coef) return false;
        }
        return true;
    }

    Poly pow(int exp) {
        Poly base(*this);
        Poly res;
        while (exp > 0) {
            if (exp & 1) res = res + base;
            base = base + base;
            exp >>= 1;
        }
        return res;
    }

    friend std::ostream& operator<<(std::ostream& os, const Poly& poly) {
        for (const auto& [var, coef]: poly.data) {
            if (coef == 0) continue;
            if (coef > 0) {
                os << '+';
            }
            os << coef << var << ' ';
        }
        return os;
    }
};

struct Parser {
    using CopyIter = std::string::const_iterator;
    using Iter = CopyIter&;
    std::string line;
    Parser() {}
    void skip(Iter it, char c) {
        // std::cerr << *it << ' ' << c << std::endl;
        assert(*it == c);
        ++it;
    }
    void skip_space(Iter it) {
        while (it != line.end() && *it == ' ') ++it;
    }

    Poly parse(const std::string& s) {
        line = s;
        auto it = line.cbegin();
        auto res = expr(it);
        assert(it == line.end());
        return res;
    }

    Poly expr(Iter it) {
        auto res = term(it);

        while (it != line.end() && (*it == '+' || *it == '-')) {
            if (*it == '+') {
                skip(it, '+');
                skip_space(it);
                res = res + term(it);
            } else {
                skip(it, '-');
                skip_space(it);
                res = res - term(it);
            }
        }

        skip_space(it);
        return res;
    }

    Poly term(Iter it) {
        auto res = factor(it);

        while (it != line.end() && *it != '+' && *it != '-' && *it != ')') {
            res = res * factor(it);
        }

        skip_space(it);
        return res;
    }

    Poly factor(Iter it) {
        if (std::isdigit(*it)) {
            return nonnegative_number(it);
        } else if (std::isalpha(*it)) {
            return variable(it);
        } else {
            skip(it, '(');
            skip_space(it);
            auto res = expr(it);
            skip(it, ')');
            skip_space(it);
            return res;
        }
    }

    Poly nonnegative_number(Iter it) {
        long long res = 0;
        while (it != line.end() && std::isdigit(*it)) {
            char c = *it;
            skip(it, c);
            res = 10 * res + c - '0';
        }
        skip_space(it);
        return Poly(res);
    }

    Poly variable(Iter it) {
        char var = *it;
        assert(std::isalpha(var));
        skip(it, var);
        skip_space(it);
        Poly res(var);
        if (it != line.end() && *it == '^') {
            skip(it, '^');
            skip_space(it);
            char exp = *it;
            assert(std::isdigit(exp) && exp != '0');
            skip(it, exp);
            res = Poly(var, exp - '0');
        }
        skip_space(it);
        return res;
    }
};

void parser_test() {
    std::vector<std::string> tests = {
        "a+b+c",
        "a a a",
        "a^2 a a",
        "(a^2+b)(a+b)",
        "2ab (4a+2b)",
        "2 2 3 3 3 a"
    };

    for (auto test: tests) {
        std::cerr << test << ": ";
        Parser parser;
        std::cerr << parser.parse(test) << std::endl;
    }
}

int main() {
    // parser_test();
    Parser parser;
    while (true) {
        std::string line;
        std::getline(std::cin, line);
        if (line == ".") break;
        Poly answer = parser.parse(line);

        while (true) {
            std::getline(std::cin, line);
            if (line == ".") break;
            Poly solution = parser.parse(line);
            if (answer == solution) {
                std::cout << "yes" << std::endl;
            } else {
                std::cout << "no" << std::endl;
            }
        }

        std::cout << "." << std::endl;
    }
}
accuracy: 1
time_ms: 3480
memory_kb: 9316
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
struct monomial{
  vector<pair<char, int>> deg;
  monomial(){
  }
  monomial(char c, int x){
    deg.push_back(make_pair(c, x));
  }
};
bool operator ==(monomial A, monomial B){
  return A.deg == B.deg;
}
bool operator < (monomial A, monomial B){
  return A.deg < B.deg;
}
monomial operator *(monomial A, monomial B){
  int N = A.deg.size();
  int M = B.deg.size();
  for (int i = 0; i < M; i++){
    bool ok = false;
    for (int j = 0; j < N; j++){
      if (A.deg[j].first == B.deg[i].first){
        A.deg[j].second += B.deg[i].second;
        ok = true;
      }
    }
    if (!ok){
      A.deg.push_back(B.deg[i]);
    }
  }
  sort(A.deg.begin(), A.deg.end());
  return A;
}
ostream& operator <<(ostream& os, monomial A){
  int N = A.deg.size();
  for (int i = 0; i < N; i++){
    cout << A.deg[i].first;
    if (A.deg[i].second >= 2){
      cout << "^" << A.deg[i].second;
    }
  }
  return os;
}
struct polynomial{
  vector<pair<monomial, int>> term;
  polynomial(){
  }
  polynomial(int x){
    if (x != 0){
      term.push_back(make_pair(monomial(), x));
    }
  }
  polynomial(char c, int x){
    term.push_back(make_pair(monomial(c, x), 1));
  }
};
bool operator ==(polynomial A, polynomial B){
  return A.term == B.term;
}
polynomial operator -(polynomial A){
  int N = A.term.size();
  for (int i = 0; i < N; i++){
    A.term[i].second *= -1;
  }
  return A;
}
polynomial operator +(polynomial A, polynomial B){
  int N = A.term.size();
  int M = B.term.size();
  for (int i = 0; i < M; i++){
    bool ok = false;
    for (int j = 0; j < N; j++){
      if (A.term[j].first == B.term[i].first){
        A.term[j].second += B.term[i].second;
        ok = true;
      }
    }
    if (!ok){
      A.term.push_back(B.term[i]);
    }
  }
  sort(A.term.begin(), A.term.end());
  int K = A.term.size();
  polynomial ans;
  for (int i = 0; i < K; i++){
    if (A.term[i].second != 0){
      ans.term.push_back(A.term[i]);
    }
  }
  return ans;
}
polynomial operator -(polynomial A, polynomial B){
  return A + (-B);
}
polynomial operator *(polynomial A, polynomial B){
  int N = A.term.size();
  int M = B.term.size();
  map<monomial, int> mp;
  for (int i = 0; i < N; i++){
    for (int j = 0; j < M; j++){
      monomial X = A.term[i].first * B.term[j].first;
      int coef = A.term[i].second * B.term[j].second;
      mp[X] += coef;
    }
  }
  polynomial ans;
  for (auto P : mp){
    if (P.second != 0){
      ans.term.push_back(P);
    }
  }
  return ans;
}
ostream& operator <<(ostream& os, polynomial A){
  int N = A.term.size();
  for (int i = 0; i < N; i++){
    if (i > 0 && A.term[i].second > 0){
      cout << '+';
    }
    if (A.term[i].second < 0){
      cout << "-" ;
    }
    int x = abs(A.term[i].second);
    if (x > 1){
      cout << x;
    }
    cout << A.term[i].first;
  }
  return os;
}
int number(string &S, int &p){
  int ans = 0;
  while (isdigit(S[p]) != 0){
    ans = ans * 10 + (S[p] - '0');
    p++;
  }
  return ans;
}
polynomial expression(string &S, int &p);
polynomial factor(string &S, int &p){
  if (S[p] == '('){
    p++;
    polynomial ans = expression(S, p);
    p++;
    return ans;
  } else if (isdigit(S[p]) != 0){
    int x = number(S, p);
    return polynomial(x);
  } else {
    char c = S[p];
    p++;
    int x = 1;
    if (S[p] == '^'){
      p++;
      x = number(S, p);
    }
    return polynomial(c, x);
  }
}
polynomial term(string &S, int &p){
  int p2 = p;
  polynomial ans = factor(S, p);
  while (true){
    if (S[p] == '*'){
      p++;
    }
    if (S[p] == '+' || S[p] == '-' || S[p] == ')' || S[p] == '='){
      break;
    }
    polynomial A = factor(S, p);
    ans = ans * A;
  }
  return ans;
}
polynomial expression(string &S, int &p){
  polynomial ans = term(S, p);
  while (true){
    if (S[p] == '+'){
      p++;
      ans = ans + term(S, p);
    } else if (S[p] == '-'){
      p++;
      ans = ans - term(S, p);
    } else {
      break;
    }
  }
  return ans;
}    
int main(){
  while (true){
    vector<string> S;
    while (true){
      string s;
      getline(cin, s);
      if (s == "."){
        break;
      }
      int N = s.size();
      string t;
      t += s[0];
      for (int i = 1; i < N; i++){
        if (s[i] != ' '){
          if (isdigit(s[i]) != 0 && isdigit(t.back()) != 0 && isdigit(s[i - 1]) == 0){
            t += '*';
          }
          t += s[i];
        }
      }
      S.push_back(t);
    }
    if (S.empty()){
      break;
    }
    int N = S.size();
    vector<polynomial> P(N);
    for (int i = 0; i < N; i++){
      S[i] += '=';
      int p = 0;
      P[i] = expression(S[i], p);
    }
    for (int i = 1; i < N; i++){
      if (P[i] == P[0]){
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
    cout << "." << endl;
  }
}
accuracy: 1
time_ms: 110
memory_kb: 9444
final_rank: 18

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"
#include "complex"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

struct Component {
	vector<long long int>dir;
	long long int by;
	Component() {
		by = -1;
		dir.resize(26);
	}
	Component(vector<long long int>d, long long int b) :dir(d), by(b) {

	}
	bool operator<(const Component&c)const {
		return make_pair(dir, by) < make_pair(c.dir, c.by);
	}
	bool operator!=(const Component&c)const {
		return make_pair(by, dir) != make_pair(c.by, c.dir);
	}
	Component operator*(const Component&c)const {
		Component ret(dir, by);
		for (int i = 0; i < 26; i++) {
			ret.dir[i] += c.dir[i];
		}
		ret.by *= c.by;
		return ret;
	}
};

struct Expression {
	vector<Component>v;
	Expression() {
	}
	Expression(vector<Component>vv) :v(vv) {

	}
	void ExpSort() {
		sort(v.begin(), v.end());
		vector<Component>w;
		for (auto i : v) {
			if (w.empty() || w.back().dir != i.dir) {
				w.push_back(i);
			}
			else {
				w.back().by += i.by;
			}
		}
		v.clear();
		for (auto i : w) {
			if (i.by)v.push_back(i);
		}
	}
	bool operator==(const Expression&e)const {
		if (v.size() != e.v.size())return false;
		for (int i = 0; i < v.size(); i++) {
			if (v[i] != e.v[i])return false;
		}
		return true;
	}
	Expression operator+(const Expression&e)const {
		auto ret = Expression(v);
		for (auto i : e.v)ret.v.push_back(i);
		return ret;
	}
	Expression operator-(const Expression&e)const {
		auto ret = Expression(v);
		for (auto i : e.v) {
			ret.v.push_back(i);
			ret.v.back().by *= -1;
		}
		return ret;
	}
	Expression operator*(const Expression&e)const {
		auto ret = Expression();
		for (auto i : v) {
			for (auto j : e.v) {
				ret.v.push_back(i*j);
			}
		}
		return ret;
	}
	Expression operator^(const Expression&e)const {
		auto ret = Expression(v);
		for (auto &i : ret.v) {
			for (auto &j : i.dir) j *= e.v.front().by;
		}
		ret.v.front().by = powl(ret.v.front().by, e.v.front().by);
		return ret;
	}
};


void DEBUG(Expression e) {
	cout << e.v.size() << endl;
	for (auto i : e.v) {
		cout << i.by;
		for (auto j : i.dir)cout << " " << j;
		cout << endl;
	}
}

Expression func(string s) {
//	cout << s << endl;
	int num = 0;
	for (int i = s.size() - 1; i >= 0; i--) {
		if (s[i] == ')') num++;
		if (s[i] == '(') num--;
		if (num == 0) {
			if (s[i] == '+') {
				auto a = func(s.substr(0, i));
				auto b = func(s.substr(i + 1, s.size() - i - 1));
				return a + b;
			}
			if (s[i] == '-') {
				auto a = func(s.substr(0, i));
				auto b = func(s.substr(i + 1, s.size() - i - 1));
				return a - b;
			}
		}
	}
	for (int i = s.size() - 1; i >= 0; i--) {
		if (s[i] == ')') num++;
		if (s[i] == '(') num--;
		if (num == 0) {
			if (s[i] == '*') {
				auto a = func(s.substr(0, i));
				auto b = func(s.substr(i + 1, s.size() - i - 1));
				return a * b;
			}
		}
	}
	for (int i = s.size() - 1; i >= 0; i--) {
		if (s[i] == ')') num++;
		if (s[i] == '(') num--;
		if (num == 0) {
			if (s[i] == '^') {
				auto a = func(s.substr(0, i));
				auto b = func(s.substr(i + 1, s.size() - i - 1));
				return a ^ b;
			}
		}
	}
	if (s.front() == '(') {
		return func(s.substr(1, s.size() - 2));
	}
	Component ret;
	for (auto i : s) {
		if (i >= 'a'&&i <= 'z') {
			ret.dir[i - 'a']++;
		}
		else {
			if (ret.by == -1)ret.by = 0;
			ret.by *= 10;
			ret.by += i - '0';
		}
	}
	if(ret.by==-1)ret.by *= -1;
	Expression re;
	re.v.push_back(ret);
	return re;
}

bool IsNum(char c) {
	return (c >= '0'&&c <= '9')  ;
}

bool IsAlpha(char c) {
	return (c >= 'a'&&c <= 'z');
}

string EraseSpace(string s) {
	string ret;
	for (auto i : s) {
		if (i != ' ')ret.push_back(i);
		else {
			if (!ret.empty() && ret.back() != ' ')ret.push_back(i);
		}
	}
	while (ret.back() == ' ')ret.pop_back();
	s = ret;
	for (int i = 1; i + 1 < s.size(); i++) {
		auto bf = s[i - 1];
		auto nx = s[i + 1];
		if (s[i] == ' ') {
			if (IsNum(bf) || IsAlpha(bf) || bf == ')') {
				if (IsNum(nx) || IsAlpha(nx) || nx == '(') {
					s[i] = '*';
				}
			}
		}
	}
	ret.clear();
	for (auto i : s) {
		if (i == ' ')continue;
		if (ret.empty()) {
			ret.push_back(i);
			continue;
		}
		if (ret.back() == ')'&&(i == '('||IsNum(i)||IsAlpha(i)))ret.push_back('*');
		else if ((ret.back() == ')' || IsNum(ret.back()) || IsAlpha(ret.back())) && i == '(')ret.push_back('*');
		else if (IsNum(ret.back()) && IsAlpha(i))ret.push_back('*');
		else if (IsAlpha(ret.back()) && IsNum(i))ret.push_back('*');
		else if (IsAlpha(ret.back()) && IsAlpha(i))ret.push_back('*');
		ret.push_back(i);
	}
	return ret;
}


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	string s;
	while (getline(cin, s), s != ".") {
		auto ans = func(EraseSpace(s));
		ans.ExpSort();
	//	DEBUG(ans);
		while (getline(cin, s), s != ".") {
			auto box = func(EraseSpace(s));
			box.ExpSort();
		//	DEBUG(box);
			if (ans == box)cout << "yes\n";
			else cout << "no\n";
		}
		cout << s << endl;
	}
}
accuracy: 1
time_ms: 40
memory_kb: 3816
final_rank: 12

code_snippet: #include<iostream>
#include<vector>
#include<map>
using namespace std;
struct Str{
	string s;
	Str():s(){}
	const char operator[](int id)const{return s[id];}
	const size_t size()const{return s.size();}
	Str&operator+=(const char c){
		s+=c;
		return*this;
	}
	Str operator*(const Str&a){
		Str ret;
		int i=0,j=0;
		while(i<s.size()||j<a.size())
		{
			if(i<s.size()&&j<a.size())
			{
				if(s[i]<a[j])ret+=s[i++];
				else ret+=a[j++];
			}
			else if(i<s.size())ret+=s[i++];
			else ret+=a[j++];
		}
		return ret;
	}
	bool operator<(const Str&a)const{
		return s<a.s;
	}
	bool operator==(const Str&a)const{return s==a.s;}
};
struct exp{
	map<Str,int>mp;
	exp():mp(){}
	int&operator[](const Str&a){return mp[a];}
	exp norm(){
		exp ret;
		for(pair<Str,int>p:mp)if(p.second!=0)ret[p.first]=p.second;
		return ret;
	}
	exp operator+(const exp&a){
		exp ret=*this;
		for(pair<Str,int>p:a.mp)ret[p.first]+=p.second;
		return ret.norm();
	}
	exp operator-(const exp&a){
		exp ret=*this;
		for(pair<Str,int>p:a.mp)ret[p.first]-=p.second;
		return ret.norm();
	}
	exp operator*(const exp&a){
		exp ret;
		for(pair<Str,int>p:mp)for(pair<Str,int>q:a.mp)
		{
			ret[p.first*q.first]+=p.second*q.second;
		}
		return ret.norm();
	}
	bool operator==(const exp&a)const{return mp==a.mp;}
	void dump()const{
		bool fst=true;
		for(pair<Str,int>p:mp)
		{
			if(!fst)cout<<"+";
			else fst=false;
			cout<<p.second<<"*"<<p.first.s;
		}
		cout<<endl;
	}
};
vector<string>now;
void parse(string s)
{
	now.clear();
	for(int i=0;i<s.size();i++)
	{
		if(s[i]==' ')continue;
		else if(isdigit(s[i]))
		{
			int j=i;
			while(isdigit(s[j]))j++;
			now.push_back(s.substr(i,j-i));
			i=j-1;
		}
		else now.push_back(string(1,s[i]));
	}
}
exp f(int&);
exp g(int&);
exp term(int&);
exp f(int&id)
{
	exp ret=g(id);
	while(id<now.size()&&(now[id]=="+"||now[id]=="-"))
	{
		string op=now[id++];
		exp nxt=g(id);
		if(op=="+")ret=ret+nxt;
		else ret=ret-nxt;
	}
	return ret;
}
exp g(int&id)
{
	exp ret=term(id);
	while(id<now.size()&&now[id]!="+"&&now[id]!="-"&&now[id]!=")")
	{
		ret=ret*term(id);
	}
	return ret;
}
int to_i(string s)
{
	int ret=0;
	for(char c:s)ret=ret*10+c-'0';
	return ret;
}
exp term(int&id)
{
	exp ret;
	if(now[id]=="(")
	{
		id++;
		ret=f(id);
		id++;
	}
	else if(isdigit(now[id][0]))
	{
		ret[Str()]=to_i(now[id++]);
	}
	else
	{
		Str tmp;
		tmp.s=now[id++];
		ret[tmp]=1;
	}
	if(id<now.size()&&now[id]=="^")
	{
		id++;
		int n=to_i(now[id++]);
		exp ans=ret;
		for(int i=1;i<n;i++)ans=ans*ret;
		ret=ans;
	}
	return ret;
}
main()
{
	while(true)
	{
		string s;
		getline(cin,s);
		if(s==".")break;
		parse(s);
		int id=0;
		exp ans=f(id).norm();
		while(true)
		{
			getline(cin,s);
			if(s==".")break;
			parse(s);
			id=0;
			exp ret=f(id).norm();
			cout<<(ans==ret?"yes":"no")<<endl;
		}
		cout<<"."<<endl;
	}
}
accuracy: 1
time_ms: 150
memory_kb: 3328
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=1005;
const int INF=1e6;
//const ll INF=1LL<<60;
string S;

vector<vector<ll>> dai(100,vector<ll>(26));
vector<ll> ans(100),comp(100);
int t;

ll expr(int &i);
ll term(int &i);
ll rui(int &i);
ll factor(int &i);
ll number(int &i);

ll expr(int &i){
    ll res=term(i);
    while(i<si(S)){
        if(S[i]==' ') i++;
        else if(S[i]=='+'){
            i++;
            ll x=term(i);
            res+=x;
        }else if(S[i]=='-'){
            i++;
            ll x=term(i);
            res-=x;
        }else{
            break;
        }
    }
    return res;
}

ll term(int &i){
    ll res=rui(i);
    
    while(i<si(S)){
        if(S[i]==' ') i++;
        else if(S[i]=='+'||S[i]=='-'||S[i]==')') break;
        else{
            ll x=rui(i);
            res*=x;
        }
    }
    
    return res;
}

ll rui(int &i){
    ll res=factor(i);
    
    while(i<si(S)){
        if(S[i]==' ') i++;
        else if(S[i]=='^'){
            i++;
            ll x=factor(i);
            ll k=res;
            res=1;
            for(int j=0;j<x;j++) res*=k;
        }else break;
    }
    
    return res;
}

ll factor(int &i){
    while(S[i]==' ') i++;
    
    if(i<si(S)&&S[i]=='('){
        i++;
        ll x=expr(i);
        while(S[i]==' ') i++;
        assert(S[i]=')');
        i++;
        
        return x;
    }else{
        return number(i);
    }
}

ll number(int &i){
    while(S[i]==' ') i++;
    if(isdigit(S[i])){
        ll res=0;
        while(i<si(S)&&isdigit(S[i])){
            res*=10;
            res+=ll(S[i]-'0');
            i++;
        }
        return res;
    }else{
        ll x=dai[t][S[i]-'a'];
        i++;
        return x;
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    mt19937_64 rng(58);
    
    for(int i=0;i<100;i++){
        for(int j=0;j<26;j++){
            dai[i][j]=rng()%9+1;
        }
    }
    
    while(1){
        getline(cin,S);
        if(S==".") break;
        
        t=0;
        while(t<100){
            int i=0;
            comp[t]=expr(i);
            t++;
        }
        ans=comp;
        
        while(1){
            getline(cin,S);
            if(S=="."){
                cout<<".\n";
                break;
            }
            
            t=0;
            
            while(t<100){
                int i=0;
                comp[t]=expr(i);
                t++;
            }
            
            if(ans==comp) cout<<"yes"<<endl;
            else cout<<"no"<<endl;
        }
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3260
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

struct Polynomial{
    map<string, int> terms;
    Polynomial(int a){
        terms[""] = a;
    }
    Polynomial(string s){
        terms[s] = 1;
    }
    Polynomial(){}
    static void erase_zero(Polynomial &a){
        for(auto it=a.terms.begin(); it!=a.terms.end();){
            if(it->second == 0){
                it = a.terms.erase(it);
            }else{
                it++;
            }
        }
    }
    Polynomial operator +(const Polynomial &a) const{
        Polynomial res = *this;
        for(const auto &p: a.terms){
            res.terms[p.first] += p.second;
        }
        erase_zero(res);
        return res;
    }
    Polynomial operator -(const Polynomial &a) const{
        Polynomial res = *this;
        for(const auto &p: a.terms){
            res.terms[p.first] -= p.second;
        }
        erase_zero(res);
        return res;
    }
    Polynomial operator *(const Polynomial &a) const{
        Polynomial res;
        for(const auto &p: terms){
            for(const auto &q: a.terms){
                string key = p.first + q.first;
                sort(key.begin(), key.end());
                res.terms[key] += p.second * q.second;
            }
        }
        erase_zero(res);
        return res;
    }
    bool operator ==(const Polynomial &a) const{
        return terms == a.terms;
    }
};

int getnum(int &p, const string &s){
    int num = 0;
    while(p<(int)s.length() and '0'<=s[p] and s[p]<='9'){
        num *= 10;
        num += s[p]-'0';
        p++;
    }
    return num;
}
Polynomial parse(int &p, const string &s){
    Polynomial res;
    Polynomial term(1);
    int sign = 1;
    while(1){
        if(p<(int)s.length() and s[p] == ' '){
            p++;
            continue;
        }
        if(p>=(int)s.length() or s[p]=='+' or s[p]=='-' or s[p]==')'){
            if(sign == 1) res = res + term;
            else res = res - term;
            term = Polynomial(1);
            if(p>=(int)s.length() or s[p]==')'){
                p++;
                return res;
            }
            if(s[p] == '+') sign = 1;
            else sign = -1;
            p++;
            continue;
        }
        if('0'<=s[p] and s[p]<='9'){
            term = term * Polynomial(getnum(p, s));
            continue;
        }
        if('a'<=s[p] and s[p]<='z'){
            char var = s[p];
            p++;
            int num = 1;
            while(p<(int)s.length() and s[p]==' '){
                p++;
            }
            if(p<(int)s.length() and s[p]=='^'){
                p++;
                while(s[p] == ' ') p++;
                num = getnum(p, s);
            }
            term = term * Polynomial(string(num, var));
            continue;
        }
        if(s[p] == '('){
            p++;
            term = term * parse(p, s);
            continue;
        }
    }
}

int main(){
    while(1){
        vector<string> s;
        string t;
        while(getline(cin, t), t!="."){
            s.push_back(t);
        }
        if(s.empty()) break;

        vector<Polynomial> p;
        for(auto ss: s){
            int i=0;
            p.push_back(parse(i, ss));
        }
        for(int i=1; i<(int)p.size(); i++){
            if(p[i] == p[0]){
                cout << "yes" << endl;
            }else{
                cout << "no" << endl;
            }
        }
        cout << "." << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 11632
final_rank: 19

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<int> vec;
typedef vector<string> svec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;

ll expr(string& s, int& i);
ll term1(string& s, int& i);
ll term(string& s, int& i);
ll factor(string& s, int& i);
ll number(string& s, int& i);

bool isdigit(char t) {
	return '0' <= t && t <= '9';
}
bool isalf(char t) {
	return 'a' <= t && t <= 'z';
}

ll mod_pow(ll x, int n) {
	ll ret = 1;
	while (n > 0) {
		if (n % 2)ret = ret * x%mod;
		x = x * x%mod; n >>= 1;
	}
	return ret;
}

ll expr(string& s, int& i) {
	ll val = term1(s, i);
	while (i < s.length()) {
		if (s[i] == ' ') {
			i++; continue;
		}
		if (s[i] == '+' || s[i] == '-') {
			char op = s[i];
			i++;
			ll val2 = term1(s, i);
			if (op == '+') {
				val += val2; if (val >= mod)val -= mod;
			}
			else {
				val += mod - val2; while (val >= mod)val -= mod;
			}
		}
		else break;
	}
	//cout << i << " expr " << val << endl;
	return val;
}
ll term1(string& s, int& i) {
	ll val = term(s, i);
	while (i < s.length() && s[i] == ' ')i++;
	if (i >= s.length() || s[i] == '+' || s[i] == '-'||s[i]==')') {
		return val;
	}
	else {
		ll val2 = term1(s, i);
		val = val * val2%mod;
	}
	//cout << i << " term1 " << val << endl;
	return val;
}
ll term(string& s, int& i) {
	ll val = factor(s, i);
	while (i < s.length() && s[i] == ' ')i++;
	if (i<s.length() && s[i] == '^') {
		i++;
		ll val2 = number(s, i);
		val = mod_pow(val, val2);
	}
	//cout << i << " term " << val << endl;
	return val;
}
ll factor(string& s, int& i) {
	while (i < s.length() && s[i] == ' ')i++;
	if (i<s.length() && isdigit(s[i]))return number(s, i);
	i++;
	while (i < s.length() && s[i] == ' ')i++;
	ll ret = expr(s, i);
	i++;
	while (i < s.length() && s[i] == ' ')i++;
	//cout << i << " fact " << ret << endl;
	return ret;
}
ll number(string& s, int& i) {
	while (s[i] == ' ')i++;
	ll ret = s[i++] - '0';
	while (i < s.length() && isdigit(s[i])) {
		ret = ret * 10 + s[i++] - '0';
		ret %= mod;
	}
	//cout << i << " numb " << ret << endl;
	return ret;
}

vector<vector<ll>> gene(int len) {
	random_device rnd;
	mt19937 mt(rnd());
	uniform_int_distribution<> u(0, 2147483647);
	vector<vector<ll>> ret(len);
	rep(i, len) {
		ret[i].resize(26);
		rep(j, 26) {
			ret[i][j] = u(mt);
		}
	}
	return ret;
}

ll calc(string s, vector<ll> t) {
	string upd;
	rep(i, s.length()) {
		if (isalf(s[i])) {
			string u = to_string(t[s[i] - 'a']);
			upd.push_back(' ');
			upd = upd + u;
		}
		else if (isdigit(s[i])) {
			int le = i;
			while (i + 1 < s.length() && isdigit(s[i + 1]))i++;
			upd.push_back(' ');
			Rep1(j, le, i)upd.push_back(s[j]);
		}
		else {
			upd.push_back(s[i]);
		}
	}
	int i = 0;
	//cout << s << endl;
	//cout << upd << endl;
	//cout << upd << endl;
	return expr(upd, i);
}

const int sz = 100;
bool solve() {
	string s;
	vector<string> v;
	while (getline(cin, s)) {
		if (s[0] == '.') {
			break;
		}
		v.push_back(s);
	}
	if (v.size() == 0)return false;
	vector<vector<ll>> test = gene(sz);
	vector<vector<ll>> ans(v.size());
	rep(i, v.size()) {
		ans[i].resize(sz);
		rep(j, sz) {
			ans[i][j] = calc(v[i], test[j]);
			//cout << i << " " << ans[i][j] << endl;
		}
	}
	for (int i = 1; i < v.size(); i++) {
		bool f = (ans[i] == ans[0]);
		if (f) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	cout << "." << endl;
	return true;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	/*string s = "a+b+c";
	string ss = "(a+b)+c";
	vector<ll> c(26, 0); c[0] = 1, c[1] = 2; c[2] = 3;
	cout << calc(s, c) << endl;
	cout << calc(ss, c) << endl;*/

	//cout << fixed << setprecision(10);
	//init();
	while (true) {
		if (!solve())break;
	}
	//stop
		return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 4080
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE

struct T{
  map<string, Int> ms;
  T(){}
  T(string s, Int c){
    sort(s.begin(),s.end());
    ms[s]+=c;
  }

  T operator+=(const T &a){
    for(auto p:a.ms)
      ms[p.first]+=p.second;
    return *this;
  }
  
  T operator-=(const T &a){
    for(auto p:a.ms)
      ms[p.first]-=p.second;
    return *this;
  }
  
  T operator+(const T &a) const{
    T res(*this);
    return res+=a;
  }
  
  T operator-(const T &a) const{
    T res(*this);
    return res-=a;
  }
  
  T operator*(const T &a) const{
    T res;
    for(auto p:ms)
      for(auto q:a.ms)
        res+=T(p.first+q.first,p.second*q.second);
    return res;
  }

  T pow(Int k) const{
    assert(k>=1);
    T res(*this);
    for(Int i=1;i<k;i++) res=res*(*this);
    return res;
  }

  map<string, Int> norm(){
    map<string, Int> res;
    for(auto p:ms)
      if(p.second) res[p.first]=p.second;
    return res;
  }
};

using V = vector<string>; 
T expr  (const V &vs,Int &p);
T term  (const V &vs,Int &p);
T factor(const V &vs,Int &p);
T power (const V &vs,Int &p);
Int number(const V &vs,Int &p);

T expr  (const V &vs,Int &p){
  T res=term(vs,p);
  while(p<(Int)vs.size()){
    if(vs[p]=="+"s){
      p++;
      res=res+term(vs,p);
      continue;
    }
    if(vs[p]=="-"s){
      p++;
      res=res-term(vs,p);
      continue;
    }
    break;
  }
  return res;
}

T term  (const V &vs,Int &p){  
  T res=factor(vs,p);
  while(p<(Int)vs.size()){
    if(vs[p]=="+"s) break;
    if(vs[p]=="-"s) break;
    if(vs[p]=="^"s) break;
    if(vs[p]==")"s) break;
    res=res*factor(vs,p);
  }
  return res;
}

T factor(const V &vs,Int &p){  
  T res=power(vs,p);
  if(p<(Int)vs.size()&&vs[p]=="^"s){
    p++;
    Int k=number(vs,p);
    return res.pow(k);
  }  
  return res;
}

T power (const V &vs,Int &p){
  if(vs[p]=="("s){
    p++;
    T res=expr(vs,p);
    assert(vs[p]==")"s);
    p++;
    return res;
  }
  if(isalpha(vs[p][0])) return T(vs[p++],1);
  return T(""s, number(vs,p));
}

Int number(const V &vs,Int &p){
  return stoll(vs[p++]);
}

T tran(string s){
  Int n=s.size();
  string t;
  for(Int i=0;i<n;i++){
    if(!isdigit(s[i])) t+=' ';
    t+=s[i];
    if(!isdigit(s[i])) t+=' ';
  }  
  V vs;
  stringstream ss(t);
  while(ss>>t) vs.emplace_back(t);
  Int p=0;
  return expr(vs,p);
}

signed main(){
  string s;
  while(getline(cin,s)){
    if(s=="."s) break;
    auto vs=tran(s);
    while(1){
      string t;
      getline(cin,t);      
      if(t=="."s) break;
      auto vt=tran(t);
      cout<<(vs.norm()==vt.norm()?"yes":"no")<<endl;
    }    
    cout<<"."<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 370
memory_kb: 3476
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using f = map<string,ll>;

void PRINT(f a){
    for(const auto &p:a){
        cout << p.se << p.fi << " + ";
    }
    cout << endl;
}

f norm(f a){
    f ret;
    for(const auto &p:a){
        string v = p.fi;
        sort(all(v));
        if(p.se != 0) ret[v] = p.se;
    }
    return ret;
}

f sub(f a){
    a = norm(a);

    f ret;
    for(const auto &p:a){
        string v = p.fi;
        if(p.se != 0) ret[v] = -p.se;
    }
    return ret;
}

f add(f a, f b){
    a = norm(a);
    b = norm(b);

    for(const auto &p:b) a[p.fi] += p.se;

    return norm(a);
}

f mul(f a, f b){
    a = norm(a);
    b = norm(b);

    f ret;
    for(const auto &p:a)for(const auto &q:b){
        string var = p.fi+q.fi;
        sort(all(var));

        ret[var] += p.se*q.se;
    }
    return norm(ret);
}

f E(string s);

f T(string s){
    int n = s.size();
    f ret;
    ret[""]=1;

    int idx = 0;
    while(idx<n){
        f m;
        if(s[idx]==' '){
            ++idx;
            continue;
        }
        else if(s[idx]=='('){
            int ep = idx;
            int p = 0;
            while(ep<n){
                if(s[ep]=='(') ++p;
                if(s[ep]==')'){
                    --p;
                    if(p==0) break;
                }
                ++ep;
            }
            assert(ep<n);
            assert(s[ep]==')');

            string t = s.substr(idx+1,ep-idx-1);
            m = E(t);
            idx = ep+1;
        }
        else if(isdigit(s[idx])){
            ll val = 0;
            while(idx<n && isdigit(s[idx])){
                val = val*10 + (s[idx]-'0');
                ++idx;
            }

            int pw = 1;
            int nx = idx;
            while(nx<n && s[nx]==' ') ++nx;
            if(nx<n && s[nx]=='^'){
                ++nx;
                while(nx<n && s[nx]==' ') ++nx;

                assert(nx<n);
                assert(isdigit(s[nx]));

                pw = 0;
                while(nx<n && isdigit(s[nx])){
                    pw = 10*pw + (s[nx]-'0');
                    ++nx;
                }
            }

            ll vv = 1;
            rep(_,pw) vv *= val;

            m[""] = vv;
            idx = nx;
        }
        else if(islower(s[idx])){
            char c = s[idx];
            ++idx;

            int pw = 1;
            int nx = idx;
            while(nx<n && s[nx]==' ') ++nx;
            if(nx<n && s[nx]=='^'){
                ++nx;
                while(nx<n && s[nx]==' ') ++nx;

                assert(nx<n);
                assert(isdigit(s[nx]));

                pw = 0;
                while(nx<n && isdigit(s[nx])){
                    pw = 10*pw + (s[nx]-'0');
                    ++nx;
                }
            }

            string t = "";
            rep(_,pw) t += c;

            m[t] = 1;
            idx = nx;
        }
        else assert(false);

        ret = mul(ret, m);
    }

    return norm(ret);
}

f E(string s){
    int n = s.size();
    f ret;

    int p = 0;
    int start = 0;
    bool plus = true;

    rep(i,n){
        if(s[i]=='(') ++p;
        if(s[i]==')') --p;

        if(s[i]=='+' || s[i]=='-'){
            if(p==0){
                string term = s.substr(start,i-start);
                f t = norm(T(term));
                if(!plus) t = sub(t);
                plus = (s[i]=='+');

                ret = add(ret, t);

                start = i+1;
            }
        }
    }

    string term = s.substr(start,n-start);
    f t = norm(T(term));
    if(!plus) t = sub(t);

    ret = add(ret, t);

    return norm(ret);
}

int main(){
    string s;
    while(getline(cin, s),(s!=".")){
        f S = E(s);
        // PRINT(S);

        string t;
        while(getline(cin, t),(t!=".")){
            f T = E(t);
            // PRINT(T);
            cout << (S==T?"yes":"no") << endl;
        }
        cout << "." << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 350
memory_kb: 3644
final_rank: 15

code_snippet: #include <cstdio>
#include <cstdint>
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
#include <random>

intmax_t ppow(intmax_t base, intmax_t iexp) {
  intmax_t res=1;
  for (intmax_t dbl=base; iexp; iexp>>=1) {
    if (iexp & 1)
      res *= dbl;

    dbl *= dbl;
  }
  return res;
}

intmax_t apply(intmax_t lhs, char op, intmax_t rhs) {
  switch (op) {
    case '+': return lhs + rhs;
    case '-': return lhs - rhs;
    case '*': return lhs * rhs;
    case '^': return ppow(lhs, rhs);
    default: assert(false);
  }
}

intmax_t fourt(
    const std::string &s, size_t &i, const std::vector<intmax_t> &vars,
    const std::vector<std::string> &ops={"+-", "*", "^", ""},
    size_t prec=0) {

  /* tsurai parser */
  if (ops[prec].empty()) {
    if (s[i] == '(') {
      intmax_t tmp=fourt(s, ++i, vars, ops, 0);
      assert(s[i] == ')');
      ++i;
      return tmp;
    }
    if (isalpha(s[i])) {
      // we assume only one-character variables
      return vars[s[i++]-'a'];
    }
    intmax_t res=0;
    while (s[i] >= '0' && s[i] <= '9')
      res = res*10 + s[i++]-'0';
    return res;
  }

  intmax_t res=fourt(s, i, vars, ops, prec+1);
  while (i < s.length()) {
    char op=s[i];
    if (!std::count(ops[prec].begin(), ops[prec].end(), op))
      break;
    intmax_t tmp=fourt(s, ++i, vars, ops, prec+1);
    res = apply(res, op, tmp);
  }
  return res;
}

intmax_t parse(const std::string &s, const std::vector<intmax_t> &vars) {
  size_t i=0;
  return fourt(s, i, vars);
}

std::string normalize(const std::string &s) {
  // explicit `*' signs and no spaces
  std::string res;
  enum {
    NONE, PAREN, FACTOR, OPERATOR
  } last=NONE;
  for (size_t i=0; i<s.length(); ++i) {
    if (s[i] == '(') {
      if (last == FACTOR) {
        res += '*';
      }
      res += '(';
      last = PAREN;
    } else if (s[i] == ')') {
      res += ')';
      last = FACTOR;
    } else if (isalnum(s[i])) {
      if (last == FACTOR) {
        res += '*';
      }
      res += s[i];
      if (isdigit(s[i])) {
        while (i+1 < s.length() && isdigit(s[i+1]))
          res += s[++i];
      }
      last = FACTOR;
    } else if (!isspace(s[i])) {
      res += s[i];
      last = OPERATOR;
    }
  }
  // fprintf(stderr, "#< %s\n", res.c_str());
  return res;
}

std::mt19937 rsk(0315);
int testcase_ends() {
  char buf[96];
  fgets(buf, sizeof buf, stdin);
  std::string s=buf;
  if (s == ".\n")
    return 1;

  s = normalize(s);
  while (true) {
    fgets(buf, sizeof buf, stdin);
    std::string t=buf;
    if (t == ".\n") {
      printf(".\n");
      return 0;
    }

    t = normalize(t);
    bool yes=true;
    for (int i=0; i<810; ++i) {
      std::vector<intmax_t> vars(26);
      for (int j=0; j<26; ++j)
        vars[j] = rsk() % 100;
      
      if (parse(s, vars) != parse(t, vars))
        yes = false;
    }

    printf("%s\n", yes? "yes":"no");
  }
}

int main() {
  while (!testcase_ends()) {}
}
accuracy: 1
time_ms: 760
memory_kb: 2584
final_rank: 16

code_snippet: #include <cstdio>
#include <cstdint>
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

//#define fprintf(...) void(0)

class Poly {
  std::map<std::string, int> coefs;
  // e.g. coefs["aab"] == 2 <=> the polynomial has the term 2 a^2 b
  const std::string s;

  Poly &operator ^=(const Poly &oth) {
    if (oth.coefs.size() == 1 && oth.coefs.find("") != oth.coefs.end()) {
      return (*this) ^= oth.coefs.find("")->second;
    } else if (oth.coefs.empty()) {
      // means 0
      coefs.clear();
      coefs[""] = 1;  // means 1
      return (*this);
    }

    assert(false);
  }

  Poly &op_eq(char op, const Poly &rhs) {
    // (*this) @= rhs
    switch (op) {
      case '+': return (*this) += rhs;
      case '-': return (*this) -= rhs;
      case '*': return (*this) *= rhs;
      case '^': return (*this) ^= rhs;
      default: assert(false);
    }
  }

  Poly fourt(
      size_t &i, 
      const std::vector<std::string> &ops={"+-", "*", "^", ""},
      size_t prec=0) {

    /* based-on tsurai parser */

    if (ops[prec].empty()) {
      if (s[i] == '(') {
        Poly tmp=fourt(++i, ops, 0);
        assert(s[i] == ')');
        ++i;
        return tmp;
      }
      if (isalpha(s[i])) {
        // we assume only one-character variables
        return Poly(s[i++]);
      }
      int coef=0;
      while (s[i] >= '0' && s[i] <= '9')
        coef = coef*10 + s[i++]-'0';
      return Poly(coef);
    }

    Poly res=fourt(i, ops, prec+1);
    while (i < s.length()) {
      char op=s[i];
      if (!std::count(ops[prec].begin(), ops[prec].end(), op))
        break;
      Poly tmp=fourt(++i, ops, prec+1);
      res.op_eq(op, tmp);
    }
    return res;
  }

  Poly &normalized() {
    for (auto it=coefs.begin(); it!=coefs.end(); ++it)
      if (it->second == 0)
        coefs.erase(it);

    return *this;
  }

public:
  Poly(const std::string &s): s(s) {
    size_t i=0;
    coefs = fourt(i).coefs;  // ugh
  }

  Poly(char ch): s(1, ch) {
    assert(isalpha(ch));
    coefs[s] = 1;
  }

  Poly(int n): s(std::to_string(n)) {
    if (n)
      coefs[""] = n;
  }

  Poly &operator +=(const Poly &oth) {
    for (const auto &p: oth.coefs) {
      const std::string &param=p.first;
      int coef=p.second;
      coefs[param] += coef;
    }
    return normalized();
  }

  Poly &operator -=(const Poly &oth) {
    for (const auto &p: oth.coefs) {
      const std::string &param=p.first;
      int coef=p.second;
      coefs[param] -= coef;
    }
    return normalized();
  }

  Poly &operator *=(const Poly &oth) {
    Poly tmp(*this);
    coefs.clear();
    for (const auto &pl: tmp.coefs) {
      for (const auto &pr: oth.coefs) {
        std::string param=pl.first+pr.first;
        std::sort(param.begin(), param.end());
        coefs[param] += pl.second*pr.second;
      }
    }
    return normalized();
  }

  Poly &operator ^=(int iexp) {
    if (iexp == 0) {
      // does not appear in #1233
      coefs.clear();
      coefs[""] = 1;  // means 1
    } else {
      Poly tmp(*this);
      for (int i=1; i<iexp; ++i)
        // O(iexp) multiplications, ugh
        *this *= tmp;
    }
    return normalized();
  }

  Poly operator +(const Poly &oth) const { return Poly(*this) += oth; }
  Poly operator -(const Poly &oth) const { return Poly(*this) -= oth; }
  Poly operator *(const Poly &oth) const { return Poly(*this) *= oth; }
  Poly operator ^(int iexp) const { return Poly(*this) ^= iexp; }
  bool operator ==(const Poly &oth) const { return coefs == oth.coefs; }

  void debug() const {
    if (coefs.empty()) {
      fprintf(stderr, "#> 0\n");
      return;
    }
    fprintf(stderr, "#>");
    for (const auto &p: coefs)
      fprintf(stderr, " %d%s", p.second, p.first.c_str());
    fprintf(stderr, "\n");
  }
};

std::string normalize(const std::string &s) {
  // explicit `*' signs and no spaces
  std::string res;
  enum {
    NONE, PAREN, FACTOR, OPERATOR
  } last=NONE;
  for (size_t i=0; i<s.length(); ++i) {
    if (s[i] == '(') {
      if (last == FACTOR) {
        res += '*';
      }
      res += '(';
      last = PAREN;
    } else if (s[i] == ')') {
      res += ')';
      last = FACTOR;
    } else if (isalnum(s[i])) {
      if (last == FACTOR) {
        res += '*';
      }
      res += s[i];
      if (isdigit(s[i])) {
        while (i+1 < s.length() && isdigit(s[i+1]))
          res += s[++i];
      }
      last = FACTOR;
    } else if (!isspace(s[i])) {
      res += s[i];
      last = OPERATOR;
    }
  }
  // fprintf(stderr, "#< %s\n", res.c_str());
  return res;
}

int testcase_ends() {
  char buf[96];
  fgets(buf, sizeof buf, stdin);
  std::string s=buf;
  if (s == ".\n")
    return 1;

  s = normalize(s);
  Poly ps(s);
  while (true) {
    fgets(buf, sizeof buf, stdin);
    std::string t=buf;
    if (t == ".\n") {
      printf(".\n");
      return 0;
    }

    t = normalize(t);
    Poly pt(t);
    printf("%s\n", (ps == pt)? "yes":"no");
  }
}

int main() {
  while (!testcase_ends()) {}
}
accuracy: 1
time_ms: 100
memory_kb: 3112
final_rank: 5

code_snippet: #include <cstdio>
#include <cstdint>
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

#define fprintf(...) void(0)

class Poly {
  std::map<std::string, int> coefs;
  // e.g. coefs["aab"] == 2 <=> the polynomial has the term 2 a^2 b
  const std::string s;

  Poly &operator ^=(const Poly &oth) {
    if (oth.coefs.size() == 1 && oth.coefs.find("") != oth.coefs.end()) {
      return (*this) ^= oth.coefs.find("")->second;
    } else if (oth.coefs.empty()) {
      // means 0
      coefs.clear();
      coefs[""] = 1;  // means 1
      return (*this);
    }

    assert(false);
  }

  Poly &op_eq(char op, const Poly &rhs) {
    // (*this) @= rhs
    switch (op) {
      case '+': return (*this) += rhs;
      case '-': return (*this) -= rhs;
      case '*': return (*this) *= rhs;
      case '^': return (*this) ^= rhs;
      default: assert(false);
    }
  }

  Poly fourt(
      size_t &i, 
      const std::vector<std::string> &ops={"+-", "*", "^", ""},
      size_t prec=0) {

    if (ops[prec].empty()) {
      if (s[i] == '(') {
        Poly tmp=fourt(++i, ops, 0);
        assert(s[i] == ')');
        ++i;
        return tmp;
      }
      if (isalpha(s[i])) {
        // we assume only one-character variables
        return Poly(s[i++]);
      }
      int coef=0;
      while (s[i] >= '0' && s[i] <= '9')
        coef = coef*10 + s[i++]-'0';
      return Poly(coef);
    }

    Poly res=fourt(i, ops, prec+1);
    while (i < s.length()) {
      char op=s[i];
      if (!std::count(ops[prec].begin(), ops[prec].end(), op))
        break;
      Poly tmp=fourt(++i, ops, prec+1);
      res.op_eq(op, tmp);
    }
    return res;
  }

  Poly &normalized() {
    for (auto it=coefs.begin(); it!=coefs.end(); ++it) {
      if (it->second == 0) {
        fprintf(stderr, "Removed: %s\n", it->first.c_str());
        coefs.erase(it);
      }
    }
    return *this;
  }

public:
  Poly(const std::string &s): s(s) {
    size_t i=0;
    coefs = fourt(i).coefs;  // ugh
  }

  Poly(char ch): s(1, ch) {
    assert(isalpha(ch));
    coefs[s] = 1;
  }

  Poly(int n): s(std::to_string(n)) {
    if (n)
      coefs[""] = n;
  }

  Poly &operator +=(const Poly &oth) {
    for (const auto &p: oth.coefs) {
      const std::string &param=p.first;
      int coef=p.second;
      coefs[param] += coef;
    }
    return normalized();
  }

  Poly &operator -=(const Poly &oth) {
    for (const auto &p: oth.coefs) {
      const std::string &param=p.first;
      int coef=p.second;
      coefs[param] -= coef;
    }
    return normalized();
  }

  Poly &operator *=(const Poly &oth) {
    Poly tmp(*this);
    coefs.clear();
    for (const auto &pl: tmp.coefs) {
      for (const auto &pr: oth.coefs) {
        std::string param=pl.first+pr.first;
        std::sort(param.begin(), param.end());
        coefs[param] += pl.second*pr.second;
      }
    }
    return normalized();
  }

  Poly &operator ^=(int iexp) {
    if (iexp == 0) {
      // does not appear in #1233
      coefs.clear();
      coefs[""] = 1;  // means 1
    } else {
      Poly tmp(*this);
      for (int i=1; i<iexp; ++i)
        // O(iexp) multiplications, ugh
        *this *= tmp;
    }
    return normalized();
  }

  Poly operator +(const Poly &oth) const { return Poly(*this) += oth; }
  Poly operator -(const Poly &oth) const { return Poly(*this) -= oth; }
  Poly operator *(const Poly &oth) const { return Poly(*this) *= oth; }
  Poly operator ^(int iexp) const { return Poly(*this) ^= iexp; }
  bool operator ==(const Poly &oth) const { return coefs == oth.coefs; }

  void debug() const {
    if (coefs.empty()) {
      fprintf(stderr, "#> 0\n");
      return;
    }
    fprintf(stderr, "#>");
    for (const auto &p: coefs)
      fprintf(stderr, " %d%s", p.second, p.first.c_str());
    fprintf(stderr, "\n");
  }
};

std::string normalize(const std::string &s) {
  // explicit `*' signs and no spaces
  std::string res;
  enum {
    NONE, PAREN, FACTOR, OPERATOR
  } last=NONE;
  for (size_t i=0; i<s.length(); ++i) {
    if (s[i] == '(') {
      if (last == FACTOR) {
        res += '*';
      }
      res += '(';
      last = PAREN;
    } else if (s[i] == ')') {
      res += ')';
      last = FACTOR;
    } else if (isalnum(s[i])) {
      if (last == FACTOR) {
        res += '*';
      }
      res += s[i];
      if (isdigit(s[i])) {
        while (i+1 < s.length() && isdigit(s[i+1]))
          res += s[++i];
      }
      last = FACTOR;
    } else if (!isspace(s[i])) {
      res += s[i];
      last = OPERATOR;
    }
  }
  fprintf(stderr, "#< %s\n", res.c_str());
  return res;
}

int testcase_ends() {
  char buf[96];
  fgets(buf, sizeof buf, stdin);
  std::string s=buf;
  if (s == ".\n")
    return 1;

  s = normalize(s);
  Poly ps(s);
  ps.debug();
  while (true) {
    fgets(buf, sizeof buf, stdin);
    std::string t=buf;
    if (t == ".\n") {
      printf(".\n");
      return 0;
    }

    t = normalize(t);
    Poly pt(t);
    pt.debug();
    printf("%s\n", (ps == pt)? "yes":"no");
  }
}

int main() {
  while (!testcase_ends()) {}
}
accuracy: 1
time_ms: 80
memory_kb: 3112
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct term {
    int coff;
    vector<int> deg;

    term() : coff(1), deg(26, 0) {}

    bool operator<(const term& other) const {
        return deg < other.deg;
    }
    bool operator==(const term& other) const {
        return coff == other.coff && deg == other.deg;
    }
};

struct expression {
    void add_term(term new_term) {
        for(auto& t : terms) {
            if(t.deg == new_term.deg) {
                t.coff += new_term.coff;
                return;
            }
        }
        terms.push_back(move(new_term));
        sort(begin(terms), end(terms));
        clean_up();
    }

    void add_expr(expression e) {
        for(auto& t : e.terms) {
            add_term(t);
        }
        clean_up();
    }

    expression operator-() const {
        expression res;
        res.terms = terms;
        for(auto& t : res.terms) {
            t.coff = -t.coff;
        }
        return res;
    }

    bool operator==(expression const& other) const {
        return terms == other.terms;
    }

    void clean_up() {
        terms.erase(remove_if(begin(terms), end(terms),
                              [](const auto& t1) {
                                  return t1.coff == 0;
                              }),
                    end(terms));
    }

    void debug_print() const {
        cout << "sz: " << terms.size() << endl;
        for(auto& t : terms) {
            cout << "coff: " << t.coff << " deg: ";
            for(int i = 0; i < 26; ++i) {
                cout << t.deg[i] << ' ';
            }
            cout << endl;
        }
    }

    vector<term> terms;
};

term operator*(term a, term const& rhs) {
    a.coff *= rhs.coff;
    for(int i = 0; i < 26; ++i) {
        a.deg[i] += rhs.deg[i];
    }
    return a;
}

expression operator*(expression const& lhs, expression const& rhs) {
    expression res;
    for(auto const& t1 : lhs.terms) {
        for(auto const& t2 : rhs.terms) {
            res.add_term(t1 * t2);
        }
    }
    return res;
}


expression parse_expr(string const& s, int& p);
expression parse_term(string const& s, int& p);
expression parse_factor(string const& s, int& p);
void skip_sp(string const& s, int& p);
int number(string const& s, int& p);

expression parse_expr(string const& s, int& p) {
    auto e = parse_term(s, p);
    skip_sp(s, p);
    while(p < (int)s.size() && (s[p] == '+' || s[p] == '-')) {
        const char op = s[p++];
        skip_sp(s, p);
        auto t2 = parse_term(s, p);
        if(op == '-') {
            t2 = -t2;
        }
        e.add_expr(t2);
        skip_sp(s, p);
    }
    return e;
}

expression parse_term(string const& s, int& p) {
    auto e = parse_factor(s, p);
    skip_sp(s, p);
    while(p < (int)s.size() && s[p] != '+' && s[p] != '-' && s[p] != ')') {
        auto f = parse_factor(s, p);
        e = e * f;
    }
    return e;
}

expression parse_factor(string const& s, int& p) {
    expression res;
    skip_sp(s, p);

    if(s[p] == '(') {
        skip_sp(s, ++p);
        auto e = parse_expr(s, p);
        res = e;
        skip_sp(s, p);
        assert(s[p] == ')');
        skip_sp(s, ++p); // )
        if(p < (int)s.size() && s[p] == '^') {
            skip_sp(s, ++p);
            int d = number(s, p); // todo: only "^ (number)" ?
            for(int i = 0; i < d - 1; ++i) {
                res = res * e;
            }
        }
        skip_sp(s, p);
    } else if(isdigit(s[p])) {
        const int n = number(s, p);
        term t;
        t.coff = n;
        res.add_term(t);
        skip_sp(s, p);
    } else if(isalpha(s[p])) {
        const int idx = s[p++] - 'a';
        term t;
        t.deg[idx] = 1;
        skip_sp(s, p);
        if(p < (int)s.size() && s[p] == '^') {
            skip_sp(s, ++p);
            t.deg[idx] *= number(s, p);
            skip_sp(s, p);
        }
        res.add_term(t);
    } else {
        cout << "str: " << s << "  p: " << p << " s[p]: " << s[p] << endl;
        assert(false);
    }

    return res;
}

int number(string const& s, int& p) {
    assert(isdigit(s[p]));
    int res = 0;
    while(p < (int)s.size() && isdigit(s[p])) {
        res *= 10;
        res += s[p++] - '0';
    }
    return res;
}

void skip_sp(string const& s, int& p) {
    while(p < (int)s.size() && s[p] == ' ') {
        p++;
    }
}

expression parse(string const& s) {
    int p = 0;
    return parse_expr(s, p);
}


int main() {
    string s;
    while(getline(cin, s), s[0] != '.') {
        if(s.back() == '\r') s.pop_back();
        const auto target = parse(s);
        string t;
        while(getline(cin, t), t[0] != '.') {
            if(t.back() == '\r') t.pop_back();
            const auto ss = parse(t);
            if(target == ss) {
                cout << "yes" << endl;
            } else {
                cout << "no" << endl;
            }
        }
        cout << "." << endl;
    }
}
accuracy: 1
time_ms: 420
memory_kb: 3372
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;

using K = string;
using M = map<K, int>;

K mul(K k1, K k2){
	k1 += k2;
	sort(k1.begin(), k1.end());
	return k1;
}

M add(M m1, M m2){
	for(auto v : m2){
		m1[v.first] += v.second;
	}
	return m1;
}

M mul(M m1, M m2){
	M ret;
	for(const auto& v1 : m1){
		for(const auto& v2 : m2){
			K k = mul(v1.first, v2.first);
			ret[k] += v1.second * v2.second;
		}
	}
	return ret;
}

M pow(M m, int x){
	M ret;
	ret[""] = 1;
	M a = m;
	while(x){
		if(x & 1) ret = mul(ret, a);
		x /= 2;
		a = mul(a, a);
	}
	return ret;
}

int c;
int N;
string s;

M exp();
M term();
M fact();

void skip(){
	while(c < N && (s[c] == ' ')) c++;
}

M exp(){
	skip();
	M t1 = term();

	skip();
	while(c < N && (s[c] == '+' || s[c] == '-')){
		char ch = s[c];
		c++;
		skip();
		M t2 = term();
		if(ch == '-'){
			for(auto& v : t2){
				v.second *= -1;
			}
		}
		t1 = add(t1, t2);
	}
	return t1;
}

M term(){
	skip();
	M f1 = fact();
	skip();
	while(c < N && (isalnum(s[c]) || s[c] == '(')){
		M f2 = fact();
		skip();
		f1 = mul(f1, f2);
	}
	return f1;
}

M fact(){
	skip();
	M r;
	if(s[c] == '('){
		c++;
		r = exp();
		c++;
		skip();
	}
	else if(isalpha(s[c])){
		K k;
		k += s[c];
		c++;
		r[k]++;
	}
	else if(isdigit(s[c])){
		int x = 0;
		while(c < N && isdigit(s[c])){
			x = x * 10 + s[c] - '0';
			c++;
		}
		r[""] += x;
	}
	skip();
	if(c < N && s[c] == '^'){
		c++;
		skip();
		int x = s[c] - '0';
		c++;
		skip();
		r = pow(r, x);
	}
	return r;
}

M f(string t){
	c = 0;
	s = t;
	N = s.size();
	return exp();
}

M filter(M m){
	M ret;
	for(auto v : m){
		if(v.second == 0) continue;
		ret[v.first] = v.second;
	}
	return ret;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
#ifdef LOCAL
	std::ifstream in("in");
	std::cin.rdbuf(in.rdbuf());
#endif

	//auto ret = f("a- (b-c)+2");

	string x, y;
	while(getline(cin, x), x != "."){
		M xm = filter(f(x));
		while(getline(cin, y), y != "."){
			auto res = filter(f(y));
			if(xm == res){
				cout << "yes" << endl;
			}
			else{
				cout << "no" << endl;
			}
		}
		cout << "." << endl;
	}
}
accuracy: 1
time_ms: 90
memory_kb: 3380
final_rank: 6

code_snippet: #include <bits/stdc++.h>
#define int long long
#define r(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
using namespace std;
string s,t;
int p;
map<char,int>m;
void convertS(){
    r(i,s.size()){
        if(i&&s[i]==' '){
            if(s[i-1]==')'&&s[i+1]=='(')s[i]='*';
            else if(isalpha(s[i-1])&&isalpha(s[i+1]))s[i]='*';
            else if(s[i-1]==')'&&isalpha(s[i+1]))s[i]='*';
            else if(isalpha(s[i-1])&&s[i+1]=='(')s[i]='*';
            else if(s[i-1]==')'&&isdigit(s[i+1]))s[i]='*';
            else if(isdigit(s[i-1])&&s[i+1]=='(')s[i]='*';
            else if(isdigit(s[i-1])&&isalpha(s[i+1]))s[i]='*';
            else if(isalpha(s[i-1])&&isdigit(s[i+1]))s[i]='*';
            else if(isdigit(s[i-1])&&isdigit(s[i+1]))s[i]='*';
            else s.erase(s.begin()+i--);
        }
        if(isdigit(s[i])&&isalpha(s[i+1]))s.insert(s.begin()+i+1,'*');
        if(isalpha(s[i])&&isalpha(s[i+1]))s.insert(s.begin()+i+1,'*');
        if(isalpha(s[i])&&isdigit(s[i+1]))s.insert(s.begin()+i+1,'*');
        if(s[i]==')'&&isalpha(s[i+1]))s.insert(s.begin()+i+1,'*');
        if(s[i]==')'&&isdigit(s[i+1]))s.insert(s.begin()+i+1,'*');
        if(s[i+1]=='('&&isalpha(s[i]))s.insert(s.begin()+i+1,'*');
        if(s[i+1]=='('&&isdigit(s[i]))s.insert(s.begin()+i+1,'*');
        if(s[i+1]=='('&&s[i]==')')s.insert(s.begin()+i+1,'*');
    }
}
void convertT(){
    r(i,t.size()){
        if(i&&t[i]==' '){
            if(t[i-1]==')'&&t[i+1]=='(')t[i]='*';
            else if(isalpha(t[i-1])&&isalpha(t[i+1]))t[i]='*';
            else if(t[i-1]==')'&&isalpha(t[i+1]))t[i]='*';
            else if(isalpha(t[i-1])&&t[i+1]=='(')t[i]='*';
            else if(t[i-1]==')'&&isdigit(t[i+1]))t[i]='*';
            else if(isdigit(t[i-1])&&t[i+1]=='(')t[i]=='*';
            else if(isdigit(t[i-1])&&isalpha(t[i+1]))t[i]='*';
            else if(isalpha(t[i-1])&&isdigit(t[i+1]))t[i]='*';
            else if(isdigit(t[i-1])&&isdigit(t[i+1]))t[i]='*';
            else t.erase(t.begin()+i--);
        }
        if(isdigit(t[i])&&isalpha(t[i+1]))t.insert(t.begin()+i+1,'*');
        if(isalpha(t[i])&&isalpha(t[i+1]))t.insert(t.begin()+i+1,'*');
        if(isalpha(t[i])&&isdigit(t[i+1]))t.insert(t.begin()+i+1,'*');
        if(t[i]==')'&&isalpha(t[i+1]))t.insert(t.begin()+i+1,'*');
        if(t[i]==')'&&isdigit(t[i+1]))t.insert(t.begin()+i+1,'*');
        if(t[i+1]=='('&&isalpha(t[i]))t.insert(t.begin()+i+1,'*');
        if(t[i+1]=='('&&isdigit(t[i]))t.insert(t.begin()+i+1,'*');
        if(t[i+1]=='('&&t[i]==')')t.insert(t.begin()+i+1,'*');
    }
}
int Sbnf1();
int get_S(){
    int res=0;
    if(s[p]=='(')p++,res=Sbnf1(),p++;
    else if(isdigit(s[p])){
        while(isdigit(s[p])){
            res=res*10+(s[p++]-'0');
        }
    }
    else res=m[s[p++]];
    return res%mod;
}
int Sbnf3(){
    int res=get_S();
    while(s[p]=='^'){
        p++;
        int ret=get_S(),kit=res;
        r(i,ret-1){
            res*=kit;
            res=res%mod;
        }
    }
    return res%mod;
}
int Sbnf2(){
    int res=Sbnf3();
    while(s[p]=='*'){
        p++;
        res*=Sbnf3();
        res=res%mod;
    }
    return res%mod;
}
int Sbnf1(){
    int res=Sbnf2();
    while(s[p]=='+'||s[p]=='-'){
        int pp=p++;
        if(s[pp]=='+')res+=Sbnf2();
        if(s[pp]=='-')res-=Sbnf2();
        while(res<0)res+=mod;
        res=res%mod;
    }
    return res%mod;
}
int Tbnf1();
int get_T(){
    int res=0;
    if(t[p]=='(')p++,res=Tbnf1(),p++;
    else if(isdigit(t[p])){
        while(isdigit(t[p])){
            res=res*10+(t[p++]-'0');
        }
    }
    else res=m[t[p++]];
    return res%mod;
}
int Tbnf3(){
    int res=get_T();
    while(t[p]=='^'){
        p++;
        int ret=get_T(),kit=res;
        r(i,ret-1){
            res*=kit;
            res=res%mod;
        }
    }
    return res%mod;
}
int Tbnf2(){
    int res=Tbnf3();
    while(t[p]=='*'){
        p++;
        res*=Tbnf3();
        res=res%mod;
    }
    return res%mod;
}
int Tbnf1(){
    int res=Tbnf2();
    while(t[p]=='+'||t[p]=='-'){
        int pp=p++;
        if(t[pp]=='+')res+=Tbnf2();
        if(t[pp]=='-')res-=Tbnf2();
        while(res<0)res+=mod;
        res=res%mod;
    }
    return res%mod;
}
main(){
    while(getline(cin,t)){
        if(t==".")break;
        convertT();
        while(getline(cin,s)){
            if(s=="."){
                cout<<'.'<<endl;
                break;
            }
            int flag = 0;
            convertS();
            r(i,100){
                r(j,26)m[j+'a']=rand()%mod;
                p=0;
                int y=Tbnf1()%mod;
                p=0;
                int x=Sbnf1()%mod;
                if(y!=x)flag++;
            }
            cout<<(flag?"no":"yes")<<endl;
        }
    }
}
accuracy: 1
time_ms: 70
memory_kb: 3140
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
 
string s;
int p;
 
ll A[200][300];
int idx;
 
ll bnf();
 
ll get_num(){
 
  if(s[p]=='('){
     
    p++;
 
    ll res=bnf();
     
    p++;
 
    return res;
  }
   
  if('a'<=s[p]&&s[p]<='z') return A[idx][s[p++]];
   
  ll num=0;
   
  while('0'<=s[p]&&s[p]<='9') num=num*10+s[p]-'0', p++;
 
  return num;
   
}
 
ll bnf3(){
 
  ll res=get_num();
   
  while(p<s.size()){
 
    if(s[p]=='^'){
 
      p++;
 
      ll r=get_num();
 
      ll tmp=1;
 
      for(int i=0;i<r;i++) tmp*=res;
 
      res=tmp;
       
    }else break;
     
  }
   
  return res;
}
 
ll bnf2(){
 
  ll res=bnf3();
 
  while(p<s.size()){
 
    if(s[p]=='*'){
 
      p++;
 
      ll r=bnf3();
       
      res*=r;
       
    }else break;
     
  }
   
  return res;
}
 
ll bnf(){
 
  ll res=bnf2();
   
  while(p<s.size()){
     
    if(s[p]=='+'){
 
      p++;
       
      ll r=bnf2();
       
      res+=r;
       
    }
    else if(s[p]=='-'){
 
      p++;
 
      ll r=bnf2();
 
      res-=r;
       
    }else break;
     
  }
   
  return res;
}
 
void change(){
   
  for(int i=0;i<s.size();i++)
    if((s[i]==')'||('a'<=s[i]&&s[i]<='z')||('0'<=s[i]&&s[i]<='9')))
      if((s[i+1]=='('||('a'<=s[i+1]&&s[i+1]<='z')||('0'<=s[i+1]&&s[i+1]<='9')))
    if(!('0'<=s[i]&&s[i]<='9'&&'0'<=s[i+1]&&s[i+1]<='9'))
      s=s.substr(0,i+1)+'*'+s.substr(i+1);
 
   
  for(int i=0;i<s.size();i++)
     
    if(s[i]==' ')
       
      while(s[i]==' '&&i+1<s.size()&&s[i+1]==' ') s.erase(s.begin()+i);
   
 
  if(s[0]==' ') s.erase(s.begin());
   
  if(s[s.size()-1]==' ') s.erase(s.begin()+s.size()-1);
 
  for(int i=0;i<s.size();i++)
    if(s[i]==' ')
      if((s[i-1]==')'||('a'<=s[i-1]&&s[i-1]<='z')||('0'<=s[i-1]&&s[i-1]<='9')))
    if((s[i+1]=='('||('a'<=s[i+1]&&s[i+1]<='z')||('0'<=s[i+1]&&s[i+1]<='9'))) s[i]='*';
 
  for(int i=0;i<s.size();i++)
     
    if(s[i]==' ') s.erase(s.begin()+i), i--;
   
   
}
 
int main(){
   
  for(int T=0;T<200;T++)
     
    for(int i=0;i<300;i++) A[T][i]=rand();
   
  while(1){
 
    string tmp=s;
     
    getline(cin,s);
     
    if(s==".") break;
     
    change();
     
    ll base[200];
 
    for(int T=0;T<200;T++){
      idx=T;
      p=0;
      base[T]=bnf();
    }
     
    while(1){
       
      getline(cin,s);
       
      if(s=="."){
    cout<<"."<<endl;
    break;
      }
       
      change();
       
      bool ans=true;
       
      for(int T=0;T<200;T++){
           
    idx=T;  
    p=0;
       
    ll r=bnf();
     
    if(base[T]!=r) ans=false;
     
      }
       
      if(ans) cout<<"yes"<<endl;
      else cout<<"no"<<endl;
       
    }
     
  }
   
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3640
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<string,int> P;
typedef map<string,int> M;

M add(M a,M b){
  for(P p2:b) a[p2.first] += p2.second;
  return a;
}

M Minus(M a,M b){
  for(P p2:b) a[p2.first] -= p2.second;
  return a;
}
 
M mult(M a,M b){
  M res;
  for(P p1:a)
    for(P p2:b){
      string s = p1.first + p2.first; sort(s.begin(),s.end());
      int num = p1.second * p2.second;
      res[s] += num;
    }
  return res;
}
 
M bnf();
int pos;
string S;
void skipsp(){while(S[pos] == ' ') pos++;}
 
int getNum(){
  int res = 0;
  while(isdigit(S[pos])) res = res * 10 + S[pos++]-'0';
  return res;
}
 
int getNum2(const string &S,int &pos){
  int res = 0;
  while(isdigit(S[pos])) res = res * 10 + S[pos++]-'0';
  return res;
}

string getBlock(){
  string res;
  skipsp();
  while(isdigit(S[pos]) || isalpha(S[pos])) res += S[pos++];
  skipsp();
  if(S[pos] == '^') {
    pos++;
    skipsp();
    assert(isdigit(S[pos])&&"hut");
    int p = getNum();
    for(int i=0;i<p-1;i++) res += res.back();
  }
  return res;
}
 
M BtoM(string b){
  M res;
  int num = 1;
  string val;
  for(int i=0;i<(int)b.size();i++){
    char ch = b[i];
    if(isalpha(ch)) val += ch;
    else if(isdigit(ch)) num *= getNum2(b,i),i--;
    else assert(ch == ' ' &&  "BtoM");
  }
  res[val] = num;
  return res;
}
 
M calc(){
  M res; res[""] = 1;
  while(1){
    skipsp();
    char ch = S[pos];
    if(ch == '(') pos++,res = mult(res,bnf()),pos++;
    else if(isalpha(ch) || isdigit(ch)){
      string block = getBlock();
      res = mult(res,BtoM(block));
    }
    else if(ch == '^') assert(!"^");
    else break;
  }
  return res;
}
 
M bnf(){
  M res = calc();
  while(1){
    skipsp();
    char ch = S[pos];
    if(isalpha(ch) || isdigit(ch)) res = mult(res,calc());
    else if(ch == '+')pos++,res = add(res,calc());
    else if(ch == '-')pos++,res = Minus(res,calc());
    else if(ch == '(') pos++,res = mult(res,bnf()),pos++;
    else if(ch == '^') assert(!"^");
    else break;
  }
  for(auto it=res.begin();it!=res.end();it++) if(it->second == 0) res.erase(it);
  return res;
}
 
signed main(){
  while(1){
    getline(cin,S);
    if(S == ".") break;
    pos = 0;
    M ori = bnf();
    while(1){
      getline(cin,S);
      if(S == ".") break;
      pos = 0;
      M res = bnf();
      cout<<((ori == res)? "yes":"no")<<endl;
    }
    cout<<"."<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3444
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<string,int> P;
typedef map<string,int> M;
 
void pr(M a,string s=""){
  return;
  cout<<s<<endl;
  for(P p:a) cout<<p.first<<" "<<p.second<<endl;
  cout<<"end"<<endl;
}
 
M add(M a,M b,int f=1){
  for(P p2:b) a[p2.first] += f * p2.second;
  return a;
}
 
M mult(M a,M b){
  M res;
  for(P p1:a)
    for(P p2:b){
      string s = p1.first + p2.first; sort(s.begin(),s.end());
      int num = p1.second * p2.second;
      res[s] += num;
    }
  return res;
}
 
 
M bnf();
int pos;
string S;
void skipsp(){while(S[pos] == ' ') pos++;}
 
int getNum(){
  int res = 0;
  while((int)S.size()> pos && isdigit(S[pos])) res = res * 10 + S[pos++]-'0';
  return res;
}
 
int getNum2(const string &S,int &pos){
  int res = 0;
  while((int)S.size()>pos && isdigit(S[pos])) res = res * 10 + S[pos++]-'0';
  return res;
}
 
string getBlock(){
  string res;
  skipsp();
  while(isdigit(S[pos]) || isalpha(S[pos])) res += S[pos++];
  skipsp();
  if(S[pos] == '^') {
    pos++;
    skipsp();
    assert(isdigit(S[pos])&&"hut");
    int p = getNum();
    for(int i=0;i<p-1;i++) res += res.back();
  }
  return res;
}
 
M BtoM(string b){
  M res;
  int num = 1;
  string val;
  for(int i=0;i<(int)b.size();i++){
    char ch = b[i];
    if(isalpha(ch)) val += ch;
    else if(isdigit(ch)) num *= getNum2(b,i),i--;
    else if(ch != ' ') assert(!"BtoM");
  }
  res[val] = num;
  return res;
}
 
M calc(){
  M res; res[""] = 1;
  while(1){
    skipsp();
    char ch = S[pos];
    if(ch == '(') pos++,res = mult(res,bnf()),pos++;
    else if(isalpha(ch) || isdigit(ch)){
      string block = getBlock();
      res = mult(res,BtoM(block));
    }
    else if(ch == '^') assert(!"^");
    else break;
  }
  pr(res,"calc,res");
  return res;
}
 
M bnf(){
  M res = calc();
  pr(res,"start bnf");
  while(1){
    skipsp();
    char ch = S[pos];
    if(isalpha(ch) || isdigit(ch)) res = mult(res,calc());
    else if(ch == '+')pos++,res = add(res,calc());
    else if(ch == '-')pos++,res = add(res,calc(),-1);
    else if(ch == '(') pos++,res = mult(res,bnf()),pos++;
    else if(ch == '^') assert(!"^");
    else break;
  }
   
  for(auto it=res.begin();it!=res.end();it++) if(it->second==0) res.erase(it);
  pr(res,"return bnf");
  return res;
}
 
signed main(){
  while(1){
    getline(cin,S);
    //cin.ignore();
    if(S == ".") break;
    pos = 0;
    M ori = bnf();
    pr(ori,"ori");
    while(1){
      getline(cin,S);
      if(S == ".") break;
      pos = 0;
      M res = bnf();
      cout<<((ori == res)? "yes":"no")<<endl;
      pr(res,S);
    }
    cout<<"."<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3428
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;

string s;
int p;

ll A[200][300];
int idx;

ll bnf();

ll get_num(){

  if(s[p]=='('){
    
    p++;

    ll res=bnf();
    
    p++;

    return res;
  }
  
  if('a'<=s[p]&&s[p]<='z') return A[idx][s[p++]];
  
  ll num=0;
  
  while('0'<=s[p]&&s[p]<='9') num=num*10+s[p]-'0', p++;

  return num;
  
}

ll bnf3(){

  ll res=get_num();
  
  while(p<s.size()){

    if(s[p]=='^'){

      p++;

      ll r=get_num();

      ll tmp=1;

      for(int i=0;i<r;i++) tmp*=res;

      res=tmp;
      
    }else break;
    
  }
  
  return res;
}

ll bnf2(){

  ll res=bnf3();

  while(p<s.size()){

    if(s[p]=='*'){

      p++;

      ll r=bnf3();
      
      res*=r;
      
    }else break;
    
  }
  
  return res;
}

ll bnf(){

  ll res=bnf2();
  
  while(p<s.size()){
    
    if(s[p]=='+'){

      p++;
      
      ll r=bnf2();
      
      res+=r;
      
    }
    else if(s[p]=='-'){

      p++;

      ll r=bnf2();

      res-=r;
      
    }else break;
    
  }
  
  return res;
}

void change(){
  
  for(int i=0;i<s.size();i++)
    if((s[i]==')'||('a'<=s[i]&&s[i]<='z')||('0'<=s[i]&&s[i]<='9')))
      if((s[i+1]=='('||('a'<=s[i+1]&&s[i+1]<='z')||('0'<=s[i+1]&&s[i+1]<='9')))
	if(!('0'<=s[i]&&s[i]<='9'&&'0'<=s[i+1]&&s[i+1]<='9'))
	  s=s.substr(0,i+1)+'*'+s.substr(i+1);

  
  for(int i=0;i<s.size();i++)
    
    if(s[i]==' ')
      
      while(s[i]==' '&&i+1<s.size()&&s[i+1]==' ') s.erase(s.begin()+i);
  

  if(s[0]==' ') s.erase(s.begin());
  
  if(s[s.size()-1]==' ') s.erase(s.begin()+s.size()-1);

  for(int i=0;i<s.size();i++)
    if(s[i]==' ')
      if((s[i-1]==')'||('a'<=s[i-1]&&s[i-1]<='z')||('0'<=s[i-1]&&s[i-1]<='9')))
	if((s[i+1]=='('||('a'<=s[i+1]&&s[i+1]<='z')||('0'<=s[i+1]&&s[i+1]<='9'))) s[i]='*';

  for(int i=0;i<s.size();i++)
    
    if(s[i]==' ') s.erase(s.begin()+i), i--;
  
  
}

int main(){
  
  for(int T=0;T<200;T++)
    
    for(int i=0;i<300;i++) A[T][i]=rand();
  
  while(1){

    string tmp=s;
    
    getline(cin,s);
    
    if(s==".") break;
    
    change();
    
    ll base[200];

    for(int T=0;T<200;T++){
      idx=T;
      p=0;
      base[T]=bnf();
    }
    
    while(1){
      
      getline(cin,s);
      
      if(s=="."){
	cout<<"."<<endl;
	break;
      }
      
      change();
      
      bool ans=true;
      
      for(int T=0;T<200;T++){
          
	idx=T;  
	p=0;
      
	ll r=bnf();
	
	if(base[T]!=r) ans=false;
	
      }
      
      if(ans) cout<<"yes"<<endl;
      else cout<<"no"<<endl;
      
    }
    
  }
  
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3632
final_rank: 10

code_snippet: #include <bits/stdc++.h>
 
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
 
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
 
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
 
// #define DEBUG
 
#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif
 
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
 
using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
 
const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf =1LL << 62;
const ll mod=1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
 
 
ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}
 
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1,6);
uniform_real_distribution<double> score(0.0,10.0);

string f;
int len;
int p = 0;

ll exp();
ll term();
ll fact();

ll tbl['z' - 'a' + 1];
inline ll c2n(char c){
    return tbl[c - 'a'];
}

char next_c(){
    while(p < len and f[p] == ' ') p++;
    return f[p];
}

ll exp(){
    ll ret = term();

    char c;
    while(c = next_c(), (c == '+' or c == '-')){
        p++;
        ll r = term();
        if(c == '+') ret = ADD(ret, r, mod);
        else         ret = SUB(ret, r, mod);
    }

    return ret;
}

const ll ERR = 2LL * mod;

ll term(){
    ll ret = fact();

    ll r;
    while((r = fact()) != ERR){
        ret = MUL(ret, r, mod);
    }

    return ret;
}

inline ll num(){
    ll ret = 0LL;
    next_c();
    while(isdigit(f[p])){
        ret = ret * 10 + (f[p] - '0');
        p++; // [0,9]
    }
    return ret % mod;
} 

ll fact(){
    ll ret = ERR;

    char c = next_c();
    if(c == '('){
        p++; // (
        ret = exp();
        assert(next_c() == ')'); p++;
    }
    else if(islower(c)){
        p++; // [a,z]
        ret = c2n(c);

        c = next_c();
        if(c == '^'){
            p++; // ^
            ll d = num();
            ll b = ret;
            rep(loop, d - 1){
                ret = MUL(ret, b, mod);
            }
        }
    }
    else if(isdigit(c)){
        ret = num();
    }
    else {
        // cerr << "cur... " << c << endl;
    }

    return ret;
}

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    auto myrand = bind(uniform_int_distribution<int>(0, (int)1e6), mt19937(static_cast<unsigned int>(time(nullptr))));

    while(true){
        string mine;
        getline(cin, mine);
        if(mine == ".") break;
        
        while(true){
            string opp;
            getline(cin, opp);
            if(opp == ".") break;

            bool ok = true;
            rep(loop, 1000){
                rep(i, 'z' - 'a' + 1) tbl[i] = myrand();

                // cerr << "-> " << mine << endl;
                f = mine; p = 0; len = f.size(); ll a = exp();
                // cerr << "-> " << opp << endl;
                f =  opp; p = 0; len = f.size(); ll b = exp();

                if(a != b){
                    ok = false;
                    break;
                }
            }

            if(ok){
                cout << "yes" << endl;
            }
            else {
                cout << "no" << endl;
            }
        }
        cout << "." << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3056
final_rank: 1