数据结构模板汇总1

好久没有看算法题了,先把以前的东西整理一下:

单链表:https://www.acwing.com/problem/content/828/

AC代码:

#include<bits/stdc++.h>
using namespace std;
int head,e[100010],ne[100010],idx;
void init()
{
    head=-1;
    idx=0;
}
void add_to_head(int x)
{
    e[idx]=x;
    ne[idx]=head;
    head=idx;
    idx++;
}
void add(int k,int x)
{
    e[idx]=x;
    ne[idx]=ne[k];
    ne[k]=idx;
    idx++;
}
void remove(int k)
{
    ne[k]=ne[ne[k]];
}
int main()
{
    int m;
    cin>>m;
    init();
    while(m--)
    {
        int k,x;
        char op;
      cin>>op;
        if(op=='H')
        {
            cin>>x;
            add_to_head(x);
        }
        else if(op=='D')
        {
            cin>>k;
            if(!k)
            {
                head=ne[head];
                continue;
            }
            remove(k-1);
        }
        else
        {
            cin>>k>>x;
            add(k-1,x);
        }
    }
    for(int i=head;i!=-1;i=ne[i]) cout<<e[i]<<" ";
}

双链表:https://www.acwing.com/problem/content/829/

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int m;
int e[N], l[N], r[N], idx;
// 在节点a的右边插入一个数x
void insert(int a, int x)
{
    e[idx] = x;
    l[idx] = a, r[idx] = r[a];
    l[r[a]] = idx, r[a] = idx ++ ;
}
// 删除节点a
void remove(int a)
{
    l[r[a]] = l[a];
    r[l[a]] = r[a];
}

int main()
{
    cin >> m;
    // 0是左端点,1是右端点
    //0,1俩互相向对方连边
    r[0] = 1, l[1] = 0;
    idx = 2;

    while (m -- )
    {
        string op;
        cin >> op;
        int k, x;
        if (op == "L")
        {
            cin >> x;
            insert(0, x);
        }
        else if (op == "R")
        {
            cin >> x;
            insert(l[1], x);
        }
        else if (op == "D")
        {
            cin >> k;
            remove(k + 1);
        }
        else if (op == "IL")
        {
            cin >> k >> x;
            insert(l[k + 1], x);
        }
        else
        {
            cin >> k >> x;
            insert(k + 1, x);
        }
    }
    for (int i = r[0]; i != 1; i = r[i]) cout << e[i] << ' ';
    cout << endl;
}

单调栈:https://www.acwing.com/problem/content/832/

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,tt;
int st[100010];
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int x;
        cin>>x;
        while(tt&&st[tt]>=x) tt--;
        if(tt) cout<<st[tt]<<' ';
        else cout<<-1<<' ';
        st[++tt]=x;
    }
}

单调队列(滑动窗口):https://www.acwing.com/problem/content/156/

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1000100;
int a[N],q[N];
int n,k;
int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++) scanf("%d",&a[i]);
    int hh=0,tt=-1;
    for(int i=0;i<n;i++)
    {   
        if(q[hh]<i-k+1) hh++;
        while(hh<=tt&&a[q[tt]]>=a[i]) tt--;
        q[++tt]=i;
        if(i>=k-1) printf("%d ",a[q[hh]]);
    }
    cout<<endl;
    hh=0,tt=-1;
    for(int i=0;i<n;i++){
        if(q[hh]<i-k+1) hh++;
        while(hh<=tt&&a[q[tt]]<=a[i]) tt--;
        q[++tt]=i;
        if(i>=k-1) printf("%d ",a[q[hh]]);
    }
}

字符串之KMP:https://www.acwing.com/problem/content/833/

第一次看头都大了(好久没有动脑生锈了),看了某大佬的题解现在有点懂了:

https://www.acwing.com/solution/content/27068/

下面是AC代码:
 

#include<bits/stdc++.h>
using namespace std;
const int N = 1000010;
string p, s;
int ne[N];//ne是next数组
int n,m;
int main()
{
    cin >> n>> p>> m >> s;//s: 长字符串,p短字符串
    ne[0] = 0;//初始化ne[0]
    ne[1] = 0;//初始化ne[1]
    for (int i = 2; i < p.size() + 1; i++)//要处理到n+1,n+1的位置就是整个字符串的next
    {
        int j = ne[i - 1];
        while (j && p[i - 1] != p[j])// p[i] 与 P[j] 不同且 j 不为 0,就一直更新 j 为next[j]。
            j = ne[j];//询问上帝
        if (j != 0)//如果 j 不为0,就是 p[i - 1] == p[j] 成立,导致 while 循环结束
            ne[i] = j + 1;
        else//j = 0 导致 while 循环结束
        {
            if (p[j] == p[i - 1]) 
                ne[i] = 1;
            else
                ne[i] = 0;
        }
    }
    for (int i = 0, j = 0; i < s.size(); i++)
    {
        while (j && s[i] != p[j])
            j = ne[j];
        if (s[i] == p[j]) j++;
        if (j == p.size())
        {
            cout << i - j + 1 << " ";
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值