Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string nstr;
- string decode(string str, int *i)
- {
- string retval;
- while (*i < str.length() && str[*i] != ']')
- {
- if (!isdigit(str[*i]))
- {
- if (str[*i] == '[')
- {
- int n = stoi(nstr);
- nstr = "";
- *i += 1;
- string istr = decode(str, i);
- *i += 1;
- while (n-- > 0)
- {
- retval += istr;
- }
- }
- else
- retval += str[(*i)++];
- }
- else
- {
- nstr += str[(*i)++];
- }
- }
- return retval;
- }
- int main()
- {
- string str;
- cout << "Enter the string to be decoded : ";
- cin >> str;
- int i = 0;
- string ans = decode(str, &i);
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement