版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37754288/article/details/81879507
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 5;
int n,x,root;
int pos[maxn];
vector<int>A[maxn];
int main(){
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
while(scanf("%d",&n) != EOF){
for(int i = 1;i <= n;i++){
scanf("%d",&x);
pos[x] = i;
A[i].clear();
}
stack<int>S;
scanf("%d",&root);
S.push(root);
for(int i = 1;i < n;i++){
scanf("%d",&x);
while(1){
int t = S.top();
if(t == root || pos[t]+1 < pos[x]){
A[t].push_back(x);
S.push(x);
break;
}
else S.pop();
}
}
for(int i = 1;i <= n;i++){
printf("%d:",i);
for(int j = 0;j < A[i].size();j++) printf(" %d",A[i][j]);
putchar('\n');
}
}
return 0;
}