Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. Stack["((a($Z"]
  2. encounter ')'
  3. Stack["((a"], vector<"Z$">
  4. reverse vector -> vector<"$Z">
  5. Parent Node -> N(id='1'), children -> <N(id='$'), N(id='Z')>
  6. push N(1) to stack
  7.  
  8. Stack["((a1]
  9. encounter ')'
  10. Stack["("], vector<"1a">
  11. reverse vector -> vector<"a1">
  12. Parent Node -> N(id='2'), children -> <N(id='a'), N(id=1)>
  13. push N(2) to stack
  14.  
  15. Stack["(2(An"]
  16. encounter ')'
  17. ...
  18.  
  19. Node* solve(string s) {
  20. stack<Node *> st;
  21. int idx = 0, rt = 1;
  22. while (idx < s.size()) {
  23. Node *n = get_node(s[idx]);
  24. st.push(n);
  25. ++idx;
  26.  
  27. if (st.top()->v == ')') {
  28. st.pop();
  29. vector<Node *> child;
  30. while (st.size() && st.top()->v != '(') {
  31. child.push_back(st.top()); st.pop();
  32. }
  33. if (st.top()->v == '(') st.pop();
  34.  
  35. Node *par = get_node('0' + rt++);
  36. reverse(child.begin(), child.end());
  37. for (Node *t : child) {
  38. t->parent = par;
  39. par->child.push_back(t);
  40. }
  41. st.push(par);
  42. }
  43. }
  44. return st.top();
  45. }
  46.  
  47. input: ((a($Zk))(An))
  48. output:
  49. Parent: 4: 2 3
  50. Parent: 2: a 1
  51. Parent: 3: A n
  52. Parent: 1: $ Z k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement