Advertisement
a53

decodif

a53
Jul 12th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. string str;
  5.  
  6. string decode(string str)
  7. {
  8. stack<int> integerstack;
  9. stack<char> stringstack;
  10. string temp="",result="";
  11. for(unsigned i=0;i<str.length();++i)
  12. {
  13. int cnt=0;
  14. if(str[i]>='0'&&str[i]<='9')
  15. {
  16. while(str[i]>='0'&&str[i]<='9')
  17. cnt=cnt*10+str[i]-'0',++i;
  18. --i;
  19. integerstack.push(cnt);
  20. }
  21. else
  22. if(str[i]==']')
  23. {
  24. temp="";
  25. cnt=0;
  26. if (! integerstack.empty())
  27. {
  28. cnt = integerstack.top();
  29. integerstack.pop();
  30. }
  31. while(!stringstack.empty()&&stringstack.top()!='[')
  32. temp=stringstack.top()+temp,stringstack.pop();
  33. if(!stringstack.empty()&&stringstack.top()=='[')
  34. stringstack.pop();
  35. for(int j=0;j<cnt;++j)
  36. result=result+temp;
  37. for(unsigned j=0;j<result.length();++j)
  38. stringstack.push(result[j]);
  39. result="";
  40. }
  41. else
  42. if(str[i]=='[')
  43. {
  44. if(str[i-1]>='0'&&str[i-1]<='9')
  45. stringstack.push(str[i]);
  46. else
  47. stringstack.push(str[i]),integerstack.push(1);
  48. }
  49. else
  50. stringstack.push(str[i]);
  51. }
  52. while(!stringstack.empty())
  53. result=stringstack.top()+result,stringstack.pop();
  54. return result;
  55. }
  56.  
  57. int main()
  58. {
  59. cin>>str;
  60. cout<<decode(str)<<'\n';
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement