数据结构2Accepted5

Trie

#include <iostream>

using namespace std;

const int N = 100010;

// son[x]表示x的儿子,son[x][0]表示x的第一个儿子
// cnt[x]以x结尾的单词有多少个
// idx表示当前使用的下标,下标是0的点即是根节点也是空节点
int son[N][26], cnt[N], idx;
char str[N];

// 插入
void insert(char str[])
{
    int p = 0;
    for (int i = 0; str[i]; i ++ )// C++中字符串结尾是\0
    {
        int u = str[i] - 'a';// 把26个字母映射到0-25
        if(!son[p][u]) son[p][u] = ++ idx;// 不存在,创建
        p = son[p][u];
    }
    
    cnt[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;
        cin >> op >> str;
        if (op == 'I') insert(str);
        else 
        {
            printf("%d\n",query(str));

        }
    }
    
    return 0;
}

并查集

#include <iostream>

using namespace std;

const int N = 100010;

int n, m;
int p[N];//p[x]表示x的父节点

// 返回x的祖宗节点,路径压缩
int find(int x)
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    scanf("%d%d", &n, &m);
    
    for (int i = 0; i <= n; i ++ ) p[i] = i;
    
    while(m -- )
    {
        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;
}

连通块中点的数量

#include <iostream>

using namespace std;

const int N = 100010;

int n, m;
int p[N], size1[N];

int find(int x)
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    scanf("%d%d", &n, &m);
    
    for (int i = 0; i <= n; i ++ ) 
    {
        p[i] = i;
        size1[i] = 1;
    }
    
    while(m -- )
    {
        char op[5];
        int a, b;
        scanf("%s", op);
        
        if (op[0] == 'C')// 操作指令为“C a b”
        {
            scanf("%d%d", &a, &b);
            if (find(a) == find(b)) continue;
            size1[find(b)] += size1[find(a)];
            p[find(a)] = find(b);
        }
        else if (op[1] == '1')// 操作指令为“Q1 a b”
        {
            scanf("%d%d", &a, &b);
            if (find(a) == find(b)) puts("Yes");
            else puts("No");
        }
        else// 操作指令为“Q2 a”
        {
            scanf("%d", &a);
            printf("%d\n",size1[find(a)]);
        }
    }
    
    return 0;
}

堆排序

#include <iostream>

using namespace std;

const int N = 100010;

int n, m;
int h[N], size1;

void down(int u)
{
    int t = u;// t表示三个点(父节点、左右子节点)里面最小值
    if (u * 2 <= size1 && h[u * 2] < h[t]) t = u * 2;//如果左儿子存在,且小于t,将小的值赋给t
    if (u * 2 + 1 <= size1 && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
    if (u != t)//如果u不是t,那就说明根节点不是最小的,那就和最小的交换一下
    {
        swap(h[u], h[t]);
        down(t);
    }
}

int main()
{
    int n, m;
    scanf("%d%d", &n, &m);
    
    for (int i = 1; i <= n; i ++ ) scanf("%d", &h[i]);
    size1 = n;
    
    for (int i = n/2; i > 0; i -- ) down(i);
    
    while ( m -- )
    {
        printf("%d ", h[1]);// 输出当前堆顶元素
        h[1] = h[size1];// 删除堆顶,将最后一个元素赋到第一个元素上去
        size1 -- ;
        down(1);
    }
    
    return 0;
}

模拟堆

#include <iostream>
#include <cstring>

using namespace std;

const int N = 100010;

int n, m;
int h[N],ph[N], hp[N], size1;

//ph[k]:pointer-heap(下标-堆),第k个插入的点在堆中的下标
//hp[k]:heap-pointer(堆-下标),堆中下标是k的点是第几个插入的
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 <= size1 && h[u * 2] < h[t]) t = u * 2;
    if (u * 2 + 1 <= size1 && 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 /= 2;
    }
}

int main()
{
    int n;
    scanf("%d", &n);
    
    while (n -- )
    {
        char op[10];
        int k, x;
        
        scanf("%s", op);
        if (!strcmp(op, "I"))// “I x”,插入一个数x
        {
            scanf("%d", &x);
            size1 ++ ;
            m ++ ;
            ph[m] = size1, hp[size1] = m;
            h[size1] = x;
            up(size1);
        }
        else if (!strcmp(op, "PM"))// “PM”,输出当前集合中的最小值
        {
            printf("%d\n", h[1]);
        }
        else if (!strcmp(op, "DM"))// “DM”,删除当前集合中的最小值(当最小值不唯一时,删除最早插入的最小值)
        {
            heap_swap(1, size1);
            size1 -- ;
            down(1);
        }
        else if (!strcmp(op, "D"))// “D k”,删除第k个插入的数;
        {
            scanf("%d", &k);
            k = ph[k];
            heap_swap(k, size1);
            size1 -- ;
            down(k), up(k);
        }
        else// “C k x”,修改第k个插入的数,将其变为x
        {
            scanf("%d%d", &k, &x);
            k = ph[k];
            h[k] = x;
            down(k), up(k);
        }
    }
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值