有一种固定的字符串速记方式,如a(b)<3>c表示b重复三次,即abbbc,可能存在嵌套,如a(b©<3>)<2>d表示abcccbcccd,输入速记形式,输出原本的字符串。
#include <iostream>
#include <string>
#include<stack>
using namespace std;
string reverseString(string &s) {
//if (s.size() == 0 || s.size() == 1) {
// return s;
//}
stack<string> temp;
string word = "";
for (int i = 0; i < s.size(); i++) {
char c = s[i];
if (c == '(') {
temp.push(word);
word = "";
}
else if (c == ')') {
i += 2;
int num = s[i] - '0';
string copy = word;
while (--num) {
word += copy;
}
word = temp.top() + word;
temp.pop();
i++;
}
else
word = word + c;
}
return word;
}
int main()
{
string s;
cin >> s;
string res = reverseString(s);
cout << res << endl;
system("pause");
return 0;
}