算法题
无
每天都要吃肉肉(●'◡'●)
绝不妄自菲薄
展开
-
【2012】计算最大叶子间距
问题描述:二叉树问题。比如节点是ABCDE编号是01234,给出每个左右子树的编号。求最大叶子间距。样例输入:输入1(如下左图):31 2-1 -1-1 -1输出1:2输入2(如下右图):61 -12 34 -1-1 5-1 -1-1 -1输出2:4#include<iostream>using namespace std;const int maxn = 11000;struct node { int left;..原创 2022-03-13 13:10:17 · 1578 阅读 · 0 评论 -
【2011】求三个字符串的最大子串
问题描述:输入3个子串, 输出这3个子串的最大公共子串样例输入:abcd acb abcab#include<cstdio>#include<iostream>#include<map>#include<vector>#include<cstring>#include<string>#include<algorithm>using namespace std;const int ma.原创 2022-03-13 12:00:53 · 210 阅读 · 0 评论 -
【2011】中序后序求层序
输入树的后序和中序排列,输出树的层次遍历Sample Input:72 3 1 5 7 6 41 2 3 4 5 6 7Sample Output:4 1 6 3 5 7 2#include<iostream>#include<queue>using namespace std;const int maxn = 110;int post[maxn], in[maxn];struct node { int data; struct node原创 2022-03-13 11:44:21 · 169 阅读 · 0 评论 -
【2012】字符串的重复输出
题目描述:给一个字符串比如ABC 再给一个整数比如3.输出AAABBBCCC就行了#include <string>#include <iostream>#include <cstdio>using namespace std; int main(){ string str; getline(cin, str); int n; cin >> n; for(int i = 0; i < str.原创 2022-03-12 11:25:57 · 1624 阅读 · 0 评论 -
【2012】堆排序模板
#include<iostream>using namespace std;const int maxn = 110;int a[maxn] = {};int n;void downadjust(int low, int high) { int i = low; int j = i * 2; while (j <= high) { if (j + 1 <= high && a[j + 1] > a[j]) j = j + 1; i.原创 2022-03-12 11:10:30 · 909 阅读 · 0 评论 -
【2013】四个小正方形放入大正方形
问题描述:Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way for the ACM-ICPC World Finals. Each of the four has a square-shaped suitcase with side length Ai (1<=i<=4) respectively. They want to pack their suitcases into a large squa.原创 2022-03-12 09:04:16 · 590 阅读 · 0 评论 -
【2015】优先队列的实现(没有思路。。)
题目描述ADD N P:往队列里加入id为N的优先级为P的任务NEXT:输出下一个最高优先级的任务的id,如果优先级相同输出id小的任务,若队列中没有任务输出-1REMOVE N:移除id为N的任务COUNT:输出队列中的任务数量思路用结构体数组充当优先级队列,记录队首和队尾的位置。ADD()先将输入的结构体加入数组中,即挂在队尾。再按优先级大小排序,优先级大的放在队头,若优先级相等,按ID大小排序,ID小得结构体靠近队头。NEXT()输入队头结构体的ID。#inc..原创 2022-03-11 22:25:44 · 153 阅读 · 0 评论 -
【2013】字符串匹配
问题描述:对于主串M和模式串P,找到P在M中出现的所有子串的第一个字符在P中的位置。P中第一个字符所在的位置为0。首行的数字表示有多少组字符串。样例输入:输入:2ababababaababaaaaaa输出:0 2 40 1思路:从头在m中匹配p,找到匹配的第一个字符串pos,继续从pos+1开始下一次匹配string知识点:str.find(str1), 当str1是str的子串时,返回其在str中第一次出现的位置;如果不是,返回string::.原创 2022-03-11 22:18:44 · 193 阅读 · 0 评论 -
【2014】汉诺塔问题(没思路。。)
问题描述:Hanoi 塔问题是印度的一个古老的传说。开天辟地的神勃拉玛在一个庙里留下了三根金刚石的棒,第一根上面套着64 个圆的金片,最大的一个在底下,其余一个比一个小,依次叠上去,庙里的众僧不倦地把它们一个个地从这根棒搬到另一根棒上,规定可利用中间的一根棒作为帮助,但每次只能搬一个,而且大的不能放在小的上面。请编写程序,把A 柱上的n 个金片,搬动到C 柱(中间可以使用B 柱),使得搬动的次数最少。输入金片的个数n(1<=n<=64),输出总搬动次数,以及最后100 次搬动。如果搬动次.原创 2022-03-11 20:27:36 · 109 阅读 · 0 评论 -
【2014】二叉树的遍历
问题描述:输入一棵二叉树,输出树的前、中、后序遍历结果。输入一个整数N(N<= 10000),表示树中有N个结点(编号0~N-1)。接下来N行,依次为结点0~结点N-1的左右孩子情况。每行3个整数,F,L,R。L,R为F的左右孩子。L,R如果为-1表示该位置上没有孩子。分三行分别输出树的前中后序遍历。同一行中的数字,用一个空格间隔。输入:50 3 11 2 -12 -1 43 -1 -14 -1 -1输出:0 3 1 2 43 0 2 4 13 4 2.原创 2022-03-11 18:45:56 · 1168 阅读 · 0 评论 -
【2014】二分查找的次数
问题描述:大家一定都能熟练掌握二分查找啦!那么来计算二分的次数吧!约定二分的中点mid = (left + right) / 2。输入:第一行输入一个整数N(N<=10000)。第二行输入N个升序整数。第三行输入一个待查找的整数(必定在第二行中出现过)。输出:输出二分查找该整数时,进行过多少次二分。输入样例:输入1:518 53 54 74 9953输出1:2输入2:70 1 2 3 4 5 65输出2:1注:查找到元素不计入次数...原创 2022-03-11 17:24:51 · 1430 阅读 · 0 评论 -
【2015工研】计算abc
数值类型一定要声明为double型的,否则上面的样例会输出YES#include<iostream>using namespace std;int main() { double a, b, c; cin >> a >> b >> c; if (a + b == c || a - b == c || b - a == c || a * b == c || a / b == c || b / a == c) cout << ...原创 2022-03-10 21:45:56 · 81 阅读 · 0 评论 -
【2015工研】长方形中的最大正方形
#include<iostream>#include<algorithm>using namespace std;int main() { int x, y; int ans = 0; cin >> x >> y; while (x != y) { ans++; if (x > y) swap(x, y); y -= x; } ans++; cout << ans; system("pause")...原创 2022-03-10 21:01:30 · 198 阅读 · 0 评论 -
编辑距离(双序列)
经动态规划:编辑距离#include<iostream>#include<string>#include<algorithm>using namespace std;const int maxn = 110;int dp[maxn][maxn];int main() { string s1, s2; cin >> s1 >> s2; for (int i = 1; i < s1.length()...原创 2022-03-10 17:23:43 · 405 阅读 · 0 评论 -
信封嵌套(最长递增子序列变种)
题目描述给n个信封的长度和宽度。如果信封A的长和宽都小于信封B,那么信封A可以放到信封B里,请求出信封最多可以嵌套多少层。输入描述:输出包含多行,第一行包括一个整数,代表信封的个数n(1≤n≤10e5)。接下来n行,每行两个整数li和wi,代表信封的长度和宽度(−1e9≤li,wi≤1e9)。输出描述:输出包括一行,代表信封最多嵌套多少层。输入93 42 34 51 32 23 61 23 22 4输出4先对宽度w进行升序排序,如果遇到w相同的情况,.原创 2022-03-09 23:12:50 · 146 阅读 · 0 评论 -
最长公共子序列(二维序列)
给你输入两个字符串s1和s2,请你找出他们俩的最长公共子序列,返回这个子序列的长度。比如说输入s1 = "zabcde", s2 = "acez",它俩的最长公共子序列是lcs = "ace",长度为 3,所以算法返回 3递归dp函数的定义是:dp(s1, i, s2, j)计算s1[i..]和s2[j..]的最长公共子序列长度明确选择:具体到每一个字符,思考每个字符该做什么还有一个小的优化,情况三「s1[i]和s2[j]都不在 lcs 中」其实可以直接忽略base case:i.原创 2022-03-09 22:44:02 · 580 阅读 · 0 评论 -
【2018工研】求线段交点
求线段交点,输入两组线段端点(整型),求其交点,不相交和无穷交点输出一句话就行,输出交点带小数的。输入: 0 0 5 50 2 2 0输出:1 1#include<iostream>using namespace std;int main() { int x0, y0, x1, y1; int x2, y2, x3, y3; cin >> x0 >> y0 >> x1 >> y1; cin >> x.原创 2022-03-09 22:15:13 · 146 阅读 · 0 评论 -
【2016】字符串的哈夫曼编码长度(优先队列)(没思路)
优先队列priority_queue知识点总结:只能用top()访问元素,常用函数有top/push/pop/empty/size 优先级设置思路:字符出现的频率则为其权值 哈夫曼树的编码长度等于各个叶节点权值与路径长度乘积之和,同时这个值等于非叶节点之和#include<iostream>#include<functional> //支持greater#include<string>#include<queue>#incl..原创 2022-03-08 21:07:34 · 239 阅读 · 0 评论 -
【2016】后缀表达式求值(栈)(简单)
#include<iostream>#include<stack>#include<string>using namespace std;int main() { string str; stack<int> s; cin >> str; for (int i = 0; i < str.length(); i++) { if (str[i] >= '0'&&str[i] <= '9'...原创 2022-03-08 18:07:42 · 101 阅读 · 0 评论 -
【2017】图的最小生成树(模板题)
题目:一个无向图,顶点为N个,其中M条边已给定,求使得整个图连通,且选出的边权值和最小。输入4 41 2 21 4 12 3 33 4 4输出6第一行的两个数据分别为N顶点个数和边M的个数;下面的M行为每条边的数据,起始点和终点,还有每条边的权值。#include<iostream>#include<algorithm>using namespace std;const int maxn = 10010,INF=99999999;in..原创 2022-03-08 17:03:26 · 148 阅读 · 0 评论 -
【2017】ISBN编码(字符串处理)(水题)
题目:给定一个9位数字的ISBN,求其校验位。ISBN格式为2-02-033598,校验位的计算方法如下:从左到右依次将各位数字乘10,9,8,……,2,求出其和S,作模运算得M=S mod 11。若11-M在1和9之间,校验位即为该数字;若11-M等于10,校验位为X;11-M等于11,校验位为0。输出添加校验位的ISBN,如2-02-033598-0。输入:2-02-033598输出2-02-033598-0#include<iostream>#include&原创 2022-03-08 13:38:42 · 176 阅读 · 0 评论 -
【2017】求中位数(水题)
题目:给定一个整数序列,求中位数。如果序列个数为奇数,中位数为升序的中间位置,如果是偶数,这位升序的中间两个数的平均值。输入:52 1 4 3 5输出:3输入:41 4 3 2输出:2.5double型:%.lf表示四舍五入到个位%.2lf表示四舍五入到小数点后两位#include<iostream>#include<algorithm>using namespace std;const int maxn = 1..原创 2022-03-08 12:33:18 · 143 阅读 · 0 评论 -
【2018】骨牌
题目:有2*n 的地板,用1*2和 2*1 的骨牌进行铺地板。问共有多少种情况。结果对 999983 取余,1<=n<=10000 。输入: 6 输出: 13#include<iostream>using namespace std;const int maxn = 11000;int main() { int dp[maxn] = {}; int n; cin >> n; dp[1] = 1; dp[2] = 2;...原创 2022-03-08 12:08:22 · 134 阅读 · 0 评论 -
【2018】求解一元一次方程(字符串处理)(没思路。。)
题目:给定一个字符串,代表一个 一元一次 方程。如果有解求解,输出格式“x=数字” ,如果解的个数无穷,输出 “infinite solutions”,如果没有解输出“no solution”。字符串长度不超过 256 。输入:10x-2x-8=4x+7+x输出:x=5#include<iostream>#include<string>using namespace std;int main() { string s; cin >> s;..原创 2022-03-08 11:38:35 · 412 阅读 · 0 评论 -
【2018】求众数(map)
题目:众数就是一个序列中出现次数最多的数字。 如果不唯一,则输出小的那个值。 给定第一个代表有几个数字。1<=n<=10^5 每个数字在 int 范围内 输入,(第一个代表有几个数字)810 3 8 8 3 2 2 2输出 2题很简单,但要注意用10^5做数组下标,开不了那么大,故用mapmap要点:如果是字符串到整型的映射,必须使用string而不是char数组 访问方式:(1)直接用下标访问:mp['s'];(2)迭代器访问:键it->fi...原创 2022-03-07 21:00:13 · 371 阅读 · 0 评论 -
【2018工研】求公约数之和
题目:输入一个数n,输出前n个数的约数的和。(印象中有1s的时间限制,大数据集可能超时,比如100000000)。输入:7输出:41约数:整数A除以整数B(B≠0) 除得的商正好是整数而没有余数,我们就说A能被B整除,或B能整除A。A称为B的倍数,B称为A的约数#include<iostream>using namespace std;int gcd(int a, int b) { if (b == 0) return a; else return原创 2022-03-07 20:10:52 · 1836 阅读 · 0 评论 -
【2018工研】求交集并集(set的用法)
题目第一题,输入两个集合,分别求其交集和并集中元素的个数,每个集合中可能存在相同的元素,而最终的交集和并集中应该不存在。输入:4 53 4 7 34 6 3 2 6输出:2 5这道题看着简单却花了很长时间,因为除了集合之间有重复,集合内部也有重复,我的处理方法是设立A,B两个数组,A控制集合之间的重复元素,B控制第二个集合内部的重复元素#include<iostream>using namespace std;const int maxn = 1原创 2022-03-07 19:27:36 · 263 阅读 · 0 评论 -
【A1082/字符处理】用中文读数字(挺绕的)
#include<iostream>#include<string>using namespace std;char num[20][20] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };char wei[20][20] = { "Shi","Bai","Qian","Wan","Yi" };int main() { string s; cin >> s; int len ...原创 2022-03-05 14:12:29 · 58 阅读 · 0 评论 -
【A1077/字符处理】多字符串的字符比较(思维挺巧妙)
思路:1、为什么做字符反转:每个字符串的长度难以确定,导致很难比较后缀,进行反转后,将后缀转为前缀方便比较2、使用gets一定要注意用getchar吸收多余字符3、比较完最短的字符串就可得到结果#include<iostream>#include<cstring>#include<algorithm>using namespace std;void reverse(char s[],int len) { for (int i = 0; i ..原创 2022-03-05 12:11:35 · 286 阅读 · 0 评论 -
【A1001/字符处理】A+B format
#include<iostream>using namespace std;const int maxn = 110; //数组下标不能设太大会溢出,一般设在250000以内int main() { int a, b; int ans[maxn] = {0}; cin >> a >> b; int temp = a + b; if (temp < 0) { cout << '-'; temp = -temp; } int...原创 2022-03-04 22:14:34 · 47 阅读 · 0 评论 -
【B1048/字符处理】数字加密
#include<iostream>#include<algorithm>using namespace std;const int maxn = 110;int main() { int a, b; int A[maxn] = { 0 }, B[maxn] = { 0 }; //char C[maxn] = { 0 }; 字符数组不初始化会输出 烫烫烫... string C; cin >> a >> b; int m = 0,...原创 2022-03-04 21:38:55 · 128 阅读 · 0 评论 -
【A1035】password(水题)
#include<iostream>#include<string>using namespace std;const int maxn = 1100;struct stu { string name; string password;}stu,res[maxn];int main() { int n,cnt=0; bool flag; cin >> n; for (int i = 0; i < n; i++) { cin >...原创 2022-03-04 19:56:41 · 83 阅读 · 0 评论 -
【A1005/字符处理】(水题)
#include<iostream>using namespace std;const int maxn = 10000;int main() { char mp[20][20] = { "zero","one","two","three","four","five","six","seven","eight","nine" }; int x, ans = 0; int temp[maxn] = {}; cin >> x; while (x != 0) { ...原创 2022-03-04 18:53:09 · 143 阅读 · 0 评论 -
【A1073/字符处理】科学计数法(有点绕)
#include<iostream>#include<string>using namespace std;int main() { string s; cin >> s; int pos = 0; while (s[pos] != 'E') //找到E的位置将底数和指数分割 pos++; int exp = 0; for (int i = pos + 2; i < s.length(); i++) //将字符型的指数化为1...原创 2022-03-04 17:06:26 · 74 阅读 · 0 评论 -
【B1009/字符处理】说反话(二维字符数组)
存储字符串,用二维数组#include<iostream>using namespace std;int main() { char s[90][90]; int i = 0; while (scanf("%s", s[i++]) != EOF); for (int j = i-2; j >= 0; j--){ //-2的原因是while结束i多加了一个1且字符从0开始 cout << s[j]; if(j!=0) cout<<...原创 2022-03-04 12:13:04 · 65 阅读 · 0 评论 -
【动态规划】最长上升子序列(序列问题)
给定一个无序的整数数组,找到其中最长上升子序列长度。输入:810 9 2 5 3 7 101 18输出:4明确dp含义:dp[i]表示以nums[i]这个数结尾的最长递增子序列的长度 明确选择:假设dp[0...i-1]都已经被算出来了,然后问自己:怎么通过这些结果算出dp[i](数学归纳思想) 明确 base case:dp[i]初始值为 1,因为以nums[i]结尾的最长递增子序列起码要包含它自己#include<iostream>#in...原创 2022-03-03 17:44:49 · 354 阅读 · 0 评论 -
【动态规划】最长子数列和(序列问题)
明确dp数组:以nums[i]结尾的「最大的子数组和」为dp[i]明确选择:dp[i]有两种「选择」,要么与前面的相邻子数组连接,形成一个和更大的子数组;要么不与前面的子数组连接,自成一派,自己作为一个子数组明确base case:dp[i]初始值为 num[i]#include<iostream>#include<algorithm>using namespace std;const int maxn=110;int main() { int n, n...原创 2022-03-03 19:01:20 · 610 阅读 · 0 评论 -
【动态规划】换零钱问题
题目:给你k种面值的硬币,面值分别为c1, c2 ... ck,每种硬币的数量无限,再给一个总金额amount,问你最少需要几枚硬币凑出这个金额,如果不可能凑出,算法返回 -1 。原创 2022-03-02 21:02:27 · 533 阅读 · 0 评论 -
【B1010/模拟】多项式求导(巧用数组)
倒序输出时,i不能=maxn ,这是越界访问了,最大从maxn-1开始#include<iostream>#include<cstdio>using namespace std;const int maxn = 1000;int main() { int p[maxn] = { 0 }, A[maxn] = { 0 },e,x; while (scanf("%d%d", &x, &e) != EOF) { A[e] = x; } for ..原创 2022-03-02 20:26:07 · 87 阅读 · 0 评论 -
【A1027/模拟】10进制转13进制(进制转换)
题目:将10进制转为13进制,其中13进制为0~9,A~C;,输出第一个字符为#代码:#include<iostream>using namespace std;int main() { char mp[13] = { '0','1','2','3','4','5','6','7','8','9','A','B','C' }; int temp; for (int i = 0; i < 3; i++) { cin >> temp; if (i ==原创 2022-03-02 20:00:26 · 517 阅读 · 0 评论