Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- string str;
- string decode(string str)
- {
- stack<int> integerstack;
- stack<char> stringstack;
- string temp="",result="";
- for(unsigned i=0;i<str.length();++i)
- {
- int cnt=0;
- if(str[i]>='0'&&str[i]<='9')
- {
- while(str[i]>='0'&&str[i]<='9')
- cnt=cnt*10+str[i]-'0',++i;
- --i;
- integerstack.push(cnt);
- }
- else
- if(str[i]==']')
- {
- temp="";
- cnt=0;
- if (! integerstack.empty())
- {
- cnt = integerstack.top();
- integerstack.pop();
- }
- while(!stringstack.empty()&&stringstack.top()!='[')
- temp=stringstack.top()+temp,stringstack.pop();
- if(!stringstack.empty()&&stringstack.top()=='[')
- stringstack.pop();
- for(int j=0;j<cnt;++j)
- result=result+temp;
- for(unsigned j=0;j<result.length();++j)
- stringstack.push(result[j]);
- result="";
- }
- 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]);
- }
- while(!stringstack.empty())
- result=stringstack.top()+result,stringstack.pop();
- return result;
- }
- int main()
- {
- cin>>str;
- cout<<decode(str)<<'\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement