Sicily 1396. Q

来源:互联网 发布:南洋金珠优化与染色 编辑:程序博客网 时间:2024/05/03 09:49

1396. Q

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

You've got a queue. And you just got to mess with it. 
Given a queue of items and a series of queue operations, return the resulting queue. 
Queue operations are defined as follows: 
starting-position to requested-position
meaning one wants the item at the starting position to be moved to the requested position. So if the queue of
items were: 
Item1 Item2 Item3 Item4 Item5
(Item1 being in position 1, Item2 in position 2, etc.) 
after applying the queue operation: 
5 to 2 
the resulting queue would be: 
Item1 Item5 Item2 Item3 Item4
as Item5 (the item in position 5) was moved to position 2. Multiple queue operations are applied at the same
time, however; e.g., given the queue of items: 
Item1 Item2 Item3 Item4 Item5 Item6 Item7 Item8
If the following queue operations were applied: 
2 to 6; 6 to 3; 4 to 5; 5 to 2; 7 to 4; 8 to 1
then the resulting queue would be: 
Item8 Item5 Item6 Item7 Item4 Item2 Item1 Item3
As you can see, the queue operations are strictly enforced, with other items (not involved in queue
operations) maintaining their order and moving to vacant positions in the queue. Note that no two queue
operations will have the same starting-position or same requested-position defined.

Input

Input to this problem will begin with a line containing a single integer x indicating the number of datasets.
Each data set consists of three components:
1.  Start line –  single line, "m n" (1 <= m, n <= 20) where m indicates the number of items in the
queue and n indicates the number of queue operations.
2.  Queue items –  line of short (between 1 and 8 characters) alphanumeric names for the items in the
queue. Names are unique for a given data set and contain no whitespace.
3.  Queue operations –n lines of queue operations in the format "starting-position requested-
position".

Output

For each dataset, output the queue after the queue operations have been applied. Print the elements of the
queue on a single line, starting from the first and ending with the last, with a single space separating each
item.

Sample Input

35 1alpha beta gamma delta epsilon5 28 6a b c d e f g h2 66 34 55 27 48 13 2foo bar baz3 11 3

Sample Output

alpha epsilon beta gamma deltah e f g d b a cbaz bar foo

刚开始一直没有读懂题意,后来才知道,原来意思是说新开一个队列,将原队列i位置上的元素移到新队列j位置上,所以才说不会有相同的开始位置或相同的目标位置。。。

直接开一个来放就好了,剩下没放的按顺序放在空位上就行

#include <stdio.h>#include <string.h>int main() {    int case_num;    scanf("%d", &case_num);    while (case_num--) {        int m, n;        scanf("%d%d", &m, &n);        char from[20][9], to[20][9];        bool used[20];        int from_pos, to_pos;        for (int i = 0; i < m; i++) {            scanf("%s", from[i]);            used[i] = false;            to[i][0] = '\0';        }        while (n--) {            scanf("%d%d", &from_pos, &to_pos);            strcpy(to[to_pos - 1], from[from_pos - 1]);            used[from_pos - 1] = true;        }        for (int i = 0, j = 0; i < m; i++) {            if (!used[i]) {                for (; j < m; j++) {                    if (to[j][0] == '\0') {                        strcpy(to[j], from[i]);                        j++;                        break;                    }                }            }        }        for (int i = 0; i < m; i++) {            if (i) {                printf(" ");            }            printf("%s", to[i]);        }        printf("\n");    }    return 0;}


0 0
原创粉丝点击