acwing数据结构


idx


单链表

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int e[N],ne[N],head=-1,idx;
void add(int k,int x){//插入到下标为k的点的后面(下标从0开始)
    e[idx]=x;
    ne[idx]=ne[k];
    ne[k]=idx++;
}
void add_head(int x){//插入在头节点上
    e[idx]=x;
    ne[idx]=head;
    head=idx++;
}
void remove(int k){//删除下标为k的点的后面一点
    ne[k]=ne[ne[k]];
}
int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        char c;
        scanf(" %c",&c);
        if(c=='I'){
            int k,x;
            scanf("%d%d",&k,&x);
            add(k-1,x);//注意k-1
        }
        else if(c=='D'){
            int k;
            scanf("%d",&k);
            if(!k) head=ne[head];//注意删除头节点
            else remove(k-1);//注意k-1
        }
        else{
            int x;
            scanf("%d",&x);
            add_head(x);
        }
    }
    for(int i=head;i!=-1;i=ne[i]) cout<<e[i]<<' ';
    return 0;
}

双链表

#include<iostream>

using namespace std;

const int N = 1e5 + 10;

int m;
int e[N], l[N], r[N];
int idx;


//! 初始化
void init()
{
    l[1] = 0, r[0] = 1;//* 初始化 第一个点的右边是 1   第二个点的左边是 0
    idx = 2;//! idx 此时已经用掉两个点了
}

//* 在第 K 个点右边插入一个 X 
void add(int k, int x)
{
    e[idx] = x;
    r[idx] = r[k]; //todo 这边的 k 不加 1 , 输入的时候 k+1 就好
    l[idx] = k;
    l[r[k]] = idx;//先改变l[r[k]]再改变r[k]
    r[k] = idx;
    idx++;
}//! 当然在 K 的左边插入一个数 可以再写一个 , 也可以直接调用我们这个函数,在 k 的左边插入一个 数 等价于在 l[k] 的右边插入一个数 add(l[k],x)

//*删除第 k个 点
void remove(int k)
{
    r[l[k]] = r[k];
    l[r[k]] = l[k];
}

int main(void)
{
    ios::sync_with_stdio(false);
    cin >> m;

    init();

    while(m--)
    {
        string op;
        cin >> op;
        int k, x;
        if(op=="R")
        {
            cin >> x;
            add(l[1], x); //!   0和 1 只是代表 头和尾  所以   最右边插入 只要在  指向 1的 那个点的右边插入就可以了
        }
        else if(op=="L")//! 同理  最左边插入就是 在指向 0的数的左边插入就可以了   也就是可以直接在 0的 有右边插入
        {
            cin >> x;
            add(0, x);
        }
        else if(op=="D")
        {
            cin >> k;
            remove(k + 1);
        }
        else if(op=="IL")
        {
            cin >> k >> x;
            add(l[k + 1], x);
        }
        else
        {
            cin >> k >> x;
            add(k + 1, x);
        }    
    }
    for(int i = r[0]; i != 1; i = r[i]) cout << e[i] << ' ';

    return 0;
}

字典树(好像可以用map代替)

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
char str[N];
int son[N][26],cnt[N],idx;//下标是0的点,既是根节点,又是空节点
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];//使“p指针”指向下一个节点位置
    }
    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 c[2];
        scanf("%s%s",c,str);
        if(c[0]=='I') insert(str);
        else printf("%d\n",query(str));
    }
    return 0;
}

在这里插入图片描述

#include <iostream>
#include <algorithm>
#include <string.h>

using namespace std;

const int N = 100010;

int h[N], ph[N], hp[N], cnt;//ph[k]存第k个插入数在堆中的下标,hp[k]堆中的第k个点是第几个插入点


void heap_swap(int a, int b)//a,b是在堆中的下标
{
    swap(ph[hp[a]],ph[hp[b]]);//交换堆中a,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 ++ ;//记录插入的第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];//k为第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;
}

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int stk[N],tt;
int main(){
    int m;
    cin>>m;
    while(m--){
        string s;
        cin>>s;
        if(s=="push"){
            int x;
            cin>>x;
            stk[++tt]=x;
        }
        else if(s=="pop") tt--;
        else if(s=="empty") cout<<(tt ? "NO":"YES")<<'\n';
        else cout<<stk[tt]<<'\n';
    }
    return 0;
}

单调栈—O(n)

O(n),最常用应用找数左边(右边)第一个比它小(大)的数

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int stk[N],tt;
int main(){
    int n;
    cin>>n;
    while(n--){
        int x;
        cin>>x;
        while(tt&&stk[tt]>=x) tt--;
        if(tt) cout<<stk[tt]<<' ';
        else cout<<-1<<' ';
        stk[++tt]=x;
    }
    return 0;
}

队列

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int que[N],tt=-1,hh;
int main(){
    int m;
    cin>>m;
    while(m--){
        string s;
        cin>>s;
        if(s=="push"){
            int x;
            cin>>x;
            que[++tt]=x;
        }
        else if(s=="empty") cout<<(hh<=tt ? "NO" : "YES")<<endl;
        else if(s=="pop") hh++;
        else cout<<que[hh]<<endl;
    }
    return 0;
}

单调队列—O(n)

我们从左到右扫描整个序列,用一个队列来维护最近 kk 个元素;
如果用暴力来做,就是每次都遍历一遍队列中的所有元素,找出最小值即可,但这样时间复杂度就变成 O(nk)O(nk) 了;
然后我们可以发现一个性质:
如果队列中存在两个元素,满足 a[i] >= a[j] 且 i < j,那么无论在什么时候我们都不会取 a[i] 作为最小值了,所以可以直接将 a[i] 删掉;
此时队列中剩下的元素严格单调递增,所以队头就是整个队列中的最小值,可以用 O(1)O(1) 的时间找到;
为了维护队列的这个性质,我们在往队尾插入元素之前,先将队尾大于等于当前数的元素全部弹出即可;
这样所有数均只进队一次,出队一次,所以时间复杂度是 O(n)O(n) 的。

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int q[N],a[N];//队列数组q[]存的是在a数组中的下标
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n,k;
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>a[i];
    int tt=-1,hh=0;
    for(int i=0;i<n;i++){
        if(hh<=tt&&i-q[hh]+1>k) hh++;//长度不能超过k,注意是q[hh]
        while(hh<=tt&&a[q[tt]]>=a[i]) tt--;//如果队列中元素值大于当前元素值则出队列
        q[++tt]=i;
        if(i>=k-1) cout<<a[q[hh]]<<' ';
    }

    cout<<'\n';

    tt=-1,hh=0;
    for(int i=0;i<n;i++){
        if(hh<=tt&&i-k+1>q[hh]) hh++;
        while(hh<=tt&&a[q[tt]]<=a[i]) tt--;
        q[++tt]=i;
        if(i>=k-1) cout<<a[q[hh]]<<' ';
    }

    return 0;
}

并查集

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,m;
int p[N],cnt[N];//盛放x的父节点
int find(int 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;
            cnt[i]=1;
    }
    while(m--){
        int a,b;
        char op[5];
        scanf("%s",op);
        if(op[0]=='C'){
            scanf("%d%d",&a,&b);
            if(find(a)==find(b)) continue;
            cnt[find(b)]+=cnt[find(a)];//注意顺序
            p[find(a)]=find(b);
           // size[find(b)]+=size[find(a)];

        }
        else if(op[1]=='1'){
            scanf("%d%d",&a,&b);
            if(find(a)==find(b)) puts("Yes");
            else puts("No");
        }
        else{
            scanf("%d",&a);
            printf("%d\n",cnt[find(a)]);
        }
    }
    return 0;
}

KMP

#include <iostream>

using namespace std;

const int N = 100010, M = 1000010;

int n, m;
int ne[N];//存储前缀后缀最大相等长度,<len
char s[M], p[N];//每次s[i]和p[j+1]比较

int main()
{
    cin >> n >> p + 1 >> m >> s + 1;

    //求ne[]数组,ne[i]=j 其含义是指p[1,j]=p[i-j+1,i]
    for (int i = 2, j = 0; i <= n; i ++ )//j=0不存储数据,i=1时无后缀,所以ne[i]一定时0
    //这里的前后缀时指不包含第一个(最后一个)字符的非平凡前(后)缀
    {
        while (j && p[i] != p[j + 1]) j = ne[j];//如果不匹配则移动到i之前仍匹配的点
        if (p[i] == p[j + 1]) j ++ ;
        ne[i] = j;
    }

    //匹配操作
    for (int i = 1, j = 0; i <= m; i ++ )
    {
        while (j && s[i] != p[j + 1]) j = ne[j];
        if (s[i] == p[j + 1]) j ++ ;
        if (j == n)
        {
            printf("%d ", i - n);
            j = ne[j];
        }
    }

    return 0;
}

哈希表

840. 模拟散列表

#include<bits/stdc++.h>
using namespace std;
const int N=100003;
int h[N],e[N],ne[N],idx;

void insert(int x){
    int k=(x%N+N)%N;
    e[idx]=x;
    ne[idx]=h[k];
    h[k]=idx++;
}
bool find(int x){
    int k=(x%N+N)%N;
    for(int i=h[k];i!=-1;i=ne[i]){
        if(e[i]==x) return true;
    }
    return false;
}
int main(){
    int n;
    cin>>n;
    memset(h,-1,sizeof h);
    while(n--){
        char op[2];
        int x;
        scanf("%s%d",op,&x);
        if(op[0]=='I') insert(x);
        else{
            if(find(x)) printf("Yes\n");
            else puts("No");
        }

    }
    return 0;
}

841. 字符串哈希

#include <iostream>
#include <algorithm>

using namespace std;

typedef unsigned long long ULL;

const int N = 100010, P = 131;

int n, m;
char str[N];
ULL h[N], p[N];

ULL get(int l, int r)
{
    return h[r] - h[l - 1] * p[r - l + 1];
}

int main()
{
    scanf("%d%d", &n, &m);
    scanf("%s", str + 1);

    p[0] = 1;
    for (int i = 1; i <= n; i ++ )
    {
        h[i] = h[i - 1] * P + str[i];//字符串哈希值为P进制的ASCII码对Q区模,Q=2^64恰好为ULL的数据范围
        p[i] = p[i - 1] * P;
    }

    while (m -- )
    {
        int l1, r1, l2, r2;
        scanf("%d%d%d%d", &l1, &r1, &l2, &r2);

        if (get(l1, r1) == get(l2, r2)) puts("Yes");
        else puts("No");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值