Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string nstr;
  5. string decode(string str, int *i)
  6. {
  7. string retval;
  8. while (*i < str.length() && str[*i] != ']')
  9. {
  10. if (!isdigit(str[*i]))
  11. {
  12. if (str[*i] == '[')
  13. {
  14. int n = stoi(nstr);
  15. nstr = "";
  16. *i += 1;
  17. string istr = decode(str, i);
  18. *i += 1;
  19. while (n-- > 0)
  20. {
  21. retval += istr;
  22. }
  23. }
  24. else
  25. retval += str[(*i)++];
  26. }
  27. else
  28. {
  29. nstr += str[(*i)++];
  30. }
  31. }
  32. return retval;
  33. }
  34.  
  35. int main()
  36. {
  37. string str;
  38. cout << "Enter the string to be decoded : ";
  39. cin >> str;
  40. int i = 0;
  41. string ans = decode(str, &i);
  42. cout << ans;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement