Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stack>
- #include <iostream>
- using namespace std;
- class Decoding{
- public:
- string decodingUtil(string str);
- stack<int> integerstack;
- stack<char> stringstack;
- string temp = "", result = "";
- };
- // Returns decoded string for 'str'
- string decode(string str)
- {
- stack<int> integerstack;
- stack<char> stringstack;
- string temp = "", result = "";
- int i = 0;
- // Traversing the string
- while( i < str.length() )
- {
- int count = 0;
- // If number, convert it into number
- // and push it into integerstack.
- if (str[i] >= '0' && str[i] <='9')
- {
- while (str[i] >= '0' && str[i] <= '9')
- {
- count = count * 10 + str[i] - '0';
- i++;
- }
- i--;
- integerstack.push(count);
- }
- // If closing bracket ']', pop elemment until
- // '[' opening bracket is not found in the
- // character stack.
- else if (str[i] == ')')
- {
- temp = "";
- count = 0;
- if (! integerstack.empty())
- {
- count = integerstack.top();
- integerstack.pop();
- }
- while (! stringstack.empty() && stringstack.top()!='(' )
- {
- temp = stringstack.top() + temp;
- stringstack.pop();
- }
- if (! stringstack.empty() && stringstack.top() == '(')
- stringstack.pop();
- // Repeating the popped string 'temo' count
- // number of times.
- for (int j = 0; j < count; j++)
- result = result + temp;
- // Push it in the character stack.
- for (int j = 0; j < result.length(); j++)
- stringstack.push(result[j]);
- result = "";
- }
- // If '[' opening bracket, push it into character stack.
- else if (str[i] == '(')
- {
- if (str[i-1] >= '0' && str[i-1] <= '9')
- stringstack.push(str[i]);
- else
- {
- stringstack.push(str[i]);
- integerstack.push(1);
- }
- }
- else
- stringstack.push(str[i]);
- i++;
- }
- // Pop all the elmenet, make a string and return.
- while (! stringstack.empty())
- {
- result = stringstack.top() + result;
- stringstack.pop();
- }
- return result;
- }
- // Driven Program
- int main()
- {
- string str;
- cout << "Enter a desired string: " << endl;
- cin >> str;
- cout << decode(str) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement