一 Tire树
Tire树:用于高效地存储和查找字符串集合的数据结构。
算法思想:用一个二维数组son[][]来存储每个结点的孩子,cnt[]来存放以某个结点为结尾的单词的个数,idx表示当前用到了哪个结点
基本操作:
1 .插入字符串
void insert(char str[])
{
int p = 0;//从头结点开始
for (int i = 0; str[i]; i++)
{
int u = str[i] - 'a';//求出每个字母对应的编号
if (!son[p][u]) son[p][u] = ++idx;//如果当前结点p不存在u这个孩子,则创造一个
p = son[p][u];//p指向下一个结点
}
cnt[p]++;//将以点p为结尾的单词的个数加1
}
2. 查询字符串的个数
int query(char str[])//用来求单词的个数
{
int p = 0;//从头结点开始
for (int i = 0; str[i]; i++)
{
int u = str[i] - 'a';//将每个字母转化为对应的编号
if (!son[p][u]) return 0;//如果当前结点p不存在u这个孩子的话,则不存在这个单词,返回0
p = son[p][u];//p指向下一个结点
}
return cnt[p];//输出以点p为结尾的单词的个数
}
代码实例:
输入:
5
I abc
Q abc
Q ab
I ab
Q ab
输出:
1 0 1
#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<ctime>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e3 + 10, null = 0x3f3f3f3f,M=2*N;
const double eps=1e-6;
int son[N][26];//由于每个结点最多有26个孩子,A-Z
int cnt[N];//记录以某个结点为结尾的单词的个数,即字符串的个数
int idx;//表示当前用到了哪个点
int t;
char str[N];//用来存储字符串
void insert(char a[])
{
int p=0;//从头结点开始
for(int i=0;a[i];i++)//遍历该字符串所有字符
{
int u=a[i]-'a';//求出每个字符对应的编号
if(!son[p][u]) son[p][u]=++idx;//如果结点p不存在u这个儿子的话,则创建一个
p=son[p][u];//p指向下一个结点
}
cnt[p]++;//以结点p结尾的单词的个数加一
}
int query(char a[])
{
int p=0;//从头结点开始
for(int i=0;a[i];i++)//遍历该字符串的所有字符
{
int u=a[i]-'a';//求出每个字符对应的编号
if(!son[p][u]) return 0;//如果结点p不存在u这个儿子的话,说明不存在这个字符串,这个字符串的个数为0,函数返回0
p=son[p][u];//p指向下一个结点
}
return cnt[p];//返回以结点p为结尾的单词的个数
}
int main()
{
cin>>t;
while(t--)
{
char op[2];//将单个字符作为字符串进行读入,可以避免读入一些奇奇怪怪的东西
scanf("%s%s",op,str);
if(op[0]=='I') insert(str);
else
{
int ans=query(str);//输出该字符串的个数
printf("%d\n",ans);
}
}
return 0;
}
二 并查集
主要用处:
-
将两个集合合并
-
询问两个元素是否在一个集合当中
基本原理:
每个集合用一棵树来表示,树根的编号就是整个集合的编号,每个结点存储它的父结点。p[x]表示结点x的父结点。
基本操作:
1 .判断树根
if (p[x] == x)//结点x的父结点等于x,说明x是树根`
2. 求集合x的编号
while (p[x] != x) x = p[x];//只要x的父结点不等于x,x就一直向上走
1.朴素并查集
int p[N]; //存储每个点的父节点
// 返回x的祖宗节点,核心操作
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
// 初始化,假定节点编号是1~n
for (int i = 1; i <= n; i ++ ) p[i] = i;//初始时每个节点的父结点就是本身自己
// 合并a和b所在的两个集合:
p[find(a)] = find(b);
2. 维护size的并查集
int p[N], size[N];
//p[]存储每个结点的父节点, size[]只有祖宗节点的有意义,表示祖宗节点所在集合中的元素的个数
// 返回x的祖宗节点
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
// 初始化,假定节点编号是1~n
for (int i = 1; i <= n; i ++ )
{
p[i] = i;
size[i] = 1;//初始时,每个点所在的集合中只有本身一个点,因此数目为1
}
// 合并a和b所在的两个集合:
p[find(a)] = find(b);
size[find(b)] += size[find(a)];
3.维护到祖宗结点距离的并查集
int p[N], d[N];
//p[]存储每个点的父节点, d[x]存储x到根节点的距离
// 返回x的祖宗节点
int find(int x)
{
if (p[x] != x)
{
int u = find(p[x]);
d[x] += d[p[x]];
p[x] = u;
}
return p[x];
}
// 初始化,假定节点编号是1~n
for (int i = 1; i <= n; i ++ )
{
p[i] = i;
d[i] = 0;
}
// 合并a和b所在的两个集合:
p[find(a)] = find(b);
d[find(a)] = distance; // 根据具体问题,初始化find(a)的偏移量
**小技巧**
:
如果读入一个字母的话,可以将其当做字符串用scanf进行读入,可以过滤掉空格和回车,比较方便。
代码示例:
输入:
4 5
M 1 2
M 3 4
Q 1 2
Q 1 3
Q 3 4
输出:yes no yes
#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<ctime>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e3 + 10, null = 0x3f3f3f3f,M=2*N;
const double eps=1e-6;
int n,t;
int p[N];
int find(int x)
{
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
int main()
{
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++) p[i]=i;
while(t--)
{
char op[2];
int a,b;
scanf("%s%d%d",op,&a,&b);
if(op[0]=='M') p[find(a)]=find(b);
else
{
if(find(a)==find(b)) puts("YES");
else puts("NO");
}
}
return 0;
}
代码实例2:
数组sz[]表示每个结点的祖宗结点所在集合中元素的个数。
输入:5 5
C 1 2
Q1 1 2
Q2 1
C 2 5
Q2 5
输出:YES 2 3
#include<iostream>
#include<algorithm>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
#include<iomanip>
#include<fstream>
#include<ctime>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e3 + 10, null = 0x3f3f3f3f,M=2*N;
const double eps=1e-6;
int n, m;
int p[N];//存储每个结点的父结点
int sz[N];//存储每个结点所在结合中元素的个数
int find(int x)//返回结点x的祖宗结点,即结点x所在集合的根节点
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}//核心操作
int main()
{
scanf("%d%d",&n,&m);
for (int i = 1; i <= n; i++)
{
p[i] = i;//每个集合中只有一个结点,该结点就是父结点,
sz[i] = 1;//由于每个集合当中只有一个点,因此这个结点的祖宗结点就是自己本身,因此这个点的祖宗结点所在集合中元素的个数为1
}//初始化
while (m--)
{
char op[5];
scanf("%s",op);//作为字符串进行读入,可以过滤掉回车和空格
int a, b;
if (op[0] == 'C')
{
scanf("%d%d",&a,&b);
if (find(a) == find(b)) continue;//如果结点a和结点b在一个集合当中
sz[find(b)] += sz[find(a)];//结点b的祖宗结点所在集合的元素个数加上结点a的祖宗结点所在集合的元素个数
p[find(a)] = find(b);//结点a所在集合当中的根节点的父结点等于结点b所在集合当中的根节点,即将集合a连接在结合b之后
}
else if (op[1] == '1')
{
scanf("%d%d",&a,&b);
if (find(a) == find(b)) puts("YES");//如果a和b在一个集合当中,输出yes,否则输出no
else puts("NO");
}
else
{
scanf("%d",&a);
printf("%d\n",sz[find(a)]);//输出结点a的祖宗结点所在集合当中的元素个数
}
}
return 0;
}
三. 堆
小根堆
基本操作:
1.插入:
heap[++size] = x; up(size);
2.求最小值
求集合当中的最小值:heap[1];
3.删除
删除最小值:
heap[1] = heap[size]; size–; down(1);
删除任意一个元素:
heap[k] = heap[size]; size–; down[k]; up[k];
4.修改
修改任意一个元素:
heap[k] = x; down[k]; up[k];
// h[N]存储堆中的值, h[1]是堆顶,x的左儿子是2x, 右儿子是2x + 1
// ph[k]存储第k个插入的点在堆中的下标
// hp[k]堆中下标为k的点是第几个插入的
int h[N], ph[N], hp[N], size;
5.交换
// 交换两个点,及其映射关系
void heap_swap(int a, int b)
{
swap(ph[hp[a]],ph[hp[b]]);
swap(hp[a], hp[b]);
swap(h[a], h[b]);
}
**6.建堆:**时间复杂度为o(n)
for (int i = n / 2; i; i–) down(i);
核心:
1.down()操作:
**void down(int u)
{
int t = u;//用t存储三个点里最小的那个点的下标
if (u * 2 <= ans && h[u * 2] < h[t]) t = u * 2;//如果左儿子存在的话并且左儿子的值小,则更新t
if (u * 2 + 1 <= ans && h[u * 2 + 1] < h[t]) t = u * 2 + 1;//如果右儿子存在的话并且右儿子的值小的话,则更新t
if (t != u)//如果t不等于u的话,说明根节点不是最小值,则将二者的值进行交换
{
heap_swap(u,t);
down(t);//递归处理
}
}**
2.up操作:
**void up(int u)
{
while (u / 2 && h[u / 2] > h[u])//只要父结点存在并且父结点的值比当前结点的值大的话,就进行交换
{
heap_swap(u,u/2);
u /= 2;
}
}**
输入:
10
I -10
PM
I -10
D
C 2 8
I 6
PM
DM
输出:
-10 6
代码实例:
#include<iostream>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<ctype.h>
using namespace std;
#define endl '\n'
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1e6 + 10;
int h[N];
int ph[N];
int hp[N];
int ans;
void heap_swap(int a, int b)//交换堆中元素值以及映射关系,堆中特有的交换
{
swap(ph[hp[a]], ph[hp[b]]);
swap(hp[a], hp[b]);
swap(h[a], h[b]);
}
void down(int u)
{
int t = u;//用t存储三个点里最小的那个点的下标
if (u * 2 <= ans && h[u * 2] < h[t]) t = u * 2;//如果左儿子存在的话并且左儿子的值小,则更新t
if (u * 2 + 1 <= ans && h[u * 2 + 1] < h[t]) t = u * 2 + 1;//如果右儿子存在的话并且右儿子的值小的话,则更新t
if (t != u)//如果t不等于u的话,说明根节点不是最小值,则将二者的值进行交换
{
heap_swap(t, u);
down(t);//递归处理
}
}
void up(int u)
{
while (u / 2 && h[u / 2] > h[u])//只要父结点存在并且父结点的值比当前结点的值大的话,就进行交换
{
heap_swap(u, u/2);
u /= 2;
}
}
int main()
{
int n;
scanf("%d",&n);
int res = 0;
while (n--)
{
char op[5];
scanf("%s", op);//作为字符串用scanf进行读入,可以忽略空格和回车
int k, x;
if (!strcmp(op,"I"))//插入
{
scanf("%d",&x);
res++;
h[++ans] = x;
ph[res] = ans;
hp[ans] = res;
up(ans);
}
else if (!strcmp(op, "PM"))//求最小值
{
printf("%d\n",h[1]);
}
else if (!strcmp(op, "D"))//删除最小值
{
heap_swap(1, ans);
ans--;
down(1);
}
else if (!strcmp(op, "C"))//修改第k个插入的点
{
scanf("%d%d",&k,&x);
k = ph[k];
h[k] = x;
down(k);
up(k);
}
else//删除第k个插入的点
{
scanf("%d",&k);
k = ph[k];
heap_swap(k, ans);
ans--;
down(k);
up(k);
}
}
return 0;
}