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

指针模拟链表:

预开指针,存在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\n");
else printf("%d\n", 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\n");
else printf("%d\n", P[c[x].a].val);
}
}
}

URAL 1992 CVS 链表的更多相关文章

  1. URAL 1992 CVS

    CVS 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=1992 Description Yoda: Visit I will the c ...

  2. URAL 1992 CVS 可持久化链栈

    http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html 看这篇的链表部分的介绍应该就能理解“可持久化”了 动态分配内存的会T,只能 ...

  3. URAL 1992

    CVS Description Yoda: Visit I will the cloners on Kamino... And see this army they have created for ...

  4. ural 1269. Obscene Words Filter

    1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check mess ...

  5. URAL题解二

    URAL题解二 URAL 1082 题目描述:输出程序的输入数据,使得程序输出"Beutiful Vasilisa" solution 一开始只看程序的核心部分,发现是求快排的比较 ...

  6. Redis链表实现

    链表在 Redis 中的应用非常广泛, 比如列表键的底层实现之一就是链表: 当一个列表键包含了数量比较多的元素, 又或者列表中包含的元素都是比较长的字符串时, Redis 就会使用链表作为列表键的底层 ...

  7. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  8. 排序算法----基数排序(RadixSort(L))单链表智能版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  9. 版本控制工具比较-CVS,SVN,GIT

    首先介绍几个版本控制软件相互比较的重要依据: a.版本库模型(Repository model):描述了多个源码版本库副本间的关系,有客户端/服务器和分布式两种模式.在客户端/服务器模式下,每一用户通 ...

随机推荐

  1. 简单的php表单

    表单的三种传递机制: $_GET:不安全,传递的参数会显示在url中. $_POST:相对安全,隐形传递. $_REQUEST:宽松的,包含所有 GET.POST.COOKIE 和 FILE 的数据. ...

  2. jquery怎么实现左右滑动的问题

    var len = $("#b span").length, curindex = 0; $("#leftRun").click(function(){ if( ...

  3. 0xc0000428 winload.exe无法验证其数字签名的解决方法

    只要把windows/system32/boot中的winload.exe复制到windows/system32中替换即可!! 只有启动画面会有变化,可以使用魔方等软件进行修复,恢复到之前的样子

  4. ThinkPHP中视图模型详解.

    很多TP的新手对于模型中的视图模型不甚了解,官方虽然有详细手册,但是对于初学者来说还是比较难以理解! 先简单说一下视图模型所能实现的功能,基本就是主表与副表之间各个字段的关联问题,实现多表关联查询,相 ...

  5. android 为组件添加contextMenu上下文菜单

    package com.example.fragmentNavigation2.fragment; import android.os.Bundle; import android.support.v ...

  6. linux-0.11内核 任务的堆栈切换

    http://blog.163.com/di_yang@yeah/blog/static/86118492201212534924900/ 一直缠绕的两个问题:怎样标识的内核栈与用户栈?如何在内核态堆 ...

  7. HDOJ多校联合第六场

    先一道一道题慢慢补上, 1009.题意,一棵N(N<=50000)个节点的树,每个节点上有一个字母值,给定一个串S0(|S0| <=30),q个询问,(q<=50000),每次询问经 ...

  8. JAVA三大框架的各自作用

    http://christhb.blog.163.com/blog/static/98982492011727114936239/ 一.Spring Spring是一个解决了许多在J2EE开发中常见的 ...

  9. hdu4734F(x)(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4734 各种不细心啊  居然算的所有和最大值会小于1024... 第二次做数位DP  不是太熟 #include ...

  10. 数据库 一致性读&&当前读

    今天小伙伴问了一个sql的问题: update t set status=2 where id in(select id from t where status=1) 这个sql,在并发的情况下,会不 ...