Advertisement
yarin0600

Untitled

Nov 15th, 2023
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <stack>
  5. #include <algorithm>
  6. #include <functional>
  7.  
  8. std::string decodeString(std::string s);
  9. std::string createStringForStack(const std::string &s, const size_t sSize, int leftIdx, std::function<bool(char)> predicate);
  10. bool is_number(const std::string &s);
  11.  
  12. int main()
  13. {
  14.    std::string s = "3[a2[c]]";
  15.    std::string decodedString = decodeString(s);
  16.    std::cout << decodedString << std::endl;
  17. }
  18.  
  19. std::string decodeString(std::string s)
  20. {
  21.    std::stack<std::string> st;
  22.    std::stack<std::string> stKeepClosedP;
  23.    std::string result = "";
  24.    std::string currentString = "";
  25.  
  26.    const size_t sSize = s.size();
  27.  
  28.    auto predicateStringBuilder = [](char c)
  29.    { return ('a' <= c && 'z' >= c); };
  30.  
  31.    auto predicateNumberBuilder = [](char c)
  32.    { return ('0' <= c && '9' >= c); };
  33.  
  34.    for (int i = 0; i < sSize; ++i)
  35.    {
  36.       if (s[i] == ']' || s[i] == '[')
  37.          st.push(std::string{s[i]});
  38.       else if (s[i] >= 'a' && s[i] <= 'z')
  39.          st.push(createStringForStack(s, sSize, i, predicateStringBuilder));
  40.       else
  41.          st.push(createStringForStack(s, sSize, i, predicateNumberBuilder));
  42.    }
  43.  
  44.    std::string buffer = ""; // change it to ostringstream
  45.    // std::ostringstream oss;
  46.    while (!st.empty())
  47.    {
  48.       currentString = st.top();
  49.       st.pop();
  50.  
  51.       while (!st.empty() && st.top() == "]")
  52.       {
  53.          st.pop();
  54.          stKeepClosedP.push("]");
  55.       }
  56.  
  57.       if (currentString[0] == ']')
  58.       {
  59.          buffer = st.top();
  60.          // oss << st.top();
  61.  
  62.          st.pop();
  63.          st.pop();                         // popping the "["
  64.          int bufferTimes = stoi(st.top()); // getting the number into 'stoi' string to integer function
  65.          st.pop();                         // remove the number we don't need it anymore.
  66.          st.push(buffer);                  // just to save new variable
  67.          // st.push(oss.str()); // just to save new variable
  68.          // oss.clear();
  69.          buffer = "";
  70.          for (int i = 0; i < bufferTimes; ++i)
  71.          {
  72.             buffer += st.top();
  73.             // oss << st.top();
  74.          }
  75.          st.pop(); // popping the original string it was
  76.  
  77.          // std::ostringstream keep;
  78.          while (!st.empty() && st.top() != "[" && st.top() != "]" && !is_number(st.top()))
  79.          {
  80.             // keep << oss.str();
  81.             // oss.clear();
  82.             buffer = st.top() + buffer;
  83.  
  84.             // oss << st.top();
  85.             // oss << keep.str();
  86.             // keep.clear();
  87.             st.pop();
  88.          }
  89.          // st.push(oss.str());
  90.          // oss.clear();
  91.          st.push(buffer);
  92.  
  93.          while (!stKeepClosedP.empty())
  94.          {
  95.             stKeepClosedP.pop();
  96.             st.push("]");
  97.          }
  98.       }
  99.       else
  100.       {
  101.          result = buffer + result;
  102.          // result = oss.str() + result;
  103.       }
  104.    }
  105.  
  106.    return result;
  107. }
  108.  
  109. std::string createStringForStack(const std::string &s, const size_t sSize, int leftIdx, std::function<bool(char)> predicate)
  110. {
  111.    int rightIdx = leftIdx;
  112.    for (; rightIdx < sSize && predicate(s[rightIdx]); ++rightIdx)
  113.    {
  114.    }
  115.    return s.substr(leftIdx, rightIdx - leftIdx);
  116. }
  117.  
  118. bool is_number(const std::string &s)
  119. {
  120.    return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement