【算法笔记自学】第 7 章 提高篇(1)——数据结构专题(1)

7.1栈的应用

#include <iostream>
#include <string>
#include <stack>
using namespace std;int main() {int n, x;string action;cin >> n;stack<int> s;for (int i = 0; i < n; i++) {cin >> action;if (action == "push") {cin >> x;s.push(x);} else {if (s.empty()) {cout << -1 << endl;} else {cout << s.top() << endl;s.pop();}}}return 0;
}

#include <iostream>
#include <string>
using namespace std;string toPostfixExpr(string infixExpr) {string result = "";result += infixExpr[0];for (int i = 2; i < infixExpr.length(); i += 4) {result += " ";result += infixExpr[i + 2];result += " ";result += infixExpr[i];}return result;
}int main() {string expr;getline(cin, expr);cout << toPostfixExpr(expr);return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <stack>
using namespace std;struct Node {int num;char op;bool isOp;
};map<char, int> priority;vector<Node> toNodeVector(string expr) {vector<Node> nodes;for (int i = 0; i < expr.length(); i++) {if (expr[i] == ' ') {continue;}if (expr[i] >= '0' && expr[i] <= '9') {Node numNode;numNode.isOp = false;numNode.num = expr[i] - '0';nodes.push_back(numNode);} else {Node opNode;opNode.isOp = true;opNode.op = expr[i];nodes.push_back(opNode);}}return nodes;
}vector<Node> toPostfixVector(vector<Node> infix) {vector<Node> postfix;stack<Node> opStack;for (int i = 0; i < infix.size(); i++) {if (infix[i].isOp) {while (!opStack.empty() && priority[infix[i].op] <= priority[opStack.top().op]) {postfix.push_back(opStack.top());opStack.pop();}opStack.push(infix[i]);} else {postfix.push_back(infix[i]);}}while (!opStack.empty()) {postfix.push_back(opStack.top());opStack.pop();}return postfix;
}int main() {priority['+'] = priority['-'] = 0;priority['*'] = priority['/'] = 1;string expr;getline(cin, expr);vector<Node> infix = toNodeVector(expr);vector<Node> postfix = toPostfixVector(infix);for (int i = 0; i < postfix.size(); i++) {if (postfix[i].isOp) {cout << postfix[i].op;} else {cout << postfix[i].num;}if (i < (int)postfix.size() - 1) {cout << " ";}}return 0;
}

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <stack>
using namespace std;struct Node {int num;char op;bool isOp;
};map<char, int> priority;vector<Node> toNodeVector(string expr) {vector<Node> nodes;for (int i = 0; i < expr.length(); i++) {if (expr[i] == ' ') {continue;}if (expr[i] >= '0' && expr[i] <= '9') {Node numNode;numNode.isOp = false;numNode.num = expr[i] - '0';nodes.push_back(numNode);} else {Node opNode;opNode.isOp = true;opNode.op = expr[i];nodes.push_back(opNode);}}return nodes;
}double eval(vector<Node> postfix) {stack<double> numStack;for (int i = 0; i < postfix.size(); i++) {if (!postfix[i].isOp) {numStack.push(postfix[i].num);} else {double num2 = numStack.top();numStack.pop();double num1 = numStack.top();numStack.pop();if (postfix[i].op == '+') {numStack.push(num1 + num2);} else if (postfix[i].op == '-') {numStack.push(num1 - num2);} if (postfix[i].op == '*') {numStack.push(num1 * num2);} if (postfix[i].op == '/') {numStack.push(num1 / num2);}}}return numStack.top();
}int main() {priority['+'] = priority['-'] = 0;priority['*'] = priority['/'] = 1;string expr;getline(cin, expr);vector<Node> postfix = toNodeVector(expr);printf("%.2f", eval(postfix));return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <stack>
using namespace std;struct Node {int num;char op;bool isOp;
};map<char, int> priority;vector<Node> toNodeVector(string expr) {vector<Node> nodes;for (int i = 0; i < expr.length(); i++) {if (expr[i] == ' ') {continue;}if (expr[i] >= '0' && expr[i] <= '9') {Node numNode;numNode.isOp = false;numNode.num = expr[i] - '0';nodes.push_back(numNode);} else {Node opNode;opNode.isOp = true;opNode.op = expr[i];nodes.push_back(opNode);}}return nodes;
}vector<Node> toPostfixVector(vector<Node> infix) {vector<Node> postfix;stack<Node> opStack;for (int i = 0; i < infix.size(); i++) {if (infix[i].isOp) {while (!opStack.empty() && priority[infix[i].op] <= priority[opStack.top().op]) {postfix.push_back(opStack.top());opStack.pop();}opStack.push(infix[i]);} else {postfix.push_back(infix[i]);}}while (!opStack.empty()) {postfix.push_back(opStack.top());opStack.pop();}return postfix;
}double eval(vector<Node> postfix) {stack<double> numStack;for (int i = 0; i < postfix.size(); i++) {if (!postfix[i].isOp) {numStack.push(postfix[i].num);} else {double num2 = numStack.top();numStack.pop();double num1 = numStack.top();numStack.pop();if (postfix[i].op == '+') {numStack.push(num1 + num2);} else if (postfix[i].op == '-') {numStack.push(num1 - num2);} if (postfix[i].op == '*') {numStack.push(num1 * num2);} if (postfix[i].op == '/') {numStack.push(num1 / num2);}}}return numStack.top();
}int main() {priority['+'] = priority['-'] = 0;priority['*'] = priority['/'] = 1;string expr;getline(cin, expr);vector<Node> infix = toNodeVector(expr);vector<Node> postfix = toPostfixVector(infix);printf("%.2f", eval(postfix));return 0;
}

7.2队列的应用

#include <iostream>
#include <string>
#include <queue>
using namespace std;int main() {int n, k;cin >> n;string action;queue<int> q;for (int i = 0; i < n; i++) {cin >> action;if (action == "push") {cin >> k;q.push(k);} else {if (q.empty()) {cout << -1 << endl;} else {cout << q.front() << endl;q.pop();}}}return 0;
}

#include <cstdio>
#include <queue>
using namespace std;int main() {int n, x;scanf("%d", &n);queue<int> q;for (int i = 0; i < n; i++) {scanf("%d", &x);q.push(x);}while (q.size() > 1) {int front1 = q.front();q.pop();int front2 = q.front();q.pop();q.push(front1 + front2);}printf("%d", q.front());return 0;
}

#include <cstdio>
#include <queue>
using namespace std;int main() {int n, k;scanf("%d%d", &n, &k);queue<int> q;for (int i = 1; i <= n; i++) {q.push(i);}while (!q.empty()) {for (int i = 0; i < k - 1; i++) {int front = q.front();q.pop();q.push(front);}printf("%d", q.front());q.pop();if (!q.empty()) {printf(" ");}}return 0;
}

7.3链表处理

#include <cstdio>const int MAXN = 100;struct Node {int data, next;
} nodes[MAXN];int main() {int n, first, id;scanf("%d%d", &n, &first);for (int i = 0; i < n; i++) {scanf("%d", &id);scanf("%d%d", &nodes[id].data, &nodes[id].next);}int current = first;while (current != -1) {printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);current = nodes[current].next;}return 0;
}

#include <cstdio>const int MAXN = 100;struct Node {int data, next;
} nodes[MAXN];int main() {int n, first, id;scanf("%d%d", &n, &first);for (int i = 0; i < n; i++) {scanf("%d", &id);scanf("%d%d", &nodes[id].data, &nodes[id].next);}int current = first;int num=0;while (current != -1) {//printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);current = nodes[current].next;num++;}printf("%d",num);return 0;
}

#include <cstdio>const int MAXN = 1024;struct Node {int data, next;
} nodes[MAXN];int main() {int n, first, id;scanf("%d%d", &n, &first);for (int i = 0; i < n; i++) {scanf("%d", &id);scanf("%d%d", &nodes[id].data, &nodes[id].next);}int num=0;int current = first;scanf("%d",&num);for (int i = 0; i < num; i++) {scanf("%d", &id);scanf("%d", &nodes[id].data);nodes[id].next=current;current=id;}while (current != -1) {printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);current = nodes[current].next;}return 0;
}

#include <cstdio>const int MAXN = 100;struct Node {int data, next;
} nodes[MAXN];int main() {int n, first, k, id;scanf("%d%d%d", &n, &first, &k);for (int i = 0; i < n; i++) {scanf("%d", &id);scanf("%d%d", &nodes[id].data, &nodes[id].next);}int current = first, last = 0;while (current != -1) {if (nodes[current].data == k) {if (current == first) {first = nodes[current].next;}nodes[last].next = nodes[current].next;current = nodes[last].next;} else {last = current;current = nodes[current].next;}}current = first;while (current != -1) {printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);current = nodes[current].next;}return 0;
}

#include <cstdio>const int MAXN = 100;struct Node {int data, next;
} nodes[MAXN];int main() {int n, first, id;scanf("%d%d", &n, &first);for (int i = 0; i < n; i++) {scanf("%d", &id);scanf("%d%d", &nodes[id].data, &nodes[id].next);}int current = first, last = -1;while (current != -1) {int next = nodes[current].next;nodes[current].next = last;last = current;current = next;}current = last;while (current != -1) {printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);current = nodes[current].next;}return 0;
}

#include <cstdio>const int MAXN = 100;
const int MAXV = 1000 + 1;struct Node {int data, next;
} nodes[MAXN];bool needDelete[MAXV] = {false};int main() {int n, first, id;scanf("%d%d", &n, &first);for (int i = 0; i < n; i++) {scanf("%d", &id);scanf("%d%d", &nodes[id].data, &nodes[id].next);}int current = first, last = -1;while (current != -1) {if (needDelete[nodes[current].data]) {nodes[last].next = nodes[current].next;current = nodes[current].next;} else {needDelete[nodes[current].data] = true;last = current;current = nodes[current].next;}}current = first;while (current != -1) {printf("%d %d %d\n", current, nodes[current].data, nodes[current].next);current = nodes[current].next;}return 0;
}

#include <cstdio>const int MAXN = 100;struct Node {int data, next;
} nodes[MAXN];int main() {int n, first, id;scanf("%d%d", &n, &first);for (int i = 0; i < n; i++) {scanf("%d", &id);scanf("%d%d", &nodes[id].data, &nodes[id].next);}int fast = first, slow = first;while (nodes[fast].next != -1 && nodes[nodes[fast].next].next != -1) {slow = nodes[slow].next;fast = nodes[fast].next;fast = nodes[fast].next;}if (nodes[fast].next == -1) {printf("%.1f", (double)nodes[slow].data);} else {printf("%.1f", (nodes[slow].data + nodes[nodes[slow].next].data) / 2.0);}return 0;
}

#include <cstdio>const int MAXN = 100;struct Node {int data, next;
} nodes[MAXN];int main() {int n, first, k, id;scanf("%d%d%d", &n, &first, &k);for (int i = 0; i < n; i++) {scanf("%d", &id);scanf("%d%d", &nodes[id].data, &nodes[id].next);}int fast = first;while (k--) {fast = nodes[fast].next;}int slow = first;while (fast != -1) {slow = nodes[slow].next;fast = nodes[fast].next;}printf("%d %d %d", slow, nodes[slow].data, nodes[slow].next);return 0;
}

#include <cstdio>const int MAXN = 100;struct Node {int data, next;
} nodes[MAXN];int reverseList(int first) {int current = first, last = -1;while (current != -1) {int next = nodes[current].next;nodes[current].next = last;last = current;current = next;}return last;
}bool judgePalindrome(int head1, int head2) {while (head2 != -1) {if (nodes[head1].data != nodes[head2].data) {return false;}head1 = nodes[head1].next;head2 = nodes[head2].next;}return true;
}int main() {int n, first, id;scanf("%d%d", &n, &first);for (int i = 0; i < n; i++) {scanf("%d", &id);scanf("%d%d", &nodes[id].data, &nodes[id].next);}int fast = first, slow = first;while (nodes[fast].next != -1 && nodes[nodes[fast].next].next != -1) {slow = nodes[slow].next;fast = nodes[fast].next;fast = nodes[fast].next;}int headOfSecondPart = reverseList(nodes[slow].next);bool isPalindrome = judgePalindrome(first, headOfSecondPart);nodes[slow].next = reverseList(headOfSecondPart);printf(isPalindrome ? "Yes" : "No");return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1475247.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

昇思25天学习打卡营第19天|Pix2Pix实现图像转换

1. 学习内容复盘 Pix2Pix概述 Pix2Pix是基于条件生成对抗网络&#xff08;cGAN, Condition Generative Adversarial Networks &#xff09;实现的一种深度学习图像转换模型&#xff0c;该模型是由Phillip Isola等作者在2017年CVPR上提出的&#xff0c;可以实现语义/标签到真实…

生活商城app微信小程序模板源码

红色的卷皮折扣电商app小程序&#xff0c;综合生活购物商城app小程序前端模板下载。包含&#xff1a;首页、分类、购物车、列表、商品详情、个人中心、优惠券、全部订单、生活超市专题等等。一套很全通用的商城app小程序模板。 生活商城app微信小程序模板源码

YOLOv8标签可视化

这一章主要是为了可视化YOLO标签设置的,为什么要进行可视化呢,因为很多时候我们标注好数据还需要进行转换成YOLO格式,这期间如果出现转换的错误,而我们没有去检查标签的话,有可能导致训练无法得到很好的一个结果,所以需要我们对YOLO标签进行可视化来检查标签的情况。 这部…

《梦醒蝶飞:释放Excel函数与公式的力量》9.5 IRR函数

9.5 IRR函数 IRR函数是Excel中用于计算内部收益率&#xff08;Internal Rate of Return, IRR&#xff09;的函数。内部收益率是评估投资项目盈利性的重要指标&#xff0c;它表示使投资项目的净现值&#xff08;NPV&#xff09;为零的折现率。 9.5.1 函数简介 IRR函数通过一系…

【免费数字孪生平台】零代码制作智慧农业蔬菜大棚可视化

一&#xff0e;智慧农业的价值 智慧农业&#xff0c;作为农业中的智慧经济形态&#xff0c;是现代科学技术与农业种植深度融合的产物。它通过将物联网、云计算、大数据、人工智能等现代信息技术集成应用于农业生产中&#xff0c;实现了农业生产的无人化、自动化和智能化管理。…

第十一节 动态面板加密解密显示

在原型中我们经常会遇到文件加密与解密显示问题&#xff0c;下面以一个简单案例来说明实现怎么切换明文与密文不同显示方式案例说明&#xff1b; 1、添加动态面板 2、设置加密与不加密 3、添加动作事项 注意为可见时要设置面板状态向前循环&#xff0c;上一项&#xff0c;否则…

“一稿多投”是学术不端,还是作者的合法权利?

【SciencePub学术】“一稿多投”一直被认为是不端的行为&#xff0c;但这个“规矩”是在纸质时代信息沟通不畅的情况下制定的&#xff0c;近年来有关取消这一观念的声音已振聋发聩&#xff01; 詹启智的《一稿多投是著作权人依法享有的合法权利一一兼论一稿多发后果的规制》一文…

3115.力扣每日一题7/2 Java

博客主页&#xff1a;音符犹如代码系列专栏&#xff1a;算法练习关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ 目录 思路 解题方法 时间复杂度 空间复杂度 Code 总结 思路 这道题的…

PDM系统中物料分类与编码规则生成方案

在企业管理软件中&#xff0c;PDM系统是企业管理的前端软件&#xff0c;用于管理研发图纸、BOM等数据&#xff0c;然后生成相关物料表或BOM&#xff0c;递交给后端ERP系统进行生产管理。在PDM系统中&#xff0c;有两种方式可以生成物料编码。 1第一种是用户可以通过软件接口将…

浅谈MyBatis的工作原理以及核心流程

引言 在行内日常开发工作中&#xff0c;基于Java的研发项目会大量的与数据库做交互访问。传统方式是通过JDBC访问数据库&#xff0c;不仅需要繁琐的手工编写很多代码&#xff0c;增加了工程项目的复杂度&#xff0c;而且存在难以维护&#xff0c;配置不够灵活等特点。为解决传…

架构师学习理解和总结

1.架构设计理念 2.架构方法论 2.1需求分析 2.1.1常见需求层次 2.1.2 常见需求结果 2.1.3 需求与架构关系 2.2 领域分析 2.3 关键需求 2.4 概念架构设计 2.5 细化架构设计 2.6 架构设计验证 3.架构设计工具 3.1 DDD领域建模 3.2 41视图分析法 3.3 UML设计工具 4.架构师知…

EDI安全:如何在2024年保护您的数据免受安全和隐私威胁

电子数据交换&#xff08;EDI&#xff09;支持使用标准化格式在组织之间自动交换业务文档。这种数字化转型彻底改变了业务通信&#xff0c;消除了对纸质交易的需求并加速了交易。然而&#xff0c;随着越来越依赖 EDI 来传输发票、采购订单和发货通知等敏感数据&#xff0c;EDI …

利用canvas压缩图片

前情提要 页面打印导出pdf文件的时候&#xff0c;图片大小会影响pdf文件大小。 为了减小pdf文件大小&#xff0c;需要将图片压缩一下。在只有图片地址的情况下&#xff0c;将图片压缩后显示&#xff0c;一开始用的browser-image-compression插件&#xff0c;这是js压缩&#x…

Elasticsearch:Node.js ECS 日志记录 - Winston

这是继上一篇文章 “Elasticsearch&#xff1a;Node.js ECS 日志记录 - Pino” 的续篇。我们继续上一篇文章来讲述使用 Winston 包来针对 Node.js 应用生成 ECS 向匹配的日子。此 Node.js 软件包为 winston 记录器提供了格式化程序&#xff0c;与 Elastic Common Schema (ECS) …

迈巴赫S480升级头等舱行政独立四座马鞍小桌板追求极致舒适

迈巴赫 S480 一直以来都代表着尊贵与奢华的巅峰。然而&#xff0c;对于追求极致舒适与专属体验的车主而言&#xff0c;常规配置或许仍不足以满足其苛刻的需求。正因如此&#xff0c;迈巴赫 S480 的头等舱行政独立 4 座改装应运而生&#xff0c;为这款豪华座驾注入了全新的灵魂。…

惟客数据Q2荣誉成绩单新鲜出炉

作为众多头部企业客户的数字化经营合作伙伴 WakeData惟客数据一直坚持以客户为中心&#xff0c;以数据驱动 致力于让数据智能商业落地更敏捷 凭借值得信赖的客户经营数字化和资源管理数字化实力 惟客数据在2024年第二季度斩获多项荣誉 1、 第一新声《2023年度中国高科技高…

【鸿蒙学习笔记】MVVM模式

官方文档&#xff1a;MVVM模式 [Q&A] 什么是MVVM ArkUI采取MVVM Model View ViewModel模式。 Model层&#xff1a;存储数据和相关逻辑的模型。View层&#xff1a;在ArkUI中通常是Component装饰组件渲染的UI。ViewModel层&#xff1a;在ArkUI中&#xff0c;ViewModel是…

STM32智能机器人导航系统教程

目录 引言环境准备智能机器人导航系统基础代码实现&#xff1a;实现智能机器人导航系统 4.1 数据采集模块 4.2 数据处理与导航算法 4.3 通信与网络系统实现 4.4 用户界面与数据可视化应用场景&#xff1a;机器人导航应用与优化问题解决方案与优化收尾与总结 1. 引言 智能机器…

Miniconda的常见用法——以Isaacgym为例

1. ubuntu24.04安装minicondda mkdir -p ~/miniconda3 wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh解释下这段代码 bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3~/miniconda3/miniconda.sh: 指向Mi…

【Java安装】windows10+JDK21+IDEA

文章目录 一、JDK安装1. 下载完成后按照自己需要的位置安装2. 配置环境变量2.1 JAVA_HOME变量2.2 PATH配置 3. 验证4. helloworld 二、IDEA安装三、IDEA-HelloWorld 一、JDK安装 JDK安装链接 1. 下载完成后按照自己需要的位置安装 2. 配置环境变量 2.1 JAVA_HOME变量 安装…