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\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 链表的更多相关文章
- URAL 1992 CVS
CVS 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=1992 Description Yoda: Visit I will the c ...
- URAL 1992 CVS 可持久化链栈
http://www.cnblogs.com/tedzhao/archive/2008/11/12/1332112.html 看这篇的链表部分的介绍应该就能理解“可持久化”了 动态分配内存的会T,只能 ...
- URAL 1992
CVS Description Yoda: Visit I will the cloners on Kamino... And see this army they have created for ...
- ural 1269. Obscene Words Filter
1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check mess ...
- URAL题解二
URAL题解二 URAL 1082 题目描述:输出程序的输入数据,使得程序输出"Beutiful Vasilisa" solution 一开始只看程序的核心部分,发现是求快排的比较 ...
- Redis链表实现
链表在 Redis 中的应用非常广泛, 比如列表键的底层实现之一就是链表: 当一个列表键包含了数量比较多的元素, 又或者列表中包含的元素都是比较长的字符串时, Redis 就会使用链表作为列表键的底层 ...
- [数据结构]——链表(list)、队列(queue)和栈(stack)
在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...
- 排序算法----基数排序(RadixSort(L))单链表智能版本
转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...
- 版本控制工具比较-CVS,SVN,GIT
首先介绍几个版本控制软件相互比较的重要依据: a.版本库模型(Repository model):描述了多个源码版本库副本间的关系,有客户端/服务器和分布式两种模式.在客户端/服务器模式下,每一用户通 ...
随机推荐
- uva 104 Bandwidth
题意: 给一个图, 将其节点以任一序列排列. 1)计算每个节点距离相邻节点的最大距离 dis[i] 2)计算出当前序列中, 所有节点的dis[i], 并求出最大的dis[i] : max_dis 求最 ...
- php调用whois接口域名查询
由两部分组成,一个index.php文件,一个whois的接口文件: <html> <head> <title>域名到期查询</title> <s ...
- Js通过原型继承创建子类
//定义一个有两个方法的类 function Person(){} Person.prototype.married = function(){}; Person.prototype.unmerrie ...
- Codeforces Round #209 (Div. 2)
A: 要么是两次要么4次,判断是否在边界: #include<cstdio> using namespace std; int main() { int n,m,x; ; scanf(&q ...
- x86, x86-64, i386, IA32, IA64...
转自x86, x86-64, i386, IA32, IA64... x86:Intel从16位微处理器8086开始的整个CPU芯片系列,系列中的每种型号都保持与以前的各种型号兼容,主要有8086,8 ...
- 【HDOJ】1203 I NEED A OFFER!
DP问题. #include <stdio.h> #define MAXNUM 10002 int main() { int m, n; int cost[MAXNUM]; // the ...
- wcf service library
创建wcf服务库的时候,系统自动生成的代码 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”. [ServiceContract] publ ...
- Android开发UI之Toast的使用
Toast,A toast provides simple feedback about an operation in a small popup. 对于操作提供一个简单反馈信息. 官网链接:htt ...
- C#中IDisposable学习
在Net中,由GC垃圾回收线程掌握对象资源的释放,程序员无法掌控析构函数的调用时机.对于一些非托管资源,比如数据库链接对象等,需要实现IDisposable接口进行手动的垃圾回收.那么什么时候使用Id ...
- ☀【组件】数组 array
<!doctype html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...