跳到正文

算法·理论

数据结构

Dyh009约 26 分钟
原文
文章信息与版本

文章信息

发布
2026年9月19日
收录
2026年9月19日
更新
2026年11月14日
篇幅
约 26 分钟

版本

  1. v2当前
  2. v1

数据结构

//如果写的有问题,请私信告诉我

字符串

C++
#include<string>
string s;
s.size(),s.length();//获取字符串长度,两者完全等价
s.empty();//判断字符串是否为空,空返回 true	
s.clear();//清空字符串,长度变为 0	
s.append(t);//把字符串 t 追加到 s 末尾	
s.append(t,n);//追加 t 的前 n 个字符	
s.substr(pos,len);//截取子串:从 pos 下标开始,取 len 个字符;len 省略则取到末尾	
s.find(t);//查找子串 t,返回起始下标;找不到返回 string::npos
s.find(t,pos);//从下标 pos 开始向后查找 t	
s.rfind(t);//从后往前反向查找子串 t	
s.replace(pos,len,t);//从 pos 开始,删除 len 个字符,替换为 t
s.erase(pos,len);//删除:pos 位置开始删除 len 字符;len 省略删到末尾
s.insert(pos,t);//在 pos 下标位置插入字符串 t

栈

C++
#include<stack>
syack<类型>s;
s.push(新元素);
s.pop();//弹出
s.top();//栈顶元素
s.size();//长度,%ull
s.empty()//判空,为空时返回1

队列

C++
#include<queue>
queue<类型>q;
q.push(新元素);
q.pop();//弹出
q.front();//队首元素
q.size();//长度,%ull
q.empty()//判空,为空时返回1

双端队列

C++
#include<deque>
deque<类型>dq;
dq.push_front(新元素);//在前端插入新元素
dq.push_back(新元素);//在后端插入新元素
dq.pop_front();//弹出队首元素
dq.pop_back();//弹出队尾元素
dq.front();//队首元素
dq.back()://队尾元素
dq.size();//长度,%ull
dq.empty()//判空,为空时返回1

动态数组

C++
#include<vector>
vector<类型>v;
v.push_back(新元素);//插入新元素
v.size();//元素个数
v.empty()//判空,为空时返回1

关联容器(红黑树)

C++
#include<map>
map<键类型,值类型>m;
m.insert(pair(键,新元素));//插入新元素
m[键]=新元素;//插入新元素
m.find(键);//查找
m.erase(键);//删除
for (const auto& pair : m) {//遍历循环
	pair.first//键
   pair.second//元素
}

无序关联容器(哈希表)

C++
#include<unordered_map>
unordered_map<键类型,值类型>m;
m.insert(pair(键,新元素));//插入新元素
m[键]=新元素;//插入新元素
m.find(键);//查找
m.count(键);//是否存在
m.erase(键);//删除
m.clean();//清空
for (const auto& pair : m) {//遍历循环
	pair.first//键
   pair.second//元素
}

优先队列

C++
struct node{
    int data,l;
    bool operator <(const node &n1)const{
        return l<n1.l;
    }
}//以上生成结构体只在小根堆时写
priority_queue<node>q;//大根堆时可直接写int等

并查集

C++
//查询
int f(int x){
    if(p[x]==x)return x;
    return p[x]=f(p[x]);
}
//检测是否同根
f(a)==f(b);
//合并
p[f(a)]=f(b);
//初始化
for(int i=0;i<n;i++)p[i]=i;

单调队列

C++
struct node{
    int data,t;
}
deque<int>q;//或其他类型
for(int i=0;i<n;i++){
    if(q.empty()){
        q.push_back;
        continue;
    }
    while(!q.empty()&&a[i]>q.back().data)q.pop_back();//淘汰
    q.push_back({a[i],i});//入队
    while(!q.empty()&&q.front().t+5<=i)q.pop_front();//退役,其中时间期限可改
}

平衡树(AVL)

C++
struct tr{//节点 
	int left_son,right_son;//左、右子节点 
	int data,self;//存储数据、在数组中的下标 
	int left_size,right_size,left_depth,right_depth;//左、右子树大小、深度 
	tr(){//初始化
		left_son=-1;
		right_son=-1;
		data=0;
		self=-1;
		left_size=0;
		right_size=0;
		left_depth=0;
		right_depth=0;
		return;
	}
};
class tree{//平衡树 
private:
	tr t[10010];
	void left_rotate(int rooti){//左旋(改变两个地址的内容,即t[f.self]中存的是新父节点) 
		tr f=t[rooti],s=t[f.right_son];
		t[f.self].data=s.data;
		t[f.self].left_son=s.self;
		t[f.self].right_son=s.right_son;
		t[f.self].left_size=f.left_size+s.left_size+1;
		t[f.self].left_depth=max(f.left_depth,s.left_depth)+1;
		t[f.self].right_size=s.right_size;
		t[f.self].right_depth=s.right_depth;
		t[s.self].data=f.data;
		t[s.self].left_son=f.left_son;
		t[s.self].right_son=s.left_son;
		t[s.self].left_size=f.left_size;
		t[s.self].left_depth=f.left_depth;
		t[s.self].right_size=s.left_size;
		t[s.self].right_depth=s.left_depth;
		return;
	}
	void right_rotate(int rooti){//右旋(同上) 
		tr f=t[rooti],s=t[f.left_son];
		t[f.self].data=s.data;
		t[f.self].right_son=s.self;
		t[f.self].left_son=s.left_son;
		t[f.self].right_size=f.right_size+s.right_size+1;
		t[f.self].right_depth=max(f.right_depth,s.right_depth)+1;
		t[f.self].left_size=s.left_size;
		t[f.self].left_depth=s.left_depth;
		t[s.self].data=f.data;
		t[s.self].right_son=f.right_son;
		t[s.self].left_son=s.right_son;
		t[s.self].right_size=f.right_size;
		t[s.self].right_depth=f.right_depth;
		t[s.self].left_size=s.right_size;
		t[s.self].left_depth=s.right_depth;
		return;
	}
	void maintain(int rooti){//维护 
		if(t[rooti].left_depth-t[rooti].right_depth>1){//l 
			if(t[t[rooti].left_son].left_depth>=t[t[rooti].left_son].right_depth){//ll
				right_rotate(rooti);
			}
			else{//lr
				left_rotate(t[rooti].left_son);
				right_rotate(rooti);
			}
		}
		else if(t[rooti].right_depth-t[rooti].left_depth>1){//r 
			if(t[t[rooti].right_son].right_depth>t[t[rooti].right_son].left_depth){//rr
				left_rotate(rooti);
			}
			else{//rl
				right_rotate(t[rooti].right_son);
				left_rotate(rooti);
			}
		}
		return;
	}
public:
	void print(void){//打印(测试用) 
		printf("root: %d\nline:\n",root);
		for(int i=0;i<length;i++)printf("%d %d %d %d %d %d %d\n",t[i].data,t[i].left_son,t[i].right_son,t[i].left_size,t[i].right_size,t[i].left_depth,t[i].right_depth);
		printf("tree like:\n");
		queue<int> q;
		q.push(root);
		q.push(-2);
		while(1){
			if(q.front()==-2){
				printf("\n");
				q.pop();
				if(q.empty()||q.front()==-2)break;
				q.push(-2);
			}
			else if(q.front()==-1){
				printf("N ");
				q.pop();
			}
			else{
				printf("%d ",t[q.front()].data);
				q.push(t[q.front()].left_son);
				q.push(t[q.front()].right_son);
				q.pop();
			}
		}
		printf("end\n");
		return;
	}
	int root,length;//根、节点数 
	void add(int x,int rooti){//建立新节点 
		if(length==0){//根 
			t[length].self=length;
			t[length++].data=x;
			return;
		}
		if(x<t[rooti].data){//加到左子树上 
			t[rooti].left_size++;
			if(t[rooti].left_son==-1){//新建 
				t[rooti].left_son=length;
				t[rooti].left_depth=1;
				t[rooti].left_size=1;
				t[length].self=length;
				t[length].data=x;
				length++;
				return;
			}
			else add(x,t[rooti].left_son);
			t[rooti].left_depth=max(t[t[rooti].left_son].left_depth,t[t[rooti].left_son].right_depth)+1;//更新深度 
			maintain(rooti);
			return;
		}
		else{//加到右子树上 
			t[rooti].right_size++;
			if(t[rooti].right_son==-1){//新建 
				t[rooti].right_son=length;
				t[rooti].right_depth=1;
				t[rooti].right_size=1;
				t[length].self=length;
				t[length].data=x;
				length++;
				return;
			}
			else add(x,t[rooti].right_son);
			t[rooti].right_depth=max(t[t[rooti].right_son].right_depth,t[t[rooti].right_son].left_depth)+1;//更新深度 
			maintain(rooti);
			return;
		}
	}
	tree(){//初始化 
		root=0;
		length=0;
		return;
	}
};
tree t;//定义 
t.root;//根 
t.length;//节点数 
t.add(xi,t.root);//添加新节点 
t.print();//打印(测试用) 

平衡树(旋转Treap)

C++
struct tr{
	int left_son,right_son;
	int data,self,rank;//加一个随机数排名
	int left_size,right_size,left_depth,right_depth;
	tr(){
		...
	}
};
bool cmp(tr a,tr b){
	return a.rank<b.rank;
}
class tree{
private:
	tr t[10010];
	bool rank_vis[N];//开大一点重复概率小
	int get_rank(void){//随机排名
		int r=rand()%N;
		while(rank_vis[r])r=rand()%N;
		rank_vis[r]=1;
		return r;
	}
	void left_rotate(int rooti);//同AVL,交换rank
	void right_rotate(int rooti);
	int update_size(int rooti){//维护左、右子树大小
		if(t[rooti].left_son!=-1)t[rooti].left_size=update_size(t[rooti].left_son!=-1);
		if(t[rooti].right_son!=-1)t[rooti].right_size=update_size(t[rooti].right_son!=-1);
		return t[rooti].left_size+t[rooti].right_size+1
	}
	int update_depth(int rooti){//维护左、右子树深度
		if(t[rooti].left_son!=-1)t[rooti].left_depth=update_depth(t[rooti].left_son!=-1);
		if(t[rooti].right_son!=-1)t[rooti].right_depth=update_depth(t[rooti].right_son!=-1);
		return max(t[rooti].left_depth,t[rooti].right_depth)+1
	}
public:
	void print(void);
	int root,length;
	void add(int x,int rooti){
		if(length==0){
			t[length].self=length;
			t[length].data=x;
			t[length].rank=get_rank();
			length++;
			return;
		}
		if(x<t[rooti].data){
			t[rooti].left_size++;
			if(t[rooti].left_son==-1){
				...//同AVL
				t[length].rank=get_rank();
				length++;
				if(t[rooti].rank>t[t[rooti].left_son].rank)right_rotate(rooti);//维护排名堆的性质
				return;
			}
			else add(x,t[rooti].left_son);
			t[rooti].left_depth=max(t[t[rooti].left_son].left_depth,t[t[rooti].left_son].right_depth)+1;
			if(t[rooti].rank>t[t[rooti].left_son].rank)right_rotate(rooti);
			return;
		}
		else{
			t[rooti].right_size++;
			if(t[rooti].right_son==-1){
				...
				t[length].rank=get_rank();
				length++;
				if(t[rooti].rank>t[t[rooti].right_son].rank)left_rotate(rooti);
				return;
			}
			else add(x,t[rooti].right_son);
			t[rooti].right_depth=max(t[t[rooti].right_son].right_depth,t[t[rooti].right_son].left_depth)+1;
			if(t[rooti].rank>t[t[rooti].right_son].rank)left_rotate(rooti);
			return;
		}
	}
  void build(int ni){//已有ni个数值和排名存于t中的建树,同笛卡尔树
		sort(t,t+ni,cmp);//按排名排序
		stack<int>s;//右链
		for(int i=1;i<=ni;i++){
			while(s.top()!=0&&h[s.top()]>h[i])s.pop();//把0当成虚根好写
			t[i].left_son=t[s.top()].right_son;//若h[t]>h[i],t成为i的左子树
			t[s.top()].right_son=i;//若h[t]<h[i],i成为t的右节点
			s.push(i);
		}
		root=t[0].right_son;//真正的根
		update_size(root);//维护大小深度
		update_depth(root);
		return;
	}
	tree(){
		...
	}
};
tree t;//定义 
t.root;//根 
t.length;//节点数 
t.add(xi,t.root);//添加新节点 
t.print();//打印(测试用) 

平衡树(无旋Treap)

C++
struct tr{//同上 
	...
};
class tree{
private:
	tr t[10010];
	bool rank_vis[40010];
	int get_rank(void){
		...
	}
	pair<int,int> split(int rooti,int key){//分裂 
		if(t[rooti].data<key){//key在右侧 
			pair<int,int>p;
			if(t[rooti].right_son!=-1)p=split(t[rooti].right_son,key);//分裂右子树 
			else p=pair<int,int>(-1,-1);
			t[rooti].right_son=p.first;//将右子树左半部分接到rooti上 
			if(t[rooti].right_son==-1){
				t[rooti].right_size=0;
				t[rooti].right_depth=0;
			}
			else{
				t[rooti].right_size=t[p.first].left_size+t[p.first].right_size+1;
				t[rooti].right_depth=max(t[p.first].left_depth,t[p.first].right_depth)+1;
			}
			return pair<int,int>(rooti,p.second);//返回根和右子树右半部分 
		}
		else{//key在左侧 
			pair<int,int>p;
			if(t[rooti].left_son!=-1)p=split(t[rooti].left_son,key);//分裂左子树 
			else p=pair<int,int>(-1,-1);
			t[rooti].left_son=p.second;//将左子树右半部分接到rooti上 
			if(t[rooti].left_son==-1){
				t[rooti].left_size=0;
				t[rooti].left_depth=0;
			}
			else{
				t[rooti].left_size=t[p.second].left_size+t[p.second].right_size+1;
				t[rooti].left_depth=max(t[p.second].left_depth,t[p.second].right_depth)+1;
			}
			return pair<int,int>(p.first,rooti);//返回根和左子树左半部分 
		}
	}
	int merge(int roota,int rootb){//合并,a树上的数值均小于b树 
		if(roota==-1)return rootb;
		if(rootb==-1)return roota;
		if(t[roota].rank<t[rootb].rank){//a在b上方 
			t[roota].right_son=merge(t[roota].right_son,rootb);//合并a的右子树和b 
			t[roota].right_size=t[t[roota].right_son].right_size+t[t[roota].right_son].left_size+1;
			t[roota].right_depth=max(t[t[roota].right_son].right_depth,t[t[roota].right_son].left_depth)+1;
			return roota;
		}
		else{//b在a上方 
			t[rootb].left_son=merge(roota,t[rootb].left_son);//合并a和b的左子树 
			t[rootb].left_size=t[t[rootb].left_son].right_size+t[t[rootb].left_son].left_size+1;
			t[rootb].left_depth=max(t[t[rootb].left_son].right_depth,t[t[rootb].left_son].left_depth)+1;
			return rootb;
		}
	}
public:
	void print(void);
	int root,length;
	void add(int x){
		if(length==0){
			t[length].self=length;
			t[length].data=x;
			t[length].rank=get_rank();
			root=0;
			length++;
			return;
		}
		t[length].self=length;
		t[length].data=x;
		t[length].rank=get_rank();
		pair<int,int> p=split(root,x);//以x为基准分裂 
		p.first=merge(p.first,length);//合并左半部分和x 
		root=merge(p.first,p.second);//合并上一步结果和右半部分 
		length++;
	}
	tree(){
		...
	}
};
tree t;//定义 
t.root;//根 
t.length;//节点数 
t.add(xi);//添加新节点 
t.print();//打印(测试用) 

二叉堆

C++
int d[1000010];//小根堆
void up(int a){//维护
	int i=a;
	while(i>1&&d[i]<d[i/2]){//检查该值与父节点的关系
		int di=d[i];
		d[i]=d[i/2];
		d[i/2]=di;
		i=i/2;
	}
	return;
}
void down(int a){//删除某个值
	int i=a,t=i*2;
	while(t<=l){
		if(t<l&&d[t]>d[t+1])t++;//用子节点中较大的值替换
		int di=d[i];
		d[i]=d[t];
		d[t]=di;
		i=t;
		t=i*2;
	}
	if(i!=l){//将最后一个值插入空位
		int di=d[i];
		d[i]=d[l];
		d[l]=di;
		up(i);
	}
	l--;
	return;
}

树状数组

C++
//简化的前缀和,c[i]为a[i-lowbit(i)]~a[i]的和,父节点为c[i+lowbit(i)]
//lowbit为一个数字二进制从末位起第一个1与后面k个0组成的2^k,计算方法为x&-x
int c[500010],a[500010];
void add(int x,int k){//给一个节点加k
	while(x<=n){
		c[x]+=k;
		x=x+(x&-x);//更新父节点
	}
	return;
}
int sum(int r){//求a[1]~a[r]的和
	int s=0,x=r;
	while(x>0){
		s+=c[x];
		x=x-(x&-x);//向前跳
	}
	return s;
}

字典树/Tire(字符型)

C++
struct tire{//01型也差不多
	int s[26];//子节点
	int data...;//数据
	tire(){//初始化
		memset(s,-1,sizeof(s));
		...
		return;
	}
}t[N];
void add_tire(int f,int d,int l){//新建字符串
	if(d>=l)return;//到头了
	if(t[f].s[str[d]-'a']!=-1)add_tire(t[f].s[str[d]-'a'],d+1,l);//存在该子节点
	else{//不存在
		cnt++;//0是虚根,子节点从1开始
		t[f].s[str[d]-'a']=cnt;
		add_tire(cnt,d+1,l);
	}
	return;
}
int check_tire(int f,int d,int l){//查询
	if(d>=l)return 0;
	if(t[f].s[str[d]-'a']!=-1)...;//存在
	else ...;//不存在
}

笛卡尔树

C++
stack<int>s;//右链
struct Tree{//二叉树
	int ls,rs;
	Tree(){
		ls=-1;
		rs=-1;
	}
}t[1000010];
    s.push(0);
    for(int i=1;i<=n;i++){
    	while(s.top()!=0&&h[s.top()]>h[i])s.pop();//把0当成虚根好写
    	t[i].ls=t[s.top()].rs;//若h[t]>h[i],t成为i的左子树
    	t[s.top()].rs=i;//若h[t]<h[i],i成为t的右节点
    	s.push(i);
    }
原文