上海交大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;
}

猜你喜欢

转载自blog.csdn.net/WX_1218639030/article/details/84344736
今日推荐