自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(28)
  • 收藏
  • 关注

原创 字符变换

t 中将 a[i] 与 b[i] 匹配的子字符串. 如果 r 已经在 db 映射中,则函数返回分别从 A 和 B 到达 t 和 r 所需的最小步数之和,加一 如果 r 已经在 da 映射中,则函数跳过转换。程序读入输入数据后,调用bfs函数进行广度优先搜索,bfs函数初始化两个队列qa和qb,分别用于存储A和B可以变换的字符串的索引,它还初始化两个数组 da 和 db,分别用于存储从 A 和 B 到达每个字符串所需的最小转换次数。该函数返回一个整数,表示将一个字符串转换为另一个字符串所需的最小步骤数。

2023-06-26 20:41:24 168

原创 单词接龙

DFS 函数称为 dfs,它有两个参数:一个表示当前正在搜索的单词的字符串,以及一个表示当前单词在单词数组中的索引的整数。搜索是通过迭代单词数组中的每个单词并检查是否可以使用其最长的公共后缀将其附加到正在搜索的当前单词来执行的。现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”,每个单词最多被使用两次。输入的第一行为一个单独的整数 n表示单词数,以下 n 行每行有一个单词(只含有大写或小写字母,长度不超过20),输入的最后一行为一个单个字符,表示“龙”开头的字母。

2023-06-26 19:26:35 82

原创 Dijkstra求最短路 I

给定一个 n 个点 m 条边的有向图,图中可能存在重边和自环,所有边权均为正值。请你求出 1 号点到 n号点的最短距离,如果无法从 1号点到 n 号点,则输出 −1。输入格式第一行包含整数 n和 m。接下来 m行每行包含三个整数 x,y,z,表示存在一条从点 x到点 y的有向边,边长为 z。输出格式输出一个整数,表示 1 号点到 n号点的最短距离。如果路径不存在,则输出 −1。数据范围1≤n≤5001≤m≤105图中涉及边长均不超过10000。输入样例:3 3。

2023-06-07 16:04:38 97

原创 抓住那头牛

农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点 N,牛位于点 K。农夫有两种移动方式:从 X移动到 X−1或 X+1,每次移动花费一分钟从 X移动到 2∗X,每次移动花费一分钟假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?输入格式共一行,包含两个整数N和K。输出格式输出一个整数,表示抓到牛所花费的最少时间。数据范围0≤N,K≤105输入样例:5 17输出样例:4。

2023-06-04 15:12:34 202

原创 武士风度的牛

农民 John 有很多牛,他想交易其中一头被 Don 称为 The Knight 的牛。这头牛有一个独一无二的超能力,在农场里像 Knight 一样地跳(就是我们熟悉的象棋中马的走法)。虽然这头神奇的牛不能跳到树上和石头上,但是它可以在牧场上随意跳,我们把牧场用一个 x,y的坐标图来表示。这头神奇的牛像其它牛一样喜欢吃草,给你一张地图,上面标注了 The Knight 的开始位置,树、灌木、石头以及其它障碍的位置,除此之外还有一捆草。现在你的任务是,确定 The Knight 要想吃到草,至少需要跳多少次。

2023-06-04 14:51:01 118

原创 迷宫问题

给定一个 n×n 的二维数组,如下所示:它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。数据保证至少存在一条从左上角走到右下角的路径。输入格式第一行包含整数 n。接下来 n 行,每行包含 n个整数 0 或 1,表示迷宫。输出格式输出从左上角到右下角的最短路线,如果答案不唯一,输出任意一条路径均可。

2023-06-04 11:38:19 198

原创 山峰和山谷

FGD小朋友特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。为了能够对旅程有一个安排,他想知道山峰和山谷的数量。给定一个地图,为FGD想要旅行的区域,地图被分为 n×n的网格,每个格子 (i,j)的高度 w(i,j)是给定的。若两个格子有公共顶点,那么它们就是相邻的格子,如与 (i,j)相邻的格子有(i−1,j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1)。

2023-06-03 23:56:48 107

原创 城堡问题

图1# = Wall方向:上北下南左西右东。图1是一个城堡的地形图。请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。城堡被分割成 m∗n个方格区域,每个方格区域可以有0~4面墙。注意:墙体厚度忽略不计。输入格式第一行包含两个整数 m 和 n,分别表示城堡南北方向的长度和东西方向的长度。接下来 m 行,每行包含 n 个整数,每个整数都表示平面图对应位置的方块的墙的特征。

2023-06-02 18:52:34 62

原创 池塘计数

农夫约翰有一片 N∗M 的矩形土地。最近,由于降雨的原因,部分土地被水淹没了。现在用一个字符矩阵来表示他的土地。每个单元格内,如果包含雨水,则用”W”表示,如果不含雨水,则用”.”表示。现在,约翰想知道他的土地中形成了多少片池塘。每组相连的积水单元格集合可以看作是一片池塘。每个单元格视为与其上、下、左、右、左上、右上、左下、右下八个邻近单元格相连。请你输出共有多少片池塘,即矩阵中共有多少片相连的”W”块。输入格式第一行包含两个整数 N和 M。

2023-06-02 18:17:36 71

原创 闇の連鎖

传说中的暗之连锁被人们称为 Dark。Dark 是人类内心的黑暗的产物,古今中外的勇者们都试图打倒它。经过研究,你发现 Dark 呈现无向图的结构,图中有 N个节点和两类边,一类边被称为主要边,而另一类被称为附加边。Dark 有 N–1条主要边,并且 Dark 的任意两个节点之间都存在一条只由主要边构成的路径。另外,Dark 还有 M条附加边。你的任务是把 Dark 斩为不连通的两部分。一开始 Dark 的附加边都处于无敌状态,你只能选择一条主要边切断。

