复制与粘贴

来源:互联网 发布:淘宝网浏览器官方下载 编辑:程序博客网 时间:2024/05/02 02:36



Problem 101: 剪切与粘贴


Time Limit:1 Ms| Memory Limit:128 MB
Difficulty:2

Description

我们用文本处理器来处理一个特殊的文本文件,该文本文件共有N行文本,每一行文本仅包含一个自然数,第一行为1、第二行为2,以此类推至N行为自然数N。
假设对该文本文件执行一次“剪切和粘贴”操作含义如下:首先选定连续的若干行文本,“剪切”操作将选定的文本从文件中剪下,而“粘贴”操作将剪切下来的文本插入到文件中的其他地方。
编写一个程序求出在进行了连续若干次“剪切和粘贴”操作后,文本文件中前十行的内容。

Input

输入文件的第一行包含两个用空格隔开的自然数N和K,N表示文件的总行数(10≤N≤100,000),K表示“剪切和粘贴”的总次数(1≤k≤1000)。
下面K行每一行包含一次“剪切和粘贴”操作的执行信息,每行包含三个用空格隔开自然数A,B和C,其中1≤A≤B≤N,0≤C≤N-(B-A+1)。A和B表示选定文本的第一行和最后一行,C表示被剪切下来的文本待插入处的前一行,如果C等于0则被剪切下来的的文本将被插入到文件的开头。

Output

输出文件,将由十行组成,其中包含所有的操作都完成后的文本文件中前十行所包含的数字。

Sample Input

13 3
6 12 1
2 9 0
10 13 8

Sample Output

6
7
8
9
10
11
12
2
3
4

代码:

#include<stdio.h>#include<stdlib.h> typedef struct node{    int d;    struct node *next;}Node; Node *head = NULL, *pre, *below;//pre指向是剪切开始的位置,below指向剪切结束的位置;int n, num; void create(Node **head)//建立链表;{    int i;    Node *p,*tail;    for(i = 1; i <= n; i++)    {        p = (Node *)malloc(sizeof(Node));        p->d = i;        p->next = NULL;        if(*head == NULL)        {            (*head) = (Node *)malloc(sizeof(Node));            (*head)->next = p;            tail = p;        }        else        {            tail->next = p;            tail = p;        }    }} int main(){    int i,j,pnum, a1, a2, a3, k;    Node *p, *tail, *in, *prever;//prever指向pre的前一个位置;    scanf("%d%d", &n, &num);    create(&head);    for(i = 1; i <= num; i++)    {        j = 0;        tail = head;        scanf("%d%d%d", &a1,&a2, &a3);        if(a3 == 0)        {            in = head;        }        while(j != a1)        {            if(a3 < a1 && j == a3 && a3 != 0)            {                in = tail;            }            prever = tail;            tail = tail->next;            j++;        }        pnum = j - 1;//pnum记录在剪切开始位置前面有多少个数字;        pre = tail;        while(j != a2)        {            tail = tail->next;            j++;        }        below = tail;        if(a3 >= a1)//注意如果是把前面的插到后面去,那么a3是在除去剪切的a2 - a1个数后的即不能把已经剪切的算入;还要注意考虑a3 = a1的情况;
        {            k = 0;            while(a3 - pnum > k)            {                tail = tail->next;                k++;            }            in = tail;        }        prever->next = below->next;        below->next = in->next;        in->next = pre;    }    p = head->next;    j = 0;    while(p)    {        if(j < 10)        {            printf("%d\n",p->d);            p = p->next;            j++;        }        else        {            break;        }    }}

0 0
原创粉丝点击