Guest User

Untitled

a guest
Apr 30th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. struct _ { ios_base::Init i; _() { cin.sync_with_stdio(0); cin.tie(0); } } _;
  5.  
  6. class Node
  7. {
  8.     public:
  9.         bool isleaf;
  10.         int times;
  11.         list<Node*> children;
  12.         string content;
  13.  
  14.         Node(int n)
  15.         {
  16.             isleaf = false;
  17.             times = n;
  18.         }
  19.  
  20.         Node(string s)
  21.         {
  22.             isleaf = true;
  23.             content = s;
  24.         }
  25.  
  26.         void push(Node *next)
  27.         {
  28.             children.push_back(next);
  29.         }
  30. };
  31.  
  32. string s;
  33. int n;
  34.  
  35. int create(Node *parent, int pos)
  36. {
  37.     int times = 0;
  38.     while (s[pos] >= '0' && s[pos] <= '9')
  39.         times = times * 10 + s[pos++] - '0';
  40.  
  41.     if (times == 0)
  42.         times = 1;
  43.  
  44.     Node *current = new Node(times);
  45.     parent->push(current);
  46.  
  47.     string curs;
  48.     while (pos < n && s[pos] != ')')
  49.         if (s[pos] != '(')
  50.             curs += s[pos++];
  51.         else
  52.         {
  53.             if (curs.length() > 0)
  54.                 current->push(new Node(curs));
  55.             curs = "";
  56.             pos = create(current, pos + 1);
  57.         }
  58.  
  59.     if (curs.length() > 0)
  60.         current->push(new Node(curs));
  61.  
  62.     return pos + 1;
  63. }
  64.  
  65. void dfs(Node *v)
  66. {
  67.     if (v->isleaf)
  68.         cout << v->content;
  69.     else
  70.         for (int i = 0; i < v->times; ++i)
  71.             for (auto node : v->children)
  72.                 dfs(node);
  73. }
  74.  
  75. Node *head;
  76.  
  77. int main()
  78. {
  79.     cin >> s;
  80.     n = s.length();
  81.  
  82.     head = new Node(1);
  83.     create(head, 0);
  84.  
  85.     dfs(head);
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment