You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1023 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
//因为不知道什么时候结束所以需要输入后回车然后CTRL+D结束
/**思路:递归的特点就是只解决一层的问题,同样问题依赖于递归调用。
一层问题的四种情况:
1、遇到[
放过它,准备读取数字
2、遇到]
递归出口,返回拼接的子串
3、遇到数字,通过cin>>D,可以把数字读取进来
循环拼接子串
4、遇到子串
不停的读取
*/
string dfs() {
string s, p;
int t;
char x;
while (cin >> x) {
if (x == '[') {
//后面必须跟着数字!
cin >> t;//读取这个数字
//递归调用
p = dfs();
//拼接t次
while (t--) s += p;
} else if (x == ']')
return s;//返回调用者
//读取X的每个字符
else s += x;
}
return s;
}
int main() {
cout << dfs();
return 0;
}