数据结构(二)
Tire树
高效地存储和查找字符串的集合的数据结构。
1.存储
在根节点之后像一棵树一样依次延伸,没有就创建,有就沿着走下去;
例如我们存储abcdef,abdef,aced,bcdf,bcff,abc这些字符串,我们还需要在一个字符串结尾处标记来确认查找。
如下图所示:
2.查找
例如查找aced,依次从根节点往下找都是存在的,而且结尾处有标记则查找到了;再例如查找abcf,我们从c找f这部是不存在的所以查找不了;例如查找abcd,虽然都能找到但结尾处没有标记所以查找不了。
代码实现存储和查找:
字符串统计
建议看图理解
#include <iostream>
using namespace std;
const int N = 100010;
//son[N][26]中的26表示每个节点最多向外扩展出26个点即26个英文字母
int son[N][26],cnt[N],idx; //cnt[i]表示以i结尾的字符串个数,idx即为用到的节点
char str[N];
void insert(char *str)
{
int p = 0;
for(int i = 0; str[i]; i++) //当字符串结尾不为'\0'即不结束时
{
int u = str[i] - 'a' ; //把a~z的字母映射成0~25的数字
if(!son[p][u]) son[p][u] = ++idx; //下一节点没有要存储的字母的话就创建
p = son[p][u]; //不管创建与否,继续赋值走下一节点
cnt[p] ++; //p最终即为字符串的结尾字母,个数++
}
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 = son[p][u]; //走回去
}
return cnt[p];
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
char op[2];
scanf("%s%s",op,str);
if(*op == 'I') insert(str);
else printf("%d\n",query(str));
}
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010, M = 3100010;
int n;
int a[N], son[M][2], idx;
void insert(int x)
{
int p = 0;
for (int i = 30; i >= 0; i -- )
{
int &s = son[p][x >> i & 1]; //&: 二进制“与”(都为1时,结果是1,否则是0。)
if (!s) s = ++ idx;
p = s;
}
}
int search(int x)
{
int p = 0, res = 0;
for (int i = 30; i >= 0; i -- )
{
int s = x >> i & 1;
if (son[p][!s])
{
res += 1 << i;
p = son[p][!s];
}
else p = son[p][s];
}
return res;
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ )
{
scanf("%d", &a[i]);
insert(a[i]);
}
int res = 0;
for (int i = 0; i < n; i ++ ) res = max(res, search(a[i]));
printf("%d\n", res);
return 0;
}
并查集
用于合并两个集合,以及查询两元素是否在同一集合下
连通块中点的个数
此题增加了判断集合个数
#include <iostream>
using namespace std;
const int N = 100010;
int p[N],cnt[N];
int find(int x)
{
if(p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; i++) p[i] = i,cnt[i] = 1; //初始化根节点,且表明当前集合个数为1
while(m--)
{
string op;
int a,b;
cin >> op;
if(op == "C")
{
cin >> a >> b;
a = find(a),b=find(b);
if(a!=b)
{
p[a] = b;
cnt[b] += cnt[a];
}
}
else if(op == "Q1")
{
cin >> a >> b;
if(find(a) == find(b)) puts("Yes");
else puts("No");
}
else
{
cin >> a;
cout << cnt[find(a)] << endl;
}
}
return 0;
}
堆
是一棵完全二叉树
除了最后一层节点外,上面所有节点都是满的不存在空的,最后一层节点是从左到右排列
小根堆:每个点都是小于等于左右儿子的,所以根节点就是最小数
存储方式:用一维数组
1为根节点,2x为左儿子 2x+1为右儿子
基本操作:down(x)往下调整 ;up(x)向上调整
在形成三角形的三个数中选择最小的数和大的数交换,大的下下移,小的上移,并且上移只要跟根节点比较即可。
操作:(下标从1开始)
1.插入一个数:在堆的最后一个数加上x,再不断上移
2.求集合中的最小数:即第一个数
3.删除最小数:把最后一个元素覆盖到堆顶去,然后把最后一个元素删掉,最后down堆顶元素
4.删除任意一个数:和操作3相似,把第k个元素覆盖到堆顶,删除第k个元素,最后执行up或down操作
5.修改任意一个数:把第k个元素变成x,然后执行down或up操作
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, m;
int h[N], cnt;
void down(int u)
{
int t = u;
if (u * 2 <= cnt && h[u * 2] < h[t]) t = u * 2;
if (u * 2 + 1 <= cnt && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
if (u != t)
{
swap(h[u], h[t]);
down(t);
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);
cnt = n;
for (int i = n / 2; i; i -- ) down(i);
while (m -- )
{
printf("%d ", h[1]);
h[1] = h[cnt -- ];
down(1);
}
puts("");
return 0;
}
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = 100010;
int h[N], ph[N], hp[N], cnt;
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;
if (u * 2 <= cnt && h[u * 2] < h[t]) t = u * 2;
if (u * 2 + 1 <= cnt && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
if (u != t)
{
heap_swap(u, t);
down(t);
}
}
void up(int u)
{
while (u / 2 && h[u] < h[u / 2])
{
heap_swap(u, u / 2);
u >>= 1;
}
}
int main()
{
int n, m = 0;
scanf("%d", &n);
while (n -- )
{
char op[5];
int k, x;
scanf("%s", op);
if (!strcmp(op, "I"))
{
scanf("%d", &x);
cnt ++ ;
m ++ ;
ph[m] = cnt, hp[cnt] = m;
h[cnt] = x;
up(cnt);
}
else if (!strcmp(op, "PM")) printf("%d\n", h[1]);
else if (!strcmp(op, "DM"))
{
heap_swap(1, cnt);
cnt -- ;
down(1);
}
else if (!strcmp(op, "D"))
{
scanf("%d", &k);
k = ph[k];
heap_swap(k, cnt);
cnt -- ;
up(k);
down(k);
}
else
{
scanf("%d%d", &k, &x);
k = ph[k];
h[k] = x;
up(k);
down(k);
}
}
return 0;
}