Files
python/TangDou/LuoGuBook/P1928.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

47 lines
1023 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}