1 #include<iostream>
2 #include<stack>
3 #include<queue>
4 #include<algorithm>
5 using namespace std;
6
7 int n;
8 char a[5000];
9
10 void dfs(queue<char> b,int n,stack<int> st,queue<char> out)//example:abc
11 {
12 if(n<=0||(b.empty()&&st.empty()&&out.empty()))
13 return;
14
15
16 if (out.size() == n)//7.out3->0;
17 {
18 while (!out.empty())
19 {
20 cout << out.front() << " ";
21 out.pop();
22 }
23 cout << endl;
24 return;
25 }
26
27 stack<int> stcopy = st;
28 queue<char> outcopy = out;
29
30 if (!st.empty())//把st的值传给out//2.stc1->0,out1->a 4.stc2->0 out2->ab 6.stc3->0 out3->abc 1.1.stc3->0 b->0
31 {
32 out.push(st.top());
33 st.pop();
34 dfs(b, n, st, out);
35 }//关键在于stcopy对于上一个st的保存在栈里面。
36
37
38 if (!b.empty())//如果队列没空把队首的值给1.stcopy//b->bc stc1->a 3.b->c stc2->b 5.b->0 stc3->c
39 {
40 stcopy.push(b.front());
41 b.pop();
42 dfs(b, n, stcopy, outcopy);
43 }
44 }
45 int main()
46 {
47 cin>>n;
48 queue<char> b,out;
49 stack<int>st;
50 for (int i = 0; i < n; i++)
51 {
52 cin>> a[i];
53 b.push(a[i]);
54 }
55 dfs(b, n, st, out);
56 return 0;
57
58 }