Acwing-----算法基础课之第二讲(数据结构二)

2021-03-29 21:26

阅读:729

标签:父节点   com   int   并查集   --   字符串   堆排   连通块   编号   

835 Trie字符串统计

  • 链接:https://www.acwing.com/problem/content/837/
#include 
using namespace std;

const int N = 100010;
int n;
int son[N][26], cnt[N], idx; //下标是0的点既是根节点又是空结点
char str[N];

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 = 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;
    cin >> n;
    while (n--) {
        char op[2];
        cin >> op >> str;
        if (op[0] == ‘I‘) insert(str);
        else cout 

143.最大异或对

  • 链接:https://www.acwing.com/problem/content/145/
#include 
using namespace std;

const int N = 100010, M = 31 * N;

int n;
int a[N], son[M][2], idx;

void insert(int x) {
    int p = 0;
    for (int i = 30; i >= 0; --i) {
        int u = x >> i & 1;
        if (!son[p][u]) son[p][u] = ++idx;
        p = son[p][u];
    }
}

int query(int x) {
    int p = 0, ans = 0;
    for (int i = 30; i >= 0; --i) {
        int u = x >> i & 1;
        if (son[p][!u]) {
            ans = ans * 2 + !u;
            p = son[p][!u];
        } else {
            p = son[p][u];
            ans = ans * 2 + u;
        }
    }
    return ans;
}

int main() {
    cin >> n;
    for (int i = 0; i > a[i];
    
    int ans = 0;
    for (int i = 0; i 

836.合并集合

  • 链接:https://www.acwing.com/problem/content/838/

并查集: 1.将两个集合合并;

? 2.询问两个元素是否在一个集合中。

基本原理:每个集合用一棵树来表示,树根的编号就是整个集合的编号,每个节点存储它的父节点,p[x]表示它的父节点。

#include 
using namespace std;

const int N = 100010;
int n, m, p[N];

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

int main() {
    cin >> n >> m;
    
    for (int i = 1; i > 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;
}

837.连通块中点的数量

  • 链接:https://www.acwing.com/problem/content/839/
#include 
using namespace std;

const int N = 100010;
int n, m;
int p[N], sz[N];

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

int main() {
    cin >> n >> m;
    
    for (int i = 1; i > op;
        
        if (op[0] == ‘C‘) {
            cin >> a >> b;
            if (find(a) == find(b)) continue;
            sz[find(b)] += sz[find(a)];
            p[find(a)] = find(b);
        }
        else if (op[1] == ‘1‘) {
            cin >> a >> b;
            if (find(a) == find(b)) puts("Yes");
            else puts("No");
        } else {
            cin >> a;
            cout 

838.堆排序

  • 链接:https://www.acwing.com/problem/content/840/
#include 
#include 

using namespace std;

const int N = 100010;
int n, m, h[N], sz;

void down(int u) {
    int t = u;
    if (u * 2 > n >> m;
    for (int i = 1; i > h[i];
    sz = n;
    
    for (int i = n / 2; i; --i) down(i);
    while (m--) {
        cout 

839.模拟堆

  • 链接:https://www.acwing.com/problem/content/841/
#include 
#include 
#include 
using namespace std;

const int N = 100010;

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

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  h[u]) {
        heap_swap(u / 2, u);
        u /= 2;
    }
} 

int main() {
    cin >> n;
    while (n --) {
        char op[10];
        int k, x;
        cin >> op;
        
        if (!strcmp(op, "I")) {
            cin >> x;
            sz++;
            m++;
            ph[m] = sz, hp[sz] = m;
            h[sz] = x;
            up(sz);
        } else if (!strcmp(op, "PM")) cout > k;
            k = ph[k];
            heap_swap(k, sz);
            sz--;
            down(k), up(k);
        } else {
            cin >> k >> x;
            k = ph[k];
            h[k] = x;
            down(k), up(k);
        }
    }
    return 0;
}

Acwing-----算法基础课之第二讲(数据结构二)

标签:父节点   com   int   并查集   --   字符串   堆排   连通块   编号   

原文地址:https://www.cnblogs.com/clown9804/p/13598659.html


评论


亲,登录后才可以留言!