Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <stack>
- #include <algorithm>
- #include <functional>
- std::string decodeString(std::string s);
- std::string createStringForStack(const std::string &s, const size_t sSize, int leftIdx, std::function<bool(char)> predicate);
- bool is_number(const std::string &s);
- int main()
- {
- std::string s = "3[a2[c]]";
- std::string decodedString = decodeString(s);
- std::cout << decodedString << std::endl;
- }
- std::string decodeString(std::string s)
- {
- std::stack<std::string> st;
- std::stack<std::string> stKeepClosedP;
- std::string result = "";
- std::string currentString = "";
- const size_t sSize = s.size();
- auto predicateStringBuilder = [](char c)
- { return ('a' <= c && 'z' >= c); };
- auto predicateNumberBuilder = [](char c)
- { return ('0' <= c && '9' >= c); };
- for (int i = 0; i < sSize; ++i)
- {
- if (s[i] == ']' || s[i] == '[')
- st.push(std::string{s[i]});
- else if (s[i] >= 'a' && s[i] <= 'z')
- st.push(createStringForStack(s, sSize, i, predicateStringBuilder));
- else
- st.push(createStringForStack(s, sSize, i, predicateNumberBuilder));
- }
- std::string buffer = ""; // change it to ostringstream
- // std::ostringstream oss;
- while (!st.empty())
- {
- currentString = st.top();
- st.pop();
- while (!st.empty() && st.top() == "]")
- {
- st.pop();
- stKeepClosedP.push("]");
- }
- if (currentString[0] == ']')
- {
- buffer = st.top();
- // oss << st.top();
- st.pop();
- st.pop(); // popping the "["
- int bufferTimes = stoi(st.top()); // getting the number into 'stoi' string to integer function
- st.pop(); // remove the number we don't need it anymore.
- st.push(buffer); // just to save new variable
- // st.push(oss.str()); // just to save new variable
- // oss.clear();
- buffer = "";
- for (int i = 0; i < bufferTimes; ++i)
- {
- buffer += st.top();
- // oss << st.top();
- }
- st.pop(); // popping the original string it was
- // std::ostringstream keep;
- while (!st.empty() && st.top() != "[" && st.top() != "]" && !is_number(st.top()))
- {
- // keep << oss.str();
- // oss.clear();
- buffer = st.top() + buffer;
- // oss << st.top();
- // oss << keep.str();
- // keep.clear();
- st.pop();
- }
- // st.push(oss.str());
- // oss.clear();
- st.push(buffer);
- while (!stKeepClosedP.empty())
- {
- stKeepClosedP.pop();
- st.push("]");
- }
- }
- else
- {
- result = buffer + result;
- // result = oss.str() + result;
- }
- }
- return result;
- }
- std::string createStringForStack(const std::string &s, const size_t sSize, int leftIdx, std::function<bool(char)> predicate)
- {
- int rightIdx = leftIdx;
- for (; rightIdx < sSize && predicate(s[rightIdx]); ++rightIdx)
- {
- }
- return s.substr(leftIdx, rightIdx - leftIdx);
- }
- bool is_number(const std::string &s)
- {
- return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement