先dfs出入栈出栈顺序,再对栈进行操作
#include<iostream>
#include<string>
#include<stack>
using namespace std;
string Sin, Sout;
int in, out, num,l1,l2;//入栈个数 出栈个数 栈内元素 字符串长度
int m;//深搜次数
char op[100];
stack<char>S;
void dfs(int m)
{
if (m >= l1 * 2){
string s;
int t=0,sx=0;
for (int i = 0; i < l1 * 2; i++){
if (op[i] == 'i') {
S.push(Sin[t]);
t++;
}
else {
s+= S.top();
S.pop();
sx++;
}
}
if (!s.compare(Sout)) {
int i;
for ( i = 0; i < l1 * 2-1; i++) {
cout << op[i]<<' ';
}
cout <<op[i]<< endl;
}
}
else{
//进栈
if (in < l1) {
in++;
op[m] = 'i';
num++;//栈内元素加一
dfs(m + 1);
in--;
num--;
}
//出栈
if (out < l2 && num > 0) {
out++;
op[m] = 'o';
num--;
dfs(m + 1);
out--;
num++;
}
}
}
int main()
{
while (cin >> Sin >> Sout)
{
l1 = Sin.length();
l2 = Sout.length();
in = out = num = 0;
cout << '[' << endl;
if (l1 == l2)
dfs(0);
cout << ']' << endl;
}
return 0;
}