
数据结构与算法
Data structure and Algorithm
夜淡
这个作者很懒,什么都没留下…
展开
-
C++刷题时应该返回对象还是返回对象的指针?
本文仅针对C++写算法题时,遇到要返回对象的情况时,应该返回对象还是返回对象的指针做出探讨。众所周知,C++的变量和java、python不同,C++的变量并不是天然的指针,所以对象类型的变量之间的赋值远比java、python耗时,例如下列代码://cppvector<int> a(10000);vector<int> b = a; // 拷贝构造,a和b两个vector有各自的独立空间,需要消耗时间和空间//pythona = [0 for _ in rang原创 2021-09-21 05:35:50 · 637 阅读 · 0 评论 -
c++ map遍历过程中删除元素的坑
结论:不同的环境下表现是不一样的。#include <bits/stdc++.h>using namespace std;int main(){ map<int,int> mp; mp[1] = 1; mp[2] = 2; mp[3] = 3; auto it = mp.find(1); mp.erase(it); cout << it->second << endl; //Mac homebrew安装的g++ 11.2.0 :原创 2021-09-19 04:37:33 · 627 阅读 · 0 评论 -
KMP算法详解
KMP算法是求解主串(以下简称为s)和模式串(以下简称为p)匹配问题的O(n)算法。其核心思想就是,当s[i]和p[j]发生不匹配现象时,i指针不需要回溯,只需j指针回溯。例如:当s[i]和p[j]发生失配,一种暴力的方法就是i回溯到上一次起始位置的下一个位置,j移动到0,然后重新进行比较。但是实际上,从我们人的角度来看,solution1一定是进行了一次无用的匹配,因为这次匹配一定是无...原创 2019-03-15 19:44:33 · 18407 阅读 · 6 评论 -
dijkstra算法详解(普通算法和堆优化算法)
今天开始不定期写一写我对各种基本数据结构与算法的详解。 当初一无所知的我也是看着各位网上优快云的博主写的博客慢慢的了解各种算法。 从最初的dijkstra算法,到后来的tarjan,从最初的set,map容器,到现在手写splay,线段树。 几乎99%的知识都来源于查看别人的优快云博客学习。 所以,现在虽然不会经常做算法题和搞OI了,但是还是想把自己对各路算法的心得,回馈给大家。 希...原创 2018-09-15 15:10:02 · 23623 阅读 · 17 评论 -
数据结构与算法模板
经常打codeforce,比赛,给学生讲算法那的啥啊的都需要模板 那么也把我整理的这些基础数据结构与算法的模板分享给大家 可能有一点点小错误,可以在评论区留言哈~namespace I_love_xinxin{/* 名称定义: n: 数据规模 m: 表示边数等数据规模 v: vertex 图中的顶点 fa: father 一般用来指代父亲 r......原创 2018-09-15 15:16:37 · 1429 阅读 · 0 评论 -
L3-002. 堆栈
L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有“入栈”(将新元素插入栈顶)和“出栈”(将栈顶元素的值返回并从堆栈中将其删除)。现请你实现一种特殊的堆栈,它多了一种操作叫“查中值”,即返回堆栈中所有元素的中值。对于N个元素,若...原创 2018-02-24 12:52:53 · 294 阅读 · 0 评论 -
gplt L2-001. 紧急救援
L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务是带领...原创 2018-02-18 16:01:00 · 194 阅读 · 0 评论 -
Hello 2018 C. Party Lemonade
C. Party Lemonade time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output A New Year party is not a New Year party without lemonade! As usual, you...原创 2018-02-14 00:21:31 · 250 阅读 · 0 评论 -
1020 Delete At Most Two Characters(35 分)
1020 Delete At Most Two Characters(35 分) Given a string which contains only lower case English letters, how many different strings you can get after deleting AT MOST TWO characters in it?Input Spec...原创 2018-06-20 16:50:12 · 352 阅读 · 0 评论 -
1015 Letter-moving Game (35)(35 分)
1015 Letter-moving Game (35)(35 分) Here is a simple intersting letter-moving game. The game starts with 2 strings S and T consist of lower case English letters. S and T contain the same letters but t...原创 2018-06-19 22:08:29 · 279 阅读 · 0 评论 -
2018 ZJU 天梯赛选拔第二场 - E
2018 ZJU 天梯赛选拔第二场 - E难度大如狗Time Limit: 1 Second Memory Limit: 65536 KB 风学长有一个只包含 “+” 和数字的算式,但是因为调皮的金学长,算式里的 “+” 全都丢失了。风学长很难过,你作为他的基友,希望你能帮他算出,对于剩下的数字串,有多少种可能的合法算式,并将所有算式之和告诉他,他可能会考虑 bg 你...原创 2018-03-01 00:56:57 · 328 阅读 · 0 评论 -
Codeforces Round #465 (Div. 2) D. Fafa and Ancient Alphabet
D. Fafa and Ancient Alphabet time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Ancient Egyptians are known to have used a large set of symb...原创 2018-03-04 14:28:48 · 256 阅读 · 0 评论 -
Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1) C. Producing Snow
C. Producing Snow time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Alice likes snow a lot! Unfortunately, this year’s winter is already ove...原创 2018-03-11 15:59:13 · 230 阅读 · 0 评论 -
Educational Codeforces Round 40 (Rated for Div. 2) E. Water Taps
E. Water Taps time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Consider a system of n water taps all pouring water into the same container...原创 2018-03-24 20:50:45 · 371 阅读 · 0 评论 -
zoj 3954 Seven-Segment Display
A seven segment display, or seven segment indicator, is a form of electronic display device for displaying decimal numerals that is an alternative to the more complex dot matrix displays. Seven segmen...原创 2018-03-28 16:00:31 · 184 阅读 · 0 评论 -
D. Contact ATC
D. Contact ATC time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Arkady the air traffic controller is now working with n planes in the air. ...原创 2018-03-31 14:25:05 · 1764 阅读 · 0 评论 -
Educational Codeforces Round 35 (Rated for Div. 2) F. Tree Destruction
F. Tree Destruction time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given an unweighted tree with n vertices. Then n - 1 followin...原创 2018-04-01 15:56:59 · 204 阅读 · 0 评论 -
1014 Circles of Friends (35)(35 分)
1014 Circles of Friends (35)(35 分) A circle of friends is a network of friend relationships. If A is a friend of B, then B is considered a friend of A no matter B admits or not, and they are said to ...原创 2018-06-16 15:17:26 · 445 阅读 · 0 评论 -
1018 Subnumbers (35)(35 分)
1018 Subnumbers (35)(35 分) Given a positive integer N, let us define a “subnumber” of N as a consecutive number of digits NOT starting with 0. For example if N = 1021, it has 7 subnumbers, namely, 1,...原创 2018-06-14 23:38:10 · 1425 阅读 · 0 评论 -
PAT甲级 1160 Forever (20 分)
“Forever number” is a positive integer A with K digits, satisfying the following constrains:the sum of all the digits of A is m;the sum of all the digits of A+1 is n; andthe greatest common diviso...原创 2019-09-10 13:24:03 · 1591 阅读 · 2 评论 -
PAT甲级 1154 Vertex Coloring (25 分)
题意检查一幅图是否所有的相邻两点颜色都不一样。代码#include <bits/stdc++.h>using namespace std;const int N = 1e4 + 10;typedef long long ll;struct edge{ int u, v;}arr[N];int col[N];int main(void){ int i, j,...原创 2019-08-23 01:49:19 · 117 阅读 · 0 评论 -
PAT甲级 1153 Decode Registration Card of PAT (25 分)
题意比较长就不解释了代码#include <bits/stdc++.h>using namespace std;const int N = 1e4 + 10;typedef long long ll;typedef pair<int, int> pii;#define fi first#define se secondstruct node{ str...原创 2019-08-23 01:33:42 · 101 阅读 · 0 评论 -
PAT甲级 1152 Google Recruitment (20 分)
题意给一串数,找连续k个数字的首个素数。代码#include <bits/stdc++.h>using namespace std;const int N = 1e3 + 10;typedef long long ll;bool judge(int num){ if (num <= 1) return false; for (int i = 2; i*i...原创 2019-08-23 00:09:47 · 219 阅读 · 0 评论 -
Codeforces Round #578 (Div. 2) D. White Lines
题意https://codeforces.com/contest/1200/problem/D一个n*n黑白相间的棋盘,可以让任意一个k*k的区域变白,能得到的最大全白行列数之和是多少。思路用一个数组维护每一行的最左黑棋子和最右黑棋子,假设擦除点为(i, j),第i行的最左黑棋子和最右黑棋子分别为l和r,那么只要i<=l<=r<=i+k−1i &a...原创 2019-08-13 04:44:20 · 173 阅读 · 0 评论 -
PAT甲级 1159 Structure of a Binary Tree (30 分)
题意根据中后序建树,然后对于不同的查询判断正确性代码1#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;typedef long long ll;int post[35];int in[35];struct tree{ int data; tree *left, *right, ...原创 2019-08-24 20:24:22 · 581 阅读 · 0 评论 -
PAT甲级 1155 Heap Paths (30 分)
题意判断一个完全二叉树是不是堆代码#include <bits/stdc++.h>using namespace std;const int N = 1e3 + 10;typedef long long ll;struct node{ int data; node *left, *right; node(){} node(int a):data(a){left ...原创 2019-08-23 03:37:08 · 183 阅读 · 0 评论 -
PAT 1002. Business (35)
As the manager of your company, you have to carefully consider, for each project, the time taken to finish it, the deadline, and the profit you can gain, in order to decide if your group should take t...原创 2018-04-17 23:29:11 · 440 阅读 · 0 评论 -
PAT (Top Level) Practice 1009 Triple Inversions (35)
逆序对问题的升级版本(不知道的童鞋自行百度BIT/线段树求逆序对) 本体思路以三个数中间的数为基准,找前面比他大得数有几个 后面比他小的数有几个(可以算出来) 然后相乘+到res里即可#include <bits/stdc++.h>using namespace std;#define N (int)1e5+10int arr[N];#define lc root&l...原创 2018-06-09 19:13:44 · 294 阅读 · 0 评论 -
1023 The Best Polygon (35)(35 分)
1023 The Best Polygon (35)(35 分) An n-gon is a polygon with n sides. For example, a triangle is a 3-gon. Now you are asked to find the best n-gon in a given convex N-gon. The vertices of the n-gon ar...原创 2018-06-09 20:06:38 · 823 阅读 · 2 评论 -
PAT (Top Level) Practice 1005 Programming Pattern (35)(35 分)
先贴一个作弊单哈希AC的代码哈哈哈哈哈。#include &lt;bits/stdc++.h&gt;using namespace std;#define N (int)2e6typedef long long ll;ll p1, p2;ll mod[2] = {10000007, 1000000009};const int seed = 131;string str;map...原创 2018-06-10 00:06:31 · 320 阅读 · 0 评论 -
PAT (Top Level) Practice 1010 Lehmer Code (35)(35 分)
1010 Lehmer Code (35)(35 分) According to Wikipedia: “In mathematics and in particular in combinatorics, the Lehmer code is a particular way to encode each possible permutation of a sequence of n numb...原创 2018-06-10 11:42:29 · 260 阅读 · 0 评论 -
Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) C. Alternating Sum
C. Alternating Sum time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output注意判断q为1的情况 代码:#include &lt;bits/stdc++.h&gt;using namespace std...原创 2018-04-18 11:14:58 · 692 阅读 · 0 评论 -
分治法求平面最近点对
题意即为平面上有n个点,求距离最近的两个点,很显然,暴力求解是n^2的。 那么可以利用分治法把时间复杂度降到nlognlogn 当然,利用归并排序时可以降到nlogn的,不过显然我是写不来归并排序的原理是这样的,先把平面上的点按x,y的顺序排序//nlogn 然后再找下标为中间的点,把平面上的点均匀两等分 然后最小点对一定是处于左边最小,右边最小和跨边界最小。 跨边界最小该如何求解呢...原创 2018-04-20 00:20:26 · 2022 阅读 · 1 评论 -
1084. Broken Keyboard (20)
写完之后才发现应该先把所有的输入变成大写字母,就没后面这么多toupper要写了。 懒得改了。。。 日常水题#include <vector>#include <iostream>#include <algorithm>#include <string>#include <set>#include <cctype>using namespace std;int main(void)原创 2017-10-14 11:24:12 · 148 阅读 · 0 评论 -
1102. Invert a Binary Tree (25)
水题,没啥好说的。 不过参考了http://blog.youkuaiyun.com/apie_czx/article/details/48415169的方法。 就是不要传统的设置左右指针,而是int左右和parent节点,也不用传统的建树。 直接数组储存一切就可以了。确实省事多了。 早上起来码的,码风不是很好,凑合着看吧。。。#include <bits/stdc++.h>using namespac原创 2017-10-31 12:46:15 · 143 阅读 · 0 评论 -
1100. Mars Numbers (20)
#include <bits/stdc++.h>using namespace std;int main(void){ int n; cin >> n; getchar(); string arr1[13] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct"原创 2017-10-30 13:28:07 · 182 阅读 · 0 评论 -
1097. Deduplication on a Linked List (25)
日常水题 AC代码:#include <bits/stdc++.h>using namespace std;struct node{ int now; int value; int next;};int main(void){ int start, n; int i; cin >> start >> n; vector<node>原创 2017-10-29 11:44:07 · 131 阅读 · 0 评论 -
1082. Read Number in Chinese (25)
Read Number in Chinese (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an integer with no more than 9 digits, you are supposed to read it in the traditional Ch原创 2017-10-11 19:20:01 · 16428 阅读 · 0 评论 -
1080. Graduate Admission (30)
最近几天心情比较糟糕 事情比较多 代码也写得非常的杂乱 凑合着看吧,反正能AC#include <bits/stdc++.h>using namespace std;struct grade{ int id; int gg; int ig; vector<int> zhiyuan;};struct arra{ int a; int b;原创 2017-10-11 12:45:20 · 185 阅读 · 0 评论 -
1087. All Roads Lead to Rome (30)
一直觉得自己dijkstra算法已经比较熟了。 遇到这种变化的题目还是很难靠自己写出来。 唉,没有天分学算法真是慢啊。。。 参考了http://blog.youkuaiyun.com/qq_26437925/article/details/47683767这位大佬的算法。 写的很清晰,能读懂。#include <bits/stdc++.h>using namespace std;map<string原创 2017-10-18 18:51:30 · 347 阅读 · 0 评论