• URAL 1992 CVS 链表


    不更改链表结构,只是添加节点,没有删除节点。通过记录和更改标记来模拟题意的插入和删除,复制

    指针模拟链表:

    预开指针,存在M[]中,可以提高效率

    #include<functional>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<map>
    #include<set>
    #include <stack>
    #define REP(i, n) for(int i=0; i<n; i++)
    #define PB push_back
    #define LL long long
    #define CLR(a, b) memset(a, b, sizeof(a))
    using namespace std;
    
    const int maxn = 500005;
    
    struct point{
        int val;
        point *pre;
    }M[maxn * 2];
    int tot;
    struct node{
        point *a, *b;
    }c[maxn];
    
    void add(point* &pre, int y)
    {
        point *u = &M[tot++];
        u->val = y;
        u->pre = pre;
        pre = u;
    }
    void del(point* &last)
    {
        last = last->pre;
    }
    int t, m, n;
    
    int main()
    {
        char op[12];
        tot = 1;
        n = 1;
        int x, y;
        scanf("%d%d", &t, &m);
    
        while (t--)
        {
            scanf("%s", op);
            scanf("%d", &x);
            if (op[0] == 'l')
            {
                scanf("%d", &y);
    
                add(c[x].a, y);
                c[x].b = NULL;
            }
            else if (op[0] == 'r' && op[1] == 'o')
            {
                if (c[x].a)
                {
                    add(c[x].b, c[x].a->val);
                    del(c[x].a);
                }
            }
            else if (op[0] == 'r' && op[1] == 'e')
            {
                if (c[x].b)
                {
                    add(c[x].a, c[x].b->val);
                    del(c[x].b);
                }
            }
            else if (op[0] == 'c' && op[1] == 'l')
            {
                c[++n] = c[x];
            }
            else
            {
                if (!c[x].a) printf("basic
    ");
                else printf("%d
    ", c[x].a->val);
            }
        }
    }

    数组模拟链表:

    #include<functional>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<map>
    #include<set>
    #include <stack>
    #define REP(i, n) for(int i=0; i<n; i++)
    #define PB push_back
    #define LL long long
    #define CLR(a, b) memset(a, b, sizeof(a))
    using namespace std;
    
    const int maxn = 500005;
    
    struct point{
        int val;
        int pre;
    }P[maxn * 2];
    
    struct node{
        int a, b;
        node(){}
        node(int a, int b) : a(a), b(b){}
    }c[maxn];
    int tot;
    
    int t, m, n;
    
    void add(int u, int &pre, int y)
    {
        P[tot].pre = pre;
        P[tot].val = y;
        pre = tot++;
    }
    
    void del(int &last)
    {
        last = P[last].pre;
    }
    
    int main()
    {
        char op[12];
        n = 1;
        tot = 1;
        int x, y;
        scanf("%d%d", &t, &m);
    
        while (t--)
        {
            scanf("%s", op);
            scanf("%d", &x);
            if (op[0] == 'l')
            {
                scanf("%d", &y);
    
                add(x, c[x].a, y);
                c[x].b = 0;
            }
            else if (op[0] == 'r' && op[1] == 'o')
            {
                if (c[x].a)
                {
                    add(x, c[x].b, P[c[x].a].val);
                    del(c[x].a);
                }
            }
            else if (op[0] == 'r' && op[1] == 'e')
            {
                if (c[x].b)
                {
                    add(x, c[x].a, P[c[x].b].val);
                    del(c[x].b);
                }
            }
            else if (op[0] == 'c' && op[1] == 'l')
            {
                c[++n] = node(c[x].a, c[x].b);
            }
            else
            {
                if (!c[x].a) printf("basic
    ");
                else printf("%d
    ", P[c[x].a].val);
            }
        }
    }



  • 相关阅读:
    【2018 “百度之星”程序设计大赛
    分班级(经典二分)
    【2018 “百度之星”程序设计大赛
    【zznu-夏季队内积分赛3-J】追忆
    常见网络名词解释
    【zznu-夏季队内积分赛3-G】2333
    【zznu-夏季队内积分赛3-F】学无止境
    【zznu-夏季队内积分赛3-I】逛超市
    html/css/javascript练习代码
    花生壳免费域名80端口无法访问问题处理
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3436936.html
Copyright © 2020-2023  润新知