版本比较
C++简单数据结构
标题
增加 1 行删除 1 行
11C++数据结构C++简单数据结构正文
增加 50 行删除 142 行
12312354567989111213141011121369 行未改动8384858692879495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311328889909163 行未改动155156157158204159160161162163112 行未改动全部展开27627727827932532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038128028128228330 行未改动31431531631742042131831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336456 行未改动42142242342448748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851942542642742842939 行未改动469470471472473474## 零、前言在c++中,数据结构是十分常用的,不管你是OI界的萌新,还是可以秒切黑题的巨佬,对数据结构一定都不陌生## 一、线性数据结构## 0.前言由于本人是 xxs,数据结构学的不多,文章有错误还请多多谅解。
### 1. 栈## 1. 栈
#### 定义:
栈是一种**后进先出**的数据结构。栈只有一段能够进出元素,我们一般称这一端为栈顶,另一端为栈底。添加或删除栈中元素时,我们只能将其插入到栈顶(进栈),或者把栈顶元素从栈中取出(出栈)。栈是一种**后进先出**的数据结构。栈只有一段能够进出元素,我们一般称这一端为栈顶,另一端为栈底。添加或删除栈中元素时,我们只能将其插入到栈顶(进栈),或者把栈顶元素从栈中取出(出栈),一般用于 DFS 。
#### 作用
栈是机器实现递归(深度优先搜索)的基本结构,遇到先进后出或后进先出时很好实现。
#### 实现:[luogu-B3614【模板】栈](https://www.luogu.com.cn/problem/B3614)
##### 非STL实现:
69 行未改动##### 1. [简单题](https://www.luogu.com.cn/problem/P2201)
本题我们可使用一个类似于求中位数的对顶堆的算法,**对顶栈**建立两个栈,栈 sta1 存储从序列开头到当前光标位置的这一段子序列,栈 sta2 存储从当前光标位置到的这一段子序列序列结尾,二者均以光标为栈顶,两个栈合起来就保存了整个序列。我们用 ans 维护栈的最大前缀和,设 sta 的栈顶位置下标时 top1, sta 是序列 sta1 的前缀和数组。
具体操作看代码:
```cpp#include<bits/stdc++.h>using namespace std;int sta1[1000010],sta2[1000010];int ans[1000010],sta[1000010];int top1,top2,n;int main(){ cin>>n; ans[0]=-(1<<30); for(int i=1;i<=n;i++) { char op;cin>>op; if(op=='I') { int x;cin>>x; sta1[++top1]=x; sta[top1]=sta[top1-1]+x; ans[top1]=max(ans[top1-1],sta[top1]); } if(op=='D') top1--; if(op=='L') sta2[++top2]=sta1[top1--]; if(op=='R') { int x=sta2[top2--]; sta1[++top1]=x; sta[top1]=sta[top1-1]+x; ans[top1]=max(ans[top1-1],sta[top1]); } if(op=='Q') { int x;cin>>x; cout<<ans[x]<<endl; } } return 0;}```
##### 2. [经典题](https://www.luogu.com.cn/problem/P1449)
$后缀表达式求值$
63 行未改动[P8815 [CSP-J 2022] 逻辑表达式](https://www.luogu.com.cn/problem/P8815)
[P3200 [HNOI2009] 有趣的数列](https://www.luogu.com.cn/problem/P3200)
### 2. 链表## 2. 链表
#### 定义
链表是由一系列**节点**组成,每个节点包含**数据元素**和**指向下一个节点的指针**的线性数据结构。链表分为单向链表和双向链表,单向链表较少使用,而双向链表则较为常用112 行未改动全部展开##### 1. [简单题](https://www.luogu.com.cn/problem/P1160)
看完题目,我们可以发现,我们可以用一个**双向链表**维护这个队伍,和模板差别不算太大。我们只需用数组 index 记录同学的编号,在插入删除时找到他就可以了。
具体操作看代码:
```cpp#include<bits/stdc++.h>using namespace std;struct Node{ int value; int pre,nex;}s[1010];int n,m;int index[100010];int tot;void ins_back(int x,int y){ int now=index[x]; s[++tot]=node(y,now,s[now].nex); s[s[now].pre].pre=tot; s[now].nex=tot; index[y]=tot;}void ins_front(int x,int y){ int now=index[x]; s[++tot]=node(y,s[now].pre,now); s[s[now].pre].nex=tot; s[now].pre=tot; index[y]=tot;}void del(int x){ int now=index[x]; int le=s[now].pre,rt=s[noe].nex; s[le].nex=rt; s[rt].pre=le; index[x]=0;}int main(){ cin>>n; s[0]=node(); ins_back(0,1); for(int i=2;i<=n;i++){ int k,p; cin>>k>>p; p?ins_back(k,i):ins_front(k,i); } cin>>m; for(int i=1;i<=m;i++){ int x; cin>>x; if(index[x])del(x); } int now=s[0].nex; while(now){ cout<<s[now].value; now=s[now].nex; } return 0;}```
##### 2. [经典题](https://www.luogu.com.cn/problem/P1996)
乍一看题大家可能都会想用队列,不过链表也是可以的。在这里我们引入链表的 STL 实现:**list**30 行未改动[P7912 \[CSP - J 2021\] 小熊的果篮](https://www.luogu.com.cn/problem/P7912)
[P4269 \[USACO18FEB\] Snow Boots G](https://www.luogu.com.cn/problem/P4269)
## 树状数据结构### 2.并查集## 3.队列#### 定义和栈正好相反,它是 **先进先出** 的,一般用于 BFS 。#### 实现队列的实现是相对简单的:```cppstruct st{ int end=0,head=1; int a[12000]; int size(){ return end-head+1; } void pop(){ head++; } int front(){ return a[head]; } void push(int x){ a[++end]=x; }};st s;```#### 例题##### 1.[简单题](https://www.luogu.com.cn/problem/P1996)很简单的一道队列,几乎就是模板。##### 2.[经典题](https://www.luogu.com.cn/problem/P1828)队列加上 BFS ,这道题是一般的队列的难度:用来维护 BFS 。
代码比较简单,自己打一打吧。##### 3.[拓展题](https://www.luogu.com.cn/problem/P9905)**单调队列**,一种神秘的队列。
这种队列可以用 `deque` 来实现,用于优化很多别的算法。
#### 练习[P1440 求m区间内的最小值](https://www.luogu.com.cn/problem/P1440)
[P3957 [NOIP 2017 普及组] 跳房子](https://www.luogu.com.cn/problem/P3957)## 4.并查集#### 定义并查集可以简单的分为```集```、```并```、```查```。
这里的```集```是**集合**的意思,也可以看做是**树**或是**联通块**,所以并查集这个算法主要是对**集合**进行的操作。那怎么存储集合呢?此时我们可以把集合看成树,存储时取任意一个结点作为根结点,通过一个```fa[]```数组来存储每个节点的祖先就可以了。
56 行未改动}```#### 例题##### 1.[简单题](https://www.luogu.com.cn/problem/P2814)这道题没什么好说的,十分基础的并查集,直接上代码:```cpp#include<bits/stdc++.h>using namespace std;map<string,string>a;string find(string x){ if(x!=a[x]) a[x]=find(a[x]); return a[x];}string s1,s2;int main(){ char c; cin>>c; while(c!='$') { cin>>s1; if(c=='#') { s2=s1; if(a[s1]=="")a[s1]=s1; } else if(c=='+') a[s1]=s2; else cout<<s1<<' '<<find(s1)<<endl; cin>>c; } return 0;}```这道题没什么好说的,十分基础的并查集。##### 2.[经典题](https://www.luogu.com.cn/problem/P1955)这道题我们可以先把相等的变量放在同一个集合里,输入完后再扫描一遍不相等的关系,再判断有没有冲突。
```cpp39 行未改动一道水蓝,[**正解**](https://www.luogu.com.cn/problem/solution/P2391)并查集,不过模拟也能$AC$。## 参考资料* [OI WIKI](https://www.oi.wiki)* [并查集](https://www.luogu.com.cn/article/uhoc3yzy) ——— Ryan_X### 致谢作者在这里感谢[zhangleyi0417](https://www.luogu.com.cn/user/1342297)、[Ryan_X](https://www.luogu.com.cn/user/1017099)对本文撰写的贡献。