11029: 【原1029】整理书架
题目
题目描述
author: yydcool 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/1029
Description
二哥又要整理书架了。他整理书架的方法很简单,把书架上一排所有的书全部挪到另一排的后面。现在二哥把它整理的顺序告诉你,你来告诉他整理之后的书架是什么样子的。
Input Format
读入一个数 \( n \leq 100 \),表示书架一共有n排,接下来有n行,每行有一些数字(不多于100个数),每个数字代表一本书,每一行表述这一排书架上的书。再下来有n-1个数对,数对x,y表示把第x排的书放到第y排的后面。
Output Format
输出只有一行,输出最后一排的所有书。
Sample Input
3
1 2 3
4 5
6 7
3 1
2 1
Sample Output
1 2 3 6 7 4 5
FineArtz's solution
/* 整理书架 */
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<vector<int>> book;
string ss;
getline(cin, ss);
for (int i = 1; i <= n ; ++i){
string st;
getline(cin, st);
istringstream iss(st);
int t;
vector<int> tv;
while (iss >> t)
tv.push_back(t);
book.push_back(tv);
}
for (int i = 1; i < n; ++i){
int x, y;
cin >> x >> y;
book[y - 1].insert(book[y - 1].end(), book[x - 1].begin(), book[x - 1].end());
book[x - 1].clear();
}
for (int i = n - 1; i >= 0; --i){
if (!book[i].empty()){
for (auto j : book[i]){
cout << j << ' ';
}
cout << endl;
return 0;
}
}
return 0;
}
ligongzzz's solution
#include "iostream"
#include "cstdio"
#include "cstring"
using namespace std;
class node {
public:
int val = 0;
node* next = nullptr;
};
node* heads[109] = { nullptr };
node* rears[109] = { nullptr };
char ch[10009] = { 0 };
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
rears[i] = heads[i] = new node;
}
cin.getline(ch, 10009);
for (int i = 1; i <= n; ++i) {
cin.getline(ch, 10000);
int length = strlen(ch);
int num = 0;
for (int j = 0; j < length; ++j) {
if (ch[j] == ' ') {
continue;
}
else if (ch[j] == '\n') {
break;
}
else {
num = num * 10 + ch[j] - '0';
if (ch[j + 1]<'0' || ch[j + 1]>'9') {
rears[i]->next = new node;
rears[i] = rears[i]->next;
rears[i]->val = num;
num = 0;
}
}
}
}
for (int i = 0; i < n - 1; ++i) {
int a, b;
scanf("%d %d", &a, &b);
rears[b]->next = heads[a]->next;
heads[a]->next = nullptr;
rears[b] = rears[a];
rears[a] = heads[a];
}
for (int i = n; i > 0; --i) {
if (heads[i]->next == nullptr)
continue;
for (auto p = heads[i]->next; p; p = p->next)
cout << p->val << " ";
}
return 0;
}
yyong119's solution
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int n, i, h, t, indexx;
int main() {
queue<string> que[101];
string temp;
scanf("%d\n", &n);
for (i = 1; i <= n; i++) {
getline(cin, temp);
que[i].push(temp);
}
for (i = 1; i < n; i++) {
scanf("%d%d", &t, &h);
while (que[t].size()) {
que[h].push(que[t].front());
que[t].pop();
}
}
for (indexx = 1; indexx <= n; indexx++) if (que[indexx].size()) break;
while (que[indexx].size()) {
cout<<que[indexx].front()<<" ";
que[indexx].pop();
}
return 0;
}