#include<iostream>
#include<string>
#include<stack>
#include<cstring>
using namespace std;
int n, pos[30], ans[30], temp[30], cnt, sum;
string s1, s2;
stack<int> t[30];
int main(){
cin >> n;
for(int i = 0; i < n; i++){
t[i].push(i);
pos[i] = i;
}
int a, b;
while(cin >> s1){
if(s1[0] == 'q') break;
else cin >> a >> s2 >> b;
if(pos[a] == pos[b]) continue;
if(s1[0] == 'm'){
while(t[pos[a]].top() != a){
t[t[pos[a]].top()].push(t[pos[a]].top());
pos[t[pos[a]].top()] = t[pos[a]].top();
t[pos[a]].pop();
}
}
if(s2[1] == 'n'){
while(t[pos[b]].top() != b){
t[t[pos[b]].top()].push(t[pos[b]].top());
pos[t[pos[b]].top()] = t[pos[b]].top();
t[pos[b]].pop();
}
}
cnt = 0;
memset(temp, 0, sizeof(temp));
while(temp[cnt] != a || (temp[cnt] == a && cnt == 0)){
temp[++cnt] = t[pos[a]].top();
t[pos[a]].pop();
pos[temp[cnt]] = pos[b];
}
for(int i = cnt; i >= 1; i--){
t[pos[b]].push(temp[i]);
}
}
for(int i = 0; i < n; i++){
printf("%d: ", i);
sum = 0;
memset(ans, 0, sizeof(ans));
while(!t[i].empty()){
ans[++sum] = t[i].top();
t[i].pop();
}
for(int j = sum; j > 1; j--){
printf("%d ", ans[j]);
}
if(sum > 0) printf("%d\n", ans[1]);
else printf("\n");
}
return 0;
}
样例过了,第一个测试点就错了。。
哪位大佬能帮我看看哪里写的有问题QwQ