![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
数据结构与算法
EasonDongH
这个作者很懒,什么都没留下…
展开
-
字符串匹配算法:蛮力算法、KMP算法、BM算法
概念定义子串:字符串中任一连续的片段,称作其子串(substring)前缀:prefix(S, K) = S.substr(0, K) = S[0, K)后缀:suffix(S, K) = S.sbustr(n-K, K) = S[n-k, n)串模式匹配(string pattern matching):对基于同一字符表的任何文本T(|T| = n)和模式串P(|P| = m): ...原创 2018-12-14 15:17:44 · 1874 阅读 · 0 评论 -
编程珠玑-给定一个最多包含40亿个随机排列的32位整数的顺序文件,找出一个不在文件中的32位整数
思路二分法。答案初始值为0,当前位i为最高位,比如32;对当前文件进行处理:根据当前第i位为0、为1,将文件一分为二;比较这两组数哪个较多,如果为0较多则答案第i为置0(不需要操作),为1较多则答案第i位置1;接着继续转向较多的那组数;重复以上步骤2-3。重复以上步骤2-3。正确性参考32位整数的范围是0到232(大于40亿),则对于一个随机40亿个整数来说,有232-40...原创 2019-02-27 17:17:51 · 738 阅读 · 2 评论 -
磁盘排序(外排序)
当参加排序的数的量太大,或内存不足以存放时,需要使用外排序。外排序可以使用插入排序的思想,也可以用归并排序的思想。下面是自己实现的归并排序思想的外排序,虽然基本做到了正确排序,且对内存的占用可以控制,但时间效率略低。实测100万数据的时候,空间占3MB(VS 2017),运行时间为700s,CPU为i7-6600U。代码示例GenerateRandomNumber.h文件,生成随机数序列。...原创 2019-02-20 16:35:22 · 2276 阅读 · 0 评论 -
不使用运算符实现“四则运算
加法:用^来计算加法;主要要考虑进位,而只有两个数的相同位都为1时才会出现进位,所以用&来计算是否要进位。int getSum(int a, int b){ int c = a&b, xor = a^b; return c==0? xor : getSum(xor, c);}减法:计算机中会把减法转换为“+反码”,因此与加法相似。乘法:可转换为“左移”与“...原创 2019-04-23 10:25:38 · 388 阅读 · 0 评论 -
5047. Minimum Score Triangulation of Polygon
Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], …, A[N-1] in clockwise order.Suppose you triangulate the polygon into N-2 triangles. For each triangle, the value of tha...原创 2019-05-05 11:58:00 · 301 阅读 · 0 评论 -
5049. Moving Stones Until Consecutive II
On an infinite number line, the position of the i-th stone is given by stones[i]. Call a stone an endpoint stone if it has the smallest or largest position.Each turn, you pick up an endpoint stone a...原创 2019-05-05 11:59:47 · 357 阅读 · 0 评论 -
最长回文子串线性算法---Manacher(马拉车)
这位朋友的文章挺好的,请转:https://blog.csdn.net/happyrocking/article/details/82622881LeetCode:5. Longest Palindromic SubstringC++实现算法:class Solution { string preProcess(const string& s) { string ans; ...原创 2019-06-29 21:29:00 · 174 阅读 · 0 评论 -
LeetCode 81. Search in Rotated Sorted Array II
81. Search in Rotated Sorted Array IISuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).You are given...原创 2019-08-06 08:31:38 · 115 阅读 · 0 评论 -
树的同构
树的同构描述:给定两棵树T1和T2。如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是“同构”的。例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A、B、G的左右孩子互换后,就得到另外一棵树。而图2就不是同构的。题解:int Isomorphism(Root R1, Root R2){ if(R1 == Null && R2 == Null)//...原创 2019-08-19 15:22:56 · 333 阅读 · 0 评论 -
Symmetric Tree
Symmetric Tree描述:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric:1/ \2 2/ \ / \3 4 4...原创 2019-08-19 15:25:01 · 101 阅读 · 0 评论 -
Validate Binary Search Tree
Validate Binary Search Tree描述Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys le...原创 2019-08-19 17:05:45 · 123 阅读 · 0 评论 -
根据BST的前序序列得到BST的后序序列
如题。代码示例vector<int> pre, post;void getPost(int root, int tail){ if(root > tail) return; int i = root+1, j=tail; while(i<=tail && pre[i] < pre[root]) ++i; whi...原创 2019-02-11 13:42:43 · 473 阅读 · 0 评论 -
PAT (Advanced Level) Practice - 1026 Table Tennis
1026 Table Tennis (30 分)A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they wi...原创 2019-01-19 11:41:24 · 229 阅读 · 0 评论 -
有权图单源最短路径 - Dijkstra算法
如题,下面是C++实现。int findMinDist(vector&lt;vector&lt;int&gt;&gt;&amp; G, vector&lt;int&gt;&amp; dist, vector&lt;bool&gt;&amp; collected) { int minV, minDist = INF; for原创 2018-12-14 15:33:00 · 349 阅读 · 0 评论 -
动态规划:刷油漆问题
描述 有n辆车排成一排,还有m种不同颜色的油漆,其中第i种油漆够涂ai辆车,同时所有油漆恰好能涂完n辆车。若任意两辆相邻的车颜色不能相同,有多少种涂油漆的方案?输入第一行包含一个正整数m。接下来一行包含m个正整数,第i个正整数表示ai。输出输出一个整数,表示答案除以23333的余数。样例1输入32 1 3样例1输出10样例1解释10个方案分别是:1 3 1 3 2 3...原创 2018-12-15 23:00:36 · 870 阅读 · 0 评论 -
回溯剪枝:n皇后问题
描述 n皇后问题:一个n×n的棋盘,在棋盘上摆n个皇后,满足任意两个皇后不能在同一行、同一列或同一斜线上的方案有多少种?输入第一行包含一个整数n。输出输出一个整数,表示方案数。样例1输入4样例1输出2代码实现#include &lt;bits/stdc++.h&gt;using namespace std;// ================= 代码实现开始 ===...原创 2018-12-16 00:00:43 · 1565 阅读 · 0 评论 -
中位数选取及第k位数选取
中位数选取描述任给有序向量S1和S2,如何找出它们归并后所得有序向量S=S1∪S2的中位数?蛮力解法代码实现(C++)template &lt;typename T&gt;T trivialMedian(vector&lt;T&gt;&amp; S1, int lo1, n1, vector&lt;T&gt;&amp; S2, int lo2, int n2) {原创 2018-12-17 10:17:04 · 735 阅读 · 0 评论 -
动态规划:倒水问题
倒水问题时间限制:1 sec空间限制:256 MB问题描述邓老师有有 2 个容量分别为 n 单位、m 单位的没有刻度的杯子。初始,它们都是空的。邓老师给了你 t 分钟时间。每一分钟,他都可以做下面 4 件事中的任意一件:用水龙头装满一个杯子。倒空一个杯子。把一个杯子里的水倒到另一个杯子里,直到一个杯子空了或者另一个杯子满了。什么都不做。邓老师希望最后能获得 d 个单位的水,假...原创 2018-12-17 15:14:59 · 1271 阅读 · 2 评论 -
动态规划:奶牛吃草问题
奶牛吃草问题描述在X轴上,用一个整树表示坐标。题目给定一个坐标表示奶牛起始位置,再给定一组坐标表示青草的位置,给出一个设定:每过一个单位时间,青草口感损失1个单位。假定奶牛运行速度就是一个单位/单位时间,忽略奶牛吃草的时间,并且要求奶牛吃掉所有青草,问最小青草损失口感为多少。输入格式第一行两个用空格隔开的整数 n,k,分别表示青草的数目和奶牛的初始坐标。第 2 行到第 n+1 行,第 ...原创 2018-12-17 15:24:01 · 2047 阅读 · 0 评论 -
最近点对:分而治之
简要题意平面坐标系中有若干点,求最近的一对点相距距离是多少。输入先输入点的个数n。接着输入n行,每行2个数分别表示x坐标、y坐标。输出直接输出一个浮点数,表示所给的所有点中最近的两个点之间的距离。保留2位小数。样例输入107 9-8 -1-3 -11 4-3 96 -47 56 6-6 100 8输出1.41解释距离最近的点为7和8,距离为(7−6)2+...原创 2018-12-24 13:44:55 · 295 阅读 · 0 评论 -
PAT (Advanced Level) Practice - 1003 Emergency
1003 EmergencyAs an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each...原创 2019-01-04 14:08:58 · 221 阅读 · 0 评论 -
二分查找---按需求返回目标位置(相同元素时任意)、不大于目标的最大下标位置、不小于目标的最小下标位置
如题。二分查找简单快捷,但也可以多种变化,其中按返回值可以将其分为如题中的三种。简单实现+测试如下。#include &amp;amp;amp;amp;lt;iostream&amp;amp;amp;amp;gt;#include &amp;amp;amp;amp;lt;vector&amp;amp;amp;amp;gt;#include &amp;amp;amp;amp;lt;algorithm&amp;原创 2019-01-08 14:57:14 · 992 阅读 · 0 评论 -
二分查找(lower_bound、upper_bound、binary_search)
http://www.cplusplus.com/reference/algorithm//*target < 最小值 :返回index=lefttarget > 最大值 :返回index=right其他:返回>=target的最小index*/int lower_bound(const vector<int>& nums, int left, i...原创 2019-09-22 19:10:19 · 261 阅读 · 0 评论