1. 程式人生 > >上海交大OJ 整理書架

上海交大OJ 整理書架

Description

二哥又要整理書架了。他整理書架的方法很簡單,把書架上一排所有的書全部挪到另一排的後面。現在二哥把它整理的順序告訴你,你來告訴他整理之後的書架是什麼樣子的。

Input Format

讀入一個數 n≤100 n≤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

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

int main(int argc, char const *argv[]) {
    //N 排
    int N;
    vector<char> q[501];//儲存字串 輸入時不除去空格

    scanf("%d",&N);
    getchar();//除掉N輸入後產生的\n

    //用於計數
    int L = N,in = N - 1;

    int i = 0;
    while (L--) {
        while (1) {
            char ch;
            scanf("%c",&ch);
            // printf("%c",ch);
            if(ch == '\n'){
                q[i].push_back(' ');//最後一位加上空格便於識別數字
                break;
            }
            q[i].push_back(ch);
        }
        i++;
    }
    //op 用於記錄某次操作的x和y
    int op[2];
    while (in--) {
        scanf("%d%d",&op[0],&op[1]);
        //將x排的數放到y排後面
        for(auto v:q[op[0] - 1]){
            q[op[1] - 1].push_back(v);
        }
        //然後將x排刪除
        q[op[0] - 1].erase(q[op[0] - 1].begin(),q[op[0] - 1].end());
    }
    // 輸出
    for(int u = 0;u < N;u++){
        for(auto v:q[u]){
            printf("%c",v);
        }
    }
    return 0;
}