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);
        }
    }
}



原文地址:https://www.cnblogs.com/fuhaots2009/p/3436936.html