Guest User

KattisAnimalsSlow

a guest
Sep 18th, 2016
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
4CS 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <algorithm>
  4. #include <string>
  5. #include <unordered_set>
  6. #include <deque>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. struct hash_X{
  12.   size_t operator()(const unordered_set<int> &x) const{
  13.     int sum = 0;
  14.     for(int i: x){sum = sum+i;}
  15.     return sum;
  16.   }
  17. };
  18.  
  19. unordered_set<unordered_set<int>, hash_X> mt(list<string> L){
  20.     unordered_set<unordered_set<int>, hash_X> S;
  21.     list<string> K;
  22.     deque<list<string>> Q;
  23.     Q.push_back(L);
  24.     while(Q.size() > 0){
  25.         K = Q.back();
  26.         unordered_set<int> tmp;
  27.         for(string s: K){if( s!="(" and s!=")" ){int i = stoi(s); tmp.insert(i);}}
  28.         S.insert(tmp);
  29.     //K is of the form for example (x(yz)), so first we unwrap ~ x(yz) and split into the two subtrees x and (yz)
  30.     //and add both to the queue. If only one node is left ( x ) we dont add it back to the queue
  31.         if(K.size() >= 2){
  32.             K.pop_front();
  33.             K.pop_back();
  34.             list<string>::iterator it = K.begin();
  35.             if(*it == "("){
  36.                 int i = 1;
  37.                 while(i != 0){
  38.                     it++;
  39.                     if(*it == ")"){i = i-1;}
  40.                     else if(*it == "("){i=i+1;}
  41.                     }
  42.                 }
  43.             it++;
  44.             list<string> B;
  45.             list<string>::iterator itB = B.begin();
  46.             B.splice(itB, K, it, K.end());
  47.             Q.push_front(K);
  48.             Q.push_front(B);
  49.         }
  50.         Q.pop_back();
  51.     }
  52.     return S;
  53. }
  54.  
  55.  
  56. int main(){
  57.     int n;
  58.     string x, y;
  59.     char c;
  60.     list<string> A, B;
  61.     cin >> n;
  62.     cin.ignore();
  63.     cin >> x;
  64.     cin >> y;
  65.     // parse ((3,(1,(5,2))),4) into ((3(1(52)))4)
  66.     for(int i = 0; i < x.size(); i++){
  67.         string c = "";
  68.         c += x[i];
  69.         if( c == "(" or c == ")" ){A.push_back(c);}
  70.         if( isdigit(x[i]) ){
  71.             string tmp = "";
  72.             while( isdigit(x[i]) ){
  73.                 tmp += x[i];
  74.                 i++;
  75.             }
  76.             i--;
  77.             A.push_back(tmp);
  78.         }
  79.     }
  80.     for(int i = 0; i <= y.size(); i++){
  81.         string c = "";
  82.         c += y[i];
  83.         if( c == "(" or c == ")" ){B.push_back(c);}
  84.         if( isdigit(y[i]) ){
  85.             string tmp = "";
  86.             while( isdigit(y[i]) ){
  87.                 tmp += y[i];
  88.                 i++;
  89.             }
  90.             i--;
  91.             B.push_back(tmp);
  92.         }
  93.     }
  94.  
  95.     // make the trees
  96.     unordered_set<unordered_set<int>, hash_X> t1 = mt(A);
  97.     unordered_set<unordered_set<int>, hash_X> t2 = mt(B);
  98.     unordered_set<unordered_set<int>, hash_X> i(t1.begin(), t1.end());
  99.     int res = count_if(t2.begin(), t2.end(), [&](unordered_set<int> k) {return i.find(k) != i.end();});
  100.     cout << res;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment