Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Stack["((a($Z"]
- encounter ')'
- Stack["((a"], vector<"Z$">
- reverse vector -> vector<"$Z">
- Parent Node -> N(id='1'), children -> <N(id='$'), N(id='Z')>
- push N(1) to stack
- Stack["((a1]
- encounter ')'
- Stack["("], vector<"1a">
- reverse vector -> vector<"a1">
- Parent Node -> N(id='2'), children -> <N(id='a'), N(id=1)>
- push N(2) to stack
- Stack["(2(An"]
- encounter ')'
- ...
- Node* solve(string s) {
- stack<Node *> st;
- int idx = 0, rt = 1;
- while (idx < s.size()) {
- Node *n = get_node(s[idx]);
- st.push(n);
- ++idx;
- if (st.top()->v == ')') {
- st.pop();
- vector<Node *> child;
- while (st.size() && st.top()->v != '(') {
- child.push_back(st.top()); st.pop();
- }
- if (st.top()->v == '(') st.pop();
- Node *par = get_node('0' + rt++);
- reverse(child.begin(), child.end());
- for (Node *t : child) {
- t->parent = par;
- par->child.push_back(t);
- }
- st.push(par);
- }
- }
- return st.top();
- }
- input: ((a($Zk))(An))
- output:
- Parent: 4: 2 3
- Parent: 2: a 1
- Parent: 3: A n
- Parent: 1: $ Z k
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement