
算法
文章平均质量分 53
HackQ_sxj
这个作者很懒,什么都没留下…
展开
-
Dijkstra与Floyd求取最短路径
最短路径算法:用于计算一个节点到其他所有节点的最短路径。是图论研究中的一个经典算法问题。一、Dijkstra算法:典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。Dijkstra算法能得出最短路径的最优解,但由于遍历计算的节点很多,所以效率低。可以用堆优化。按照路径长度递增的次序一步步并入来求取,是贪心算法的一...原创 2020-03-13 22:02:10 · 332 阅读 · 0 评论 -
面试常考算法题
一、[编程题]折纸问题时间限制:C/C++ 3秒,其他语言6秒空间限制:C/C++ 32M,其他语言64M请把纸条竖着放在桌⼦上,然后从纸条的下边向上⽅对折,压出折痕后再展 开。此时有1条折痕,突起的⽅向指向纸条的背⾯,这条折痕叫做“下”折痕 ;突起的⽅向指向纸条正⾯的折痕叫做“上”折痕。如果每次都从下边向上⽅ 对折,对折N次。请从上到下计算出所有折痕的⽅向。给定折的次数n,请返回...原创 2020-03-13 12:10:58 · 327 阅读 · 0 评论 -
剑指Offer48:最长不含重复字符的子字符串
题目:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度假设字符串中只包含'a'~'z'的字符例如,在字符串“arabcacfr”中,最长的不含重复字符的子字符串是“acfr”,长度为4思路:我们从头遍历,考虑每次所读的当前字符为结尾的字符串的不含重复字符的最长子字符串问题。如我们读到了第i个字符,假设当前以i为结尾的字符串的最长不含重复字符的字符...原创 2020-03-12 17:39:15 · 336 阅读 · 0 评论 -
分糖
N个孩子站成一排,给每个人设定一个权重。按照如下规则分配糖果:1)每个孩子至少分的一个糖果2)权重高的孩子,会比他的邻居获得更多的糖果问:总共最少需要多少颗糖果?分析算法思路,算法时间、空间复杂度?思路:假设每个孩子分到的糖果数组为A[N],初始化为{1},因为每个人至少分到一颗糖。1、与前面的邻居比较,前向遍历权重数组ratings,如果ratings[i] > r...原创 2020-03-12 17:22:36 · 336 阅读 · 0 评论 -
LRU缓存
设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作:获取数据 get 和 写入数据 put 。获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回-1。写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值...原创 2020-03-12 17:19:40 · 172 阅读 · 0 评论 -
常见的八种排序算法
排序按类别分为:插入排序(插入排序、希尔排序)、选择排序(选择排序、堆排序)、交换排序(冒泡排序、快速排序)、归并排序、基数排序。1. 冒泡排序/** * 从第一个数据开始,将顺序表中的每一个数据与其后一个数据比较大小 如果比后边大,就交换位置, 直到倒数第二个与倒数第一个比较完毕 * 我们可以得到最大的数,放在队尾, 不断循环执行,直到数组有序 */ public sta...原创 2019-11-26 11:09:17 · 614 阅读 · 0 评论 -
剑指Offer学习 【面试题23:从上往下打印二叉树】【面试题24:二叉搜索树的后序遍历序列】【面试题25:二叉树中和为某一值的路径】【面试题26:复杂链表的复制】
23题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左向右的顺序打印。public class Offer23 { public static class BinaryTreeNode{ int value; BinaryTreeNode left; BinaryTreeNode right; } public static void printFromToB...原创 2019-10-31 17:15:02 · 154 阅读 · 0 评论 -
剑指Offer学习【面试题21:包含min 函数的栈】【面试题22:栈的压入、弹出序列】
21题目: 定义栈的数据结构,请在该类型中实现一个能够得到栈的最小素的min 函数。在该栈中,调用min、push 及pop的时间复杂度都是0(1)/** * 定义栈的数据结构 * 使得返回最小元素min() pop() push()的时间复杂度都为O(1) * @author aa * @param <T> * */public class StackWithMi...原创 2019-10-30 11:10:56 · 173 阅读 · 0 评论 -
0815 - Andy's First Dictionary
思路: 读入字符串后,先进行大小写统一,并将标点符号转换为空格,方便使用stringstream函数进行切分。然后存入集合中,并最后输出即可。#include <iostream>#include <string>#include <set>#include <sstream>using namespace std;set<...原创 2019-03-20 19:39:40 · 356 阅读 · 0 评论 -
UVA156 - Ananagrams
思路: 因为要求输出时应保留输入中的大小写,所以我们将读入的字符串s先进行一个副本保存,存入不定长数组vector中。然后开始检测单词是否满足情况:首先对读入字符串s转化为小写,并根据字母序排序。然后检测若该字符串只出现过一次(即单词不能通过字母重排得到输入文本中的另外一个单词)则cnt[r]为1、否则为0。 然后统计words中的单词在映射map中的值是否为1,为1证明该单词不能重排...原创 2019-03-20 20:14:42 · 355 阅读 · 0 评论 -
uva213 - Message Decoding
#include <stdio.h>#include <string.h>int readchar();int readint(int c);int code[8][1<<8];int readcodes();int main(){ while(readcodes()) { for(;;) {...转载 2019-03-17 21:56:37 · 417 阅读 · 0 评论 -
12096 - The SetStack Computer
#include <iostream>#include <set>#include <stack>#include <map>#include <vector>#include <algorithm>using namespace std;#define ALL(x) x.begin(), x.end()#...原创 2019-03-21 18:41:22 · 273 阅读 · 0 评论 -
540 - Team Queue
#include <cstdio>#include <queue>#include <map>using namespace std;const int maxn = 1000+10;int main(){ int t, kase = 0; while(scanf("%d", &t) == 1&&t) ...转载 2019-03-21 18:42:00 · 221 阅读 · 0 评论 -
136 - Ugly Numbers
#include <iostream>#include <vector>#include <queue>#include <set>using namespace std;typedef long long LL;const int coeff[3] = {2, 3, 5};int main(){ priority_qu...转载 2019-03-21 19:26:37 · 204 阅读 · 0 评论 -
随机数生成并排序
#include <cstdlib>#include <vector>#include <iostream>#include <algorithm>#include <cassert>using namespace std;void fill_random_int(vector<int>& v, int...原创 2019-03-21 20:37:20 · 2726 阅读 · 0 评论 -
400 - Unix ls
法一:#include <iostream>#include <vector>#include <string>#include <algorithm>#include <cmath>using namespace std;int main(){ int N; while(cin >> N...转载 2019-03-22 21:04:47 · 207 阅读 · 0 评论 -
221 - Urban Elevations
#include <cstdio>#include <algorithm>using namespace std;const int maxn = 100+5;struct Building{ int id; double x, y, w, d, h; bool operator < (const Building& r...原创 2019-03-22 21:45:22 · 206 阅读 · 0 评论 -
814 - The Letter Carrier's Rounds
#include <iostream>#include <string>#include <vector>#include <set>#include <map>using namespace std;void parse_address(const string& s, string& user, str...原创 2019-03-23 09:57:33 · 535 阅读 · 0 评论 -
101 - The Blocks Problem
四个指令:1.move a onto b:把a和b上方的木块全部归位,然后把a摞在b上面。2.move a over b:把a 上方的木块全部归位,然后把a放在b所在木块堆的顶部。3.pile a onto b:把b上方的木块全部归位,然后把a及上面的木块整体摞在b上面。4.pile a over b:把a及上面的木块整体摞在b所在的木块堆的顶部。题目分析: 本题无需...原创 2019-03-20 18:57:08 · 208 阅读 · 0 评论 -
10474 - Where is the Marble?
#include <cstdio>#include <algorithm>using namespace std;const int maxn = 10000;int main(){ int n, q, x, a[maxn], kase = 0; while(scanf("%d%d", &n, &q)==2&&...转载 2019-03-20 16:41:18 · 155 阅读 · 0 评论 -
实现汽车加油问题
一辆汽车加满油后可行驶n公里。旅途中有若干个加油站。设计一个有效算法,指出应在哪些加油站停靠加油,使沿途加油次数最少。对于给定的n(n <= 5000)和k(k <= 1000)个加油站位置,编程计算最少加油次数。并证明算法能产生一个最优解。要求:输入:第一行有2个正整数n和k,表示汽车加满油后可行驶n公里,且旅途中有k个加油站。接下来的1 行中,有k+1 个整数,表示第k个加油站与第...原创 2018-05-11 11:36:31 · 2596 阅读 · 0 评论 -
独立任务最优调度问题
问题描述:独立任务最优调度,又称双机调度问题:用两台处理机A和B处理n个作业。设第i个作业交给机器A处理时所需要的时间是a[i],若由机器B来处理,则所需要的时间是b[i]。现在要求每个作业只能由一台机器处理,每台机器都不能同时处理两个作业。设计一个动态规划算法,使得这两台机器处理完这n个作业的时间最短(从任何一台机器开工到最后一台机器停工的总的时间)。数据输入: n = 6 a[n] = 2...原创 2018-05-14 21:13:38 · 4638 阅读 · 0 评论 -
401 - Palindromes
#include <stdio.h>#include <string.h>#include <ctype.h>const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";const char* msg[] = {"not a palindrome", "a regular palindrome", ...原创 2019-03-08 18:48:59 · 159 阅读 · 0 评论 -
10082 - WERTYU
#include <stdio.h>char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./"; //为表示\,这里的采取\\来实现转义int main(){ int i, c; while((c = getchar())!=EOF) { for(i = 1; s[i]&a...原创 2019-03-08 18:51:00 · 274 阅读 · 0 评论 -
340 - Master-Mind Hints
#include <stdio.h>#define maxn 1010int main(){ int n; int a[maxn], b[maxn]; int kase = 0; while(scanf("%d", &n)!=EOF&&n) { for(int i = 0; i < n; i...原创 2019-03-08 19:06:42 · 159 阅读 · 0 评论 -
1583 - Digit Generator
#include <stdio.h>#include <string.h>#define maxn 100005int a[maxn];int main(){ int T, n; memset(a, 0, sizeof(a)); for(int i = 1; i < maxn; i++) { int x =...原创 2019-03-08 19:46:57 · 163 阅读 · 0 评论 -
1584 - Circular Sequence - 环状序列
题目描述: 长度为n的环状串有n种表示法,分别为某个位置开始顺时针得到。CGAGTCAGCT,GAGTCAGCTC,AGTCAGCTCG等。在这些表示法中,字典序最小的称为“最小表示”。输入一个长度为n(n<=100)的环状DNA串(只包含A、C、G、T这4种字符)的一种表示法,你的任务是输出该环状串的最小表示。例如,CTCC的最小表示是CCCT,CGAGTCAGCT的最小表示为AGC...原创 2019-03-08 19:47:34 · 246 阅读 · 0 评论 -
1585 - Score
#include <stdio.h>#include <string.h>#define maxn 85int main(){ char a[maxn]; int n; scanf("%d", &n); while(n--) { scanf("%s", a); int len = s...原创 2019-03-09 10:37:18 · 218 阅读 · 0 评论 -
uva1586 - Molar mass
#include <cstdio>#include <cstring>double mass[256];int main(){ memset(mass, 0, sizeof(mass)); mass['C'] = 12.01; mass['H'] = 1.008; mass['O'] = 16.00; mass['N']...转载 2019-03-09 11:48:13 · 155 阅读 · 0 评论 -
uva1339 - Ancient Cipher
#include <stdio.h>#include <string.h>#include <stdlib.h>#define maxn 105int cmp(const void *a, const void *b){ return *(int *)a - *(int *)b;}int main(){ char a[max...转载 2019-03-14 11:23:05 · 250 阅读 · 0 评论 -
UVA1225 - Digit Counting
两种解题方法:1. 暴力#include <cstdio>#include <cstring>using namespace std;int ans[15];int main(){ int t; scanf("%d", &t); while(t--) { memset(ans, 0, sizeof(...原创 2019-03-09 21:03:46 · 133 阅读 · 0 评论 -
UVA 455 - Periodic Strings
#include <stdio.h>#include <string.h>int main(){ int n; scanf("%d", &n); while(n--) { char s[80]; scanf("%s", s); int len = strlen(s); ...转载 2019-03-09 22:57:09 · 126 阅读 · 0 评论 -
489 - Hangman Judge
#include <stdio.h>#include <string.h>#define maxn 100int left, chance;char s[maxn], s2[maxn];int win, lose;void guess(char ch){ int bad = 1; for(int i = 0; i < strlen(s...原创 2019-03-14 16:16:56 · 244 阅读 · 0 评论 -
uva133 - The Dole Queue
#include <stdio.h>#define maxn 25int n, k, m, a[maxn];int go(int p, int d, int t){ while(t--) { do{ p = (p+d+n-1)%n + 1; }while(a[p] == 0); } r...转载 2019-03-14 17:18:39 · 235 阅读 · 0 评论 -
512 - Spreadsheet Tracking
#include <cstdio>#include <cstring>const int maxd = 100;const int BIG = 10000;int r, c, n, d[maxd][maxd], d2[maxd][maxd], ans[maxd][maxd], cols[maxd];void copy(char type, int p, in...转载 2019-03-19 17:29:09 · 176 阅读 · 0 评论 -
Tex中的引号
在Tex中,左双引号是“``”,右双引号是“''”。将输入的内容转换为Tex的格式。#include <stdio.h>#include <string.h>int main(){ int c, q = 1; while((c = getchar()) != EOF) { if(c == '"') {...原创 2019-03-07 21:36:58 · 180 阅读 · 0 评论 -
UVA得分
一序列字符串(由X,O组成),每个O的得分为目前连续出现的O的个数,X的得分为0。如OOXO=1+2+0+1=4.#include #includeint main(){ int i,x,n,t,sum,len; char a[81]; scanf("%d",&n); for(x = 0; x < n; x++) { scanf("%s",a);原创 2017-10-15 21:30:22 · 395 阅读 · 0 评论