zoukankan      html  css  js  c++  java
  • ZOJ

    Given initially empty stacks, there are three types of operations:

    1 s v: Push the value onto the top of the -th stack.

    2 s: Pop the topmost value out of the -th stack, and print that value. If the -th stack is empty, pop nothing and print “EMPTY” (without quotes) instead.

    3 s t: Move every element in the -th stack onto the top of the -th stack in order.

    Precisely speaking, denote the original size of the -th stack by , and the original size of the -th stack by . Denote the original elements in the -th stack from bottom to top by , and the original elements in the -th stack from bottom to top by .

    After this operation, the -th stack is emptied, and the elements in the -th stack from bottom to top becomes . Of course, if , this operation actually does nothing.

    There are operations in total. Please finish these operations in the input order and print the answer for every operation of the second type.

    Input
    There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

    The first line contains two integers and (), indicating the number of stacks and the number of operations.

    The first integer of the following lines will be (), indicating the type of operation.

    If , two integers and (, ) follow, indicating an operation of the first type.
    If , one integer () follows, indicating an operation of the second type.
    If , two integers and (, ) follow, indicating an operation of the third type.
    It’s guaranteed that neither the sum of nor the sum of over all test cases will exceed .

    Output
    For each operation of the second type output one line, indicating the answer.

    Sample Input
    2
    2 15
    1 1 10
    1 1 11
    1 2 12
    1 2 13
    3 1 2
    1 2 14
    2 1
    2 1
    2 1
    2 1
    2 1
    3 2 1
    2 2
    2 2
    2 2
    3 7
    3 1 2
    3 1 3
    3 2 1
    2 1
    2 2
    2 3
    2 3
    Sample Output
    13
    12
    11
    10
    EMPTY
    14
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    EMPTY
    EMPTY

    题意:有n个栈,有q个操作,操作有3种。
    1 s t:在第s个栈中push元素t
    2 s :弹出第s个栈的栈顶,并输出这个数
    3 s t:将第t个栈直接加到第s个栈顶之后,第t个栈清空。第s个栈栈顶加入从第t个栈栈底到栈顶所有元素,也就是合并栈

    据普遍反映使用STL模拟会因为操作过多而超时。此处手动链表模拟栈,对于栈合并的操作不需要一个一个元素倒出再push,而是直接将被合并的栈底指针连接到被合并的另一个栈的栈顶。使两个栈完全合并。 注意指针指向,很可能出现SF指针错误的问题。

    #include<stdio.h>
    #include<malloc.h>
    #include<string.h>
    const int maxn=300005;
    struct node//定义链表结点结构
    {
        int val;
        node *next,*pre;
    };
    node* newnode()///创建新结点并初始化
    {
        node* p=(node*)malloc(sizeof(node));
        p->next=NULL;
        p->pre=NULL;
        return p;
    }
    int n,q;
    node* head[maxn];//每个栈的栈底指针,头结点存储该栈大小(元素个数)
    node* tail[maxn];//每个栈的栈顶指针,初始和头结点相同
    int main()
    {
        int t,flag,s,val;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&q);
            for(int i=1; i<=n; i++)///初始化所有栈的栈底和栈顶
            {
                head[i]=newnode();
                head[i]->val=0;
                head[i]->next=NULL;
                head[i]->pre=NULL;
                tail[i]=head[i];
            }
            while(q--)
            {
                scanf("%d",&flag);
                if(flag==1)///对于插入操作,创建新结点并赋予新值
                {
                    scanf("%d%d",&s,&val);
                    head[s]->val++;
                    node* p=newnode();
                    p->val=val;
                    p->pre=tail[s];
                    tail[s]->next=p;
                    tail[s]=p;
                }
                else if(flag==2)///删除结点,注意只删除不为空的栈
                {
                    scanf("%d",&s);
                    if(head[s]->val==0) printf("EMPTY
    ");
                    else
                    {
                        printf("%d
    ",tail[s]->val);
                        node *p=tail[s];
                        tail[s]=tail[s]->pre;
                        tail[s]->next=NULL;
                        free(p);///被删除的结点释放空间
                        head[s]->val--;
                    }
                }
                else
                {
                    scanf("%d%d",&s,&val);
                    if(head[val]->val!=0)///合并栈,注意只能合并不为空的栈,否则会指针出错
                    {
                        tail[s]->next=head[val]->next;
                        head[val]->next->pre=tail[s];
                        tail[s]=tail[val];
                        head[s]->val+=head[val]->val;///加上被合并的栈的容量
                        head[val]->val=0;///被合并的栈容量设为零
                        head[val]->next=NULL;
                        tail[val]=head[val];
                    }
                }
            }
        }
    }
  • 相关阅读:
    努力努力,我假期要练琴学css
    在遇到困难时我们都会想要是这一切不曾发生该多好,可现实并不以我们的意志为转移,我们所能做的就是去克服...
    寒假学习目标~
    平安夜&&圣诞节
    you are the ont that we would like to trust and ca...
    Happy New Year!PR升3啦!
    呵呵,Merry Christmas & Happy New Year!
    使用Rx Framework实现XAML中的物体拖动
    MVVM绑定多层级数据到TreeView并设置项目展开
    Entity framework中EntityFunctions.CreateDateTime方法的闰年闰月bug
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135800.html
Copyright © 2011-2022 走看看