当前位置: 首页 > news >正文

编程题学习

acwing

826. 单链表

#include <iostream>

using namespace std;

const int N = 100010;

int idx, e[N], ne[N], head;


void init()
{
    head = -1;
    idx = 0;
}

void insert_head(int x)
{
    e[idx] = x;
    ne[idx] = head;
    head = idx ++ ;
}

void delete_k_pos(int x, int k)
{
    e[idx] = x;
    ne[idx] = ne[k];
    ne[k] = idx ++ ;
}

void delete_k(int k)
{
    ne[k] = ne[ne[k]];
}

int main()
{
    int n;
    cin >> n;
    
    init();
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    char op;
    int x, k;
    while (n -- )
    {
        cin >> op;
        
        if (op == 'H')
        {
            cin >> x;
            
            insert_head(x);
        }
        else if (op == 'D')
        {
            cin >> k;
            if (!k) head = ne[head];
            else
            {
                delete_k(k - 1);
            }
        }
        else
        {
            cin >> k >> x;
            
            delete_k_pos(x, k - 1);
            
        }
    }
    
    for (int i = head; i != -1; i = ne[i]) cout << e[i] << ' ';
    cout << endl;
    return 0;
}

827. 双链表

#include <iostream>
#include <string>

using namespace std;

const int N = 100010;

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

void init()
{
    r[0] = 1;
    l[1] = 0;
    idx = 2;
}

// 在节点a的右边插入一个数x
void insert_a_right(int a, int x)
{
    e[idx] = x;
    r[idx] = r[a];
    l[idx] = a;
    l[r[a]] = idx;
    r[a] = idx ++ ;
}

// 删除节点a
void delet_k(int a)
{
    l[r[a]] = l[a];
    r[l[a]] = r[a];
}

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

    return 0;
}

1. 之所以在 “D”, “IL”, “IR” 要用 k+1 的原因是 双链表的起始点是2. 所以,每个插入位置k的真实位置应该为 k-1+2 = k+1 (在单链表中为 k-1)。
2. 0, 1 节点的作用是边界。0为左边界,1为右边界。他俩在这里有点类似保留字的作用。正因如此,我们的idx也是从2开始
3. 最后遍历输出结果的 for (int i = rn[0]; i != 1; i = rn[i])。从 rn[0] 开始是因为 0 为左边界,而终止条件 i==1是因为1为右边界(如果碰到,说明已经遍历完毕)

828. 模拟栈

#include <iostream>

using namespace std;

const int N = 100010;

int top = -1;

int stk[N];

int main()
{
    int n;
    cin >> n;
    
    string op;
    
    while (n -- )
    {
        int x;
        cin >> op;
        if (op == "push")
        {
            cin >> x;
            stk[++ top] = x;
        }
        else if (op == "query")
        {
            cout << stk[top] << endl;
        }
        else if (op == "pop")
        {
            top -- ;
        }
        else if (op == "empty")
        {
            if (top == -1)
            {
                cout << "YES"<< endl;
            }
            else 
            {
                cout << "NO" << endl;
            }
        }
    }
}


文章转载自:
http://aca.gbfuy28.cn
http://bctv.gbfuy28.cn
http://calcutta.gbfuy28.cn
http://blossomy.gbfuy28.cn
http://boanerges.gbfuy28.cn
http://bimestrial.gbfuy28.cn
http://chink.gbfuy28.cn
http://avouchment.gbfuy28.cn
http://argumentation.gbfuy28.cn
http://backwardly.gbfuy28.cn
http://christen.gbfuy28.cn
http://actualization.gbfuy28.cn
http://bennet.gbfuy28.cn
http://bodega.gbfuy28.cn
http://canonicals.gbfuy28.cn
http://calyptrogen.gbfuy28.cn
http://checkers.gbfuy28.cn
http://bartend.gbfuy28.cn
http://bitter.gbfuy28.cn
http://cabined.gbfuy28.cn
http://archly.gbfuy28.cn
http://calycoideous.gbfuy28.cn
http://burnouse.gbfuy28.cn
http://almirah.gbfuy28.cn
http://adopt.gbfuy28.cn
http://bion.gbfuy28.cn
http://cheth.gbfuy28.cn
http://benzonitrile.gbfuy28.cn
http://antepartum.gbfuy28.cn
http://biloculate.gbfuy28.cn
http://www.dtcms.com/a/114148.html

相关文章:

  • SSE Emitter在Spring Boot和Vue中的简单使用
  • 青少年编程与数学 02-016 Python数据结构与算法 01课题、算法
  • 计算机网络 3-1 数据链路层(功能+组帧+差错控制)
  • 请问你怎么看代软件测试的潜力和挑战?
  • Kafka 漏消费和重复消费问题
  • SDL多线程编程
  • Zemax设计实例:AR近眼显示光学系统(60°视场,8K分辨率,超薄波导)
  • 程序化广告行业(62/89):DSP系统的媒体与PDB投放设置探秘
  • 34% 关税冲击下 LabVIEW 开发的变局
  • 哈尔滨工业大学DeepSeek公开课:探索大模型原理、技术与应用从GPT到DeepSeek|附视频与讲义免费下载方法
  • 多线程编程中的锁策略
  • java面试
  • 雅思练习总结(十八)
  • Java的SeleniumChromeDriver的常用方法
  • 从 Dense LLM 到 MoE LLM:以 DeepSeek MoE 为例讲解 MoE 的基本原理
  • 【Linux】文件描述符1
  • 定制一款国密浏览器(2):修改包名
  • Java学习总结-Commons-io框架-配置io框架
  • AI比人脑更强,因为被植入思维模型【42】思维投影思维模型
  • linux服务器安装pyenv
  • 题解:AT_abc241_f [ABC241F] Skate
  • 代码随想录回溯算法01(递归)
  • 为什么AI需要连接真实世界?
  • 使用人车关系核验API快速核验车辆一致性
  • IPSG 功能协议
  • 【realtek sdk-3.4.14b】RTL8197FH-VG+RTL8812F WiFi 2.4G 功率异常问题分析及解决方案
  • MyBatis逆向工程|mybatis-generator:generate插件的使用教程
  • Linux动态监控进程利器:top命令详解
  • 【微服务】基础概念
  • Java常用数据结构操作方法全面总结