2023-06-02 17:12:19 42

原创 距 离

给出 n个点的一棵树,多次询问两点之间的最短距离。注意:边是无向的。所有节点的编号是 1,2,…,n。输入格式第一行为两个整数 n和 m。n表示点数,m表示询问次数;接下来 n−1行,每行三个整数 x,y,k,表示点 x和点 y 之间存在一条边长度为 k;再接下来 m行,每行两个整数 x,y,表示询问点 x到点 y的最短距离。树中结点编号从 1到 n。输出格式共 m行,对于每次询问,输出一行询问结果。数据范围2≤n≤1041≤m≤2×1040<k≤100。

2023-06-02 17:04:00 36

原创 grandparents inquiry

Given a rooted undirected tree containing n nodes, the node numbers are different from each other, but not necessarily 1∼n. There are m queries, each query gives a pair of node numbers x and y, and asks about the grandparent relationship between x and y.in

2023-06-02 16:59:31 32

原创 the center of the tree

Given a tree, the tree contains n nodes (numbered 1~n) and n−1 undirected edges, and each edge has a weight. Please find a point in the tree such that the furthest distance from this point to other nodes in the tree is the shortest.input formatThe first l

2023-05-26 15:36:21 72

原创 longest path of the tree

Given a tree, the tree contains n nodes (numbered 1~n) and n−1 undirected edges, and each edge has a weight. Now please find the longest path in the tree. In other words, find a path such that the points at both ends of the path are farthest apart.Note: A

2023-05-25 22:59:19 59

原创 eight digits

In a 3×3 grid, the 8 numbers from 1 to 8 and an x happen to be distributed in this 3×3 grid without duplication or omission.For example:1 2 3x 4 67 5 8During the game, you can swap x with its number in one of the four directions up, down, left, or righ

2023-05-25 17:45:50 42

原创 walk the maze

Given a n×m two-dimensional integer array to represent a maze, the array contains only 0or 1, where 0 means walkable paths and 1 means impassable walls. Initially, there is a person at (1,1) in the upper left corner, and it is known that the person can mo

2023-05-25 15:26:13 46

原创 Hierarchy of points in the graph

Given a directed graph with n points and m edges, multiple edges and self-loops may exist in the graph. All sides are of length 1, and the vertices are numbered 1∼n. Please find the shortest distance from point 1 to point n. If you cannot reach point n fro

2023-05-25 15:22:48 45

原创 C++ STL

set在默认情况下的比较函数为(如果是非内置类型需要重载 < 运算符然而在某些特殊情况下,我们希望能自定义set内部的比较方式。这时候可以通过传入自定义比较器来解决问题。具体来说,我们需要定义一个类,并在这个类中重载 () 运算符。例如,我们想要维护一个存储整数,且较大值靠前的set,可以这样实现:12345对于其他关联式容器,可以用类似的方式实现自定义比较,这里不再赘述。123// 使用默认底层容器 deque,数据类型为 TypeName。STL

2023-05-25 10:38:07 384

原创 N-queens Problem

The n-queen problem refers to placing n queens on an n×n chessboard so that the queens cannot attack each other, that is, any two queens cannot be on the same row, column or slope.Now given an integer n, please output all chess pieces that meet the condit

2023-05-24 23:49:01 68

原创 Gravity of the Tree

Given a tree, the tree contains n nodes (numbered 1∼n) and n−1 undirected edges. Please find the center of gravity of the tree, and output the maximum number of points in the remaining connected blocks after the center of gravity is deleted.Definition of c

2023-05-24 22:59:51 43

原创 Arrange Numbers

Given an integer n, arrange the numbers 1∼n in a row, there will be many ways to arrange them. Now, please output all the permutation methods in lexicographical order.input formatA line containing an integer n.output formatOutput all permutation schemes

2023-05-24 15:59:55 67 1

原创 Sum of Submatrices

Input an integer matrix with n rows and m columns, and then input q queries, each query contains four integers x1, y1, x2, y2, representing the coordinates of the upper left corner and the lower right corner of a sub-matrix. For each query output the sum o

2023-05-24 00:55:09 36

原创 Prefix Sum

Enter a sequence of integers of length n. Next, enter m queries, and enter a pair of l, r for each query. For each query, output the sum from the lth number to the rth number in the original sequence.input formatThe first line contains two integers n and

2023-05-23 18:42:11 26 1

原创 Range of Numbers

Given an integer array of length n sorted in ascending order, and q queries.For each query, return the start and end positions of an element k (positions are counted from 0). Returns -1 -1 if the element does not exist in the array.input formatThe first

2023-05-23 18:15:49 30

原创 number of reversed pairs

Given an array of integers of length n, please count the number of reversed pairs in the array.The reverse pair is defined as follows: For the i-th and j-th elements of the array, if i<j And a[i]>a[j], then it is a reversed pair; otherwise it is not.inpu

2023-05-22 22:00:32 58 1

原创 Merge Sort

You are given an array of integers of length n.Please use merge sort to sort this array from smallest to largest. And output the sorted array in order.input formatThe input consists of two lines, the first line contains the integer nThe second line conta

2023-05-22 20:15:37 64

原创 kth number

Given an integer sequence of length n, and an integer k, please use the quick selection algorithm to find the kth number after sorting the sequence from small to large.input formatThe first line contains two integers n and kThe second row contains n inte

2023-05-22 18:02:35 83 1

原创 Quick Sort Solution

You are given an array of integers of length n. Please use quick sort to sort this array from smallest to largest. And output the sorted array in order.input formatThe input consists of two lines, the first line contains the integer nThe second line conta

2023-05-22 16:47:27 67 2

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除