Guest User

Untitled

a guest
Apr 30th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. enum class type_t {
  9.   string,
  10.   begin,
  11.   end,
  12.   number
  13. };
  14.  
  15. struct lex_t {
  16.   string s;
  17.   size_t x;
  18.   type_t type;
  19.  
  20.   lex_t(type_t type) :
  21.       s(),
  22.       x(),
  23.       type(type) {
  24.   }
  25. };
  26.  
  27. string search(queue<lex_t> &q) {
  28.   string res;
  29.   while (!q.empty()) {
  30.     lex_t lex = q.front();
  31.     q.pop();
  32.     switch (lex.type) {
  33.       case type_t::string:
  34.         res += lex.s;
  35.         break;
  36.       case type_t::begin:
  37.         res += search(q);
  38.         break;
  39.       case type_t::end:
  40.         return res;
  41.         break;
  42.       case type_t::number:
  43.         string buf = search(q);
  44.         for (size_t i = 0; i < lex.x; ++i)
  45.           res += buf;
  46.         return res;
  47.     }
  48.   }
  49.   return res;
  50. }
  51.  
  52. int main() {
  53.   ios_base::sync_with_stdio(false);
  54.   string s;
  55.   cin >> s;
  56.   std::vector<lex_t> lex;
  57.   for (size_t i = 0; i < s.length(); ++i) {
  58.     if (s[i] == '(') {
  59.       lex.push_back(lex_t(type_t::begin));
  60.     } else if (s[i] == ')') {
  61.       lex.push_back(lex_t(type_t::end));
  62.     } else if (s[i] <= '9' && s[i] >= '0') {
  63.       size_t x = s[i] - '0';
  64.       if (lex.empty() || lex.back().type != type_t::number) {
  65.         lex.push_back(lex_t(type_t::number));
  66.         lex.back().x = x;
  67.       } else {
  68.         lex.back().x *= 10;
  69.         lex.back().x += x;
  70.       }
  71.     } else {
  72.       string t;
  73.       t += s[i];
  74.       if (lex.empty() || lex.back().type != type_t::string) {
  75.         lex.push_back(lex_t(type_t::string));
  76.         lex.back().s = t;
  77.       } else {
  78.         lex.back().s += t;
  79.       }
  80.     }
  81.   }
  82.   queue<lex_t> q;
  83.   for (lex_t const &l : lex)
  84.     q.push(l);
  85.   string ans;
  86.   while (!q.empty())
  87.     ans += search(q);
  88.   cout << ans << endl;
  89.   return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment