2.6学习总结

news/2025/2/7 8:26:09/

洛谷1241代码:

应用栈后进先出的特性解决

(只有48,未ac)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1000
typedef struct {char data[MAX_SIZE];int top;
} Stack;
//初始化
void initStack(Stack* s) {s->top = -1;
}
//进栈
void push(Stack* s, char c) {if (s->top < MAX_SIZE - 1) {s->data[++(s->top)] = c;}
}
//出栈
char pop(Stack* s) {if (s->top >= 0) {return s->data[(s->top)--];}return '\0';
}
//遍历
char peek(Stack* s) {if (s->top >= 0) {return s->data[s->top];}return '\0';
}
//匹配
int isMatchingPair(char left, char right) {return (left == '(' && right == ')') || (left == '[' && right == ']');
}
//修复
void fixUnmatched(char* s) {Stack stack;initStack(&stack);int len = strlen(s);int* matched = (int*)calloc(len, sizeof(int));for (int i = 0; i < len; i++) {if (s[i] == '(' || s[i] == '[') {push(&stack, i);}else if (s[i] == ')' || s[i] == ']') {if (stack.top >= 0 && isMatchingPair(s[peek(&stack)], s[i])) {matched[pop(&stack)] = 1;matched[i] = 1;}}}for (int i = len - 1; i >= 0; i--) {if (!matched[i]) {if (s[i] == '(' || s[i] == ')') {printf("()");}else if (s[i] == '[' || s[i] == ']') {printf("[]");}}else {printf("%c", s[i]);}}free(matched);
}
int main() {char s[MAX_SIZE];scanf("%s", s);fixUnmatched(s);return 0;
}

洛谷p4913题解

采用bfs广搜

#include<stdio.h>
#include<stdlib.h>
#define max 1000001
typedef struct {int left;int right;
}node;
node tree[max];
int depth[max];
int main() {int n;scanf("%d", &n);for (int i = 1;i <= n;i++) {scanf("%d %d", &tree[i].left, &tree[i].right);}int maxdepth = 0;int queue[max];int front = 0, rear = 0;queue[rear++] = 1;depth[1] = 1;while (front < rear) {int current = queue[front++];if (depth[current] > maxdepth) {maxdepth = depth[current];}if (tree[current].left != 0) {queue[rear++] = tree[current].left;depth[tree[current].left] = depth[current] + 1;}if (tree[current].right != 0) {queue[rear++] = tree[current].right;depth[tree[current].right] = depth[current] + 1;}}printf("%d", maxdepth);return 0;
}

 

 


http://www.ppmy.cn/news/1570024.html

相关文章

3NF讲解

3NF讲解 3NF&#xff08;第三范式&#xff09;是数据库设计中的一种规范化方法&#xff0c;目的是消除数据冗余和避免数据异常。它帮助数据库保持高效&#xff0c;灵活和一致性。理解3NF的关键点在于它依赖于前两个范式&#xff08;1NF和2NF&#xff09;的基础。 1. 第一范式…

深度学习之“线性代数”

线性代数在深度学习中是解决多维数学对象计算问题的核心工具。这些数学对象包括标量、向量、矩阵和张量&#xff0c;借助它们可以高效地对数据进行操作和建模。以下将详细介绍这些数学对象及其在深度学习中的典型用途。 数学对象概述 标量 标量是最简单的数学对象&#xff0…

C++底层学习预备:模板初阶

文章目录 1.编程范式2.函数模板2.1 函数模板概念2.2 函数模板原理2.3 函数模板实例化2.3.1 隐式实例化2.3.2 显式实例化 2.4 模板参数的匹配原则 3.类模板希望读者们多多三连支持小编会继续更新你们的鼓励就是我前进的动力&#xff01; 进入STL库学习之前我们要先了解有关模板的…

八大排序算法细讲

目录 排序 概念 运用 常见排序算法 插入排序 直接插入排序 思想&#xff1a; 步骤&#xff08;排升序&#xff09;: 代码部分&#xff1a; 时间复杂度&#xff1a; 希尔排序 思路 步骤 gap的取法 代码部分&#xff1a; 时间复杂度&#xff1a; 选择排序 直接选…

【SQL技术】不同数据库引擎 SQL 优化方案剖析

一、引言 在数据处理和分析的世界里&#xff0c;SQL 是不可或缺的工具。不同的数据库系统&#xff0c;如 MySQL、PostgreSQL&#xff08;PG&#xff09;、Doris 和 Hive&#xff0c;在架构和性能特点上存在差异&#xff0c;因此针对它们的 SQL 优化策略也各有不同。这些数据库中…

Linux 源码编译安装httpd 2.4,提供系统服务管理脚本并测试

第一种方式 1. 下载 Apache HTTP Server 源代码 首先&#xff0c;从 Apache 官网 下载最新版本的 httpd 2.4 源码&#xff0c;或者直接使用 wget 下载&#xff1a; [rootlocalhost ~]# wget https://downloads.apache.org/httpd/httpd-2.4.36.tar.gz # 解压 [rootlocalhost ~…

javaEE-8.JVM(八股文系列)

目录 一.简介 二.JVM中的内存划分 JVM的内存划分图: 堆区:​编辑 栈区:​编辑 程序计数器&#xff1a;​编辑 元数据区&#xff1a;​编辑 经典笔试题&#xff1a; 三,JVM的类加载机制 1.加载: 2.验证: 3.准备: 4.解析: 5.初始化: 双亲委派模型 概念: JVM的类加…

算法设计与分析三级项目--管道铺设系统

摘 要 该项目使用c算法逻辑&#xff0c;开发环境为VS2022&#xff0c;旨在通过Prim算法优化建筑物间的连接路径&#xff0c;以支持管线铺设规划。可以读取文本文件中的建筑物名称和距离的信息&#xff0c;并计算出建筑物之间的最短连接路径和总路径长度&#xff0c;同时以利用…