题目:
首先,括号对前面的字符串的输出是无法分割的。
也就是说,括号右边的字符只能输出到前面这个字符串的2边。
这个和算术表达式里面的小括号是很像的,小括号是封闭的,括号外面的内容无法影响里面的内容。
所以,这个题目可以模拟算术表达式,找到唯一的核心分割点,比如a*b-c+d中的+,又比如a*b-c*d中的-
找到之后,递归即可。
代码:
#include<iostream>
#include<string>
#include<string.h>
#include<stack>
using namespace std
;
stack
<int>s
;
char c
[100005];
void f(int high
)
{
if (s
.empty())
{
for (int i
= 0; i
<= high
; i
++)printf("%c", c
[i
]);
return;
}
int key
= s
.top();
s
.pop();
if (c
[key
] == ']')
{
f(key
- 1);
for (int i
= key
+ 1; i
<= high
; i
++)printf("%c", c
[i
]);
}
else
{
for (int i
= key
+ 1; i
<= high
; i
++)printf("%c", c
[i
]);
f(key
- 1);
}
}
int main()
{
string str
;
int l
;
while (scanf("%s", c
) != EOF
)
{
l
= strlen(c
);
while (!s
.empty())s
.pop();
for (int i
= 0; i
< l
; i
++)if (c
[i
] == '[' || c
[i
] == ']')s
.push(i
);
f(l
-1);
cout
<< endl
;
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-1310411.html