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

指针模拟链表:

预开指针,存在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. NPOI常用功能工具类

    public class NPOIHelper { /// <summary> /// DataTable导出到Excel文件 /// </summary> /// <p ...

  2. BZOJ 4005 [JLOI 2015] 骗我呢

    首先,我们可以得到:每一行的数都是互不相同的,所以每一行都会有且仅有一个在 $[0, m]$ 的数没有出现. 我们可以考虑设 $Dp[i][j]$ 为处理完倒数 $i$ 行,倒数第 $i$ 行缺的数字 ...

  3. HTTP Response Spliting 防范策略研究

    目录0x1:HTTP请求的格式0x2:HTTP请求的方法0x3:HTTP响应的格式0x4:HTTP响应拆分攻击0x5:防范的方法 HTTP请求的格式 客户端所提出的HTTP请求包含下列信息:(1)请求 ...

  4. Design Tutorial: Make It Nondeterministic

    Codeforces Round #270:C;http://codeforces.com/contest/472 题意:水题 题解:贪心即可. #include<iostream> #i ...

  5. 李洪强漫谈iOS开发[C语言-015]-变量的使用

  6. Android Touch事件传递机制

    1.基础知识 (1) 所有Touch事件都被封装成了MotionEvent对象,包括Touch的位置.时间.历史记录以及第几个手指(多指触摸)等. (2) 事件类型分为ACTION_DOWN, ACT ...

  7. SSH框架中配置Hibernate使用proxool连接池

    一.导入proxool.jar包 案例用的是proxool-0.8.3.jar,一般通过MyEclipse配置的SSH都会包含这个jar,如果没有,就去网上搜下下载导入就好了. 二.新建Proxool ...

  8. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  9. Android开发之启动Activity的最佳写法

    从MainActivity跳转到SecondActivity 在SecondActivity中,写一个静态方法actionStart() public static void actionStart( ...

  10. .NET程序内,访问私有或者保护成员的技巧

    如果是C++,我们可以计算对象内成员的位置,然后偏移指针以访问类型的所有非公开成员.但是.NET对象完全受GC管理,地址根本无法得到,并且也无法通过指针调用方法. 当然... 这是一种很不值得推荐的技 ...