题目说的天花乱坠,其实就是解密字符串 要把%和#之间的字符按照%号前面数字次数来循环
eg: 3%acm#2%acm# ------> acmacmacmacmacm
还有嵌套的情况
3%g2%n## ->>>>> gnngnngnn
眼尖的读者一看就明白了 这不就是栈么! 当然递归也可以
那我就不多bb,上代码!
#include<iostream>
#include<stack> //用c++的STL
#include<string>
#include<cstring>
using namespace std;//3%g2%n##
int main()
{
stack<char> s;
string input;
cin >> input;
int num = 0;
char *p = new char[50];
string str;
for (int i = 0; i < input.size(); i++)
{
if (input[i] == '#')//遇到#号就弹栈
{
int j = 0;
while (1)
{
if (s.top() == '%')//到%就叫停 然后多读取一次 把%前面的数字读出来
{
s.pop();
num= s.top()-48;
s.pop();
p[j] = '\0';
break;
}
p[j] = s.top();
s.pop();
j++;
}
strrev(p); // 转置字符串 因为弹栈之后的入栈要按照之前的顺序
str = "";
for (int k = 0; k < num; k++)
{
str += p;
}
for (int i = 0; i < str.size(); i++)
{
s.push(str[i]);
}
continue;
}
else
{
s.push(input[i]);
//cout << s.top();
}
}
while (!s.empty()) //最后栈空 输出结束!
{
cout << s.top();
s.pop();
}
system("pause");
}