针对IT笔试的几种常用算法
WaitingForSummer
这个作者很懒,什么都没留下…
展开
-
最长公共子串和子序列
//在牛客网上看到的,转了过来,侵权删哈 举个栗子: str1="123ABCD456" str2 = "ABE12345D" 最长公共子串是:123 最长公共子序列是:12345 这两个都可以用动态规划,只是状态转移方程有点区别 最长公共子序列是: dp[i][j] -- 表示子串str1[0...i]和子串str[0...j]的最长公共子序列 当str1[转载 2016-09-20 10:06:08 · 257 阅读 · 0 评论 -
堆排序
堆排序是利用二叉排序树中的父节点和子节点的内在关系,在当前无序序列中选择关键字最大(或者最小)的记录。 没空写了,把别人的粘贴来,记得他的代码有个小bug,看原贴的评论里有说 http://www.cnblogs.com/mengdd/archive/2012/11/30/2796845.html转载 2016-09-21 10:45:38 · 177 阅读 · 0 评论 -
迷宫-最短路径
华为oj和剑指offer都有相关题型,但针对不同题型会有不同解法,有动态规划取最优解和广度、深度遍历的方法。 这里针对华为oj上的原题,以及16年华为大牛专场的笔试题。 所求的是地图中的最短路径,但并不是只有一条路径。 华为最早的oj题里是只有一条路径,那么用深度遍历是最快捷的。 但如果有多条路径,需要就最短路径。还是要用广度遍历。 深度遍历无法保证求得最优解。 #includ原创 2016-09-20 20:34:49 · 831 阅读 · 0 评论 -
四则运算
同样还是华为oj上的题,利用栈的思想实现 最后算出的数据都只有加减法,没有乘除法。 #include #include #include #include #include #include using namespace std; void addnum(deque& rec, int pre) { if (!rec.empty()) { int cur = 0转载 2016-09-20 20:29:50 · 292 阅读 · 0 评论 -
绕圈拿红包的问题(动态规划)
我记得是美团的笔试题吧,记不清了,原题是n个红包,首尾相连,不能拿相邻的两个,求最多能拿多少钱。 这里只处理了3个以上的红包,3个以下,直接取最大那个就可以了。 #include #include #include #include #include #include #include using namespace std; int main() { int s1原创 2016-09-20 20:28:45 · 479 阅读 · 0 评论 -
火车站进站
同样是华为oj里的一道题,主要是利用了next_permutation这个函数实现了全排列。 #include #include #include #include #include #include using namespace std; bool isPop(vector rec, vectorp) { if (rec.size() return false;转载 2016-09-20 20:23:11 · 468 阅读 · 0 评论 -
把M个同样的苹果放在N个同样的盘子里
/* 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。 */ #include using namespace std; int putapples(int m, int n) { if (m return 0; if (m == 1 || n == 1) retu转载 2016-09-20 20:18:04 · 4591 阅读 · 0 评论 -
选择排序
选择排序的思想也是将待排序数据分成两部分,未排序部分和已排序部分。 方法是,每次在未排序序列中找最小,将其放到排序序列尾部。 时间复杂度是n^2,不稳定。 void select_sort(vector input, int n) { for (int i = 0; i { int k = i; for (int j = i + 1; j原创 2016-09-20 20:04:45 · 207 阅读 · 0 评论 -
插入排序和希尔排序
插入排序是从将待排列的数组分成两列,待排列的无序区和排列好的有序区; 思想就是每次从无序区取出第一个数据插入到有序区的正确位置。 时间复杂度是n~n^2,平均是n^2,档大部分数据有序的时候,插入排序的速度会大大提升。 是稳定的排序算法。 void incert_sort(vector input, int n) { for (int i = 1; i {原创 2016-09-20 19:52:43 · 162 阅读 · 0 评论 -
快速排序的两种实现
只是看到两种快速排序的实现方式,于是记录了下来,欢迎指正 时间复杂度都是nlogn~n^2;不稳定的查询方式,平均是nlogn; 第一种: void quick(vector input, int start, int end) { if (start >= end) return; int i = start + 1; int j = end + 1; int tm原创 2016-09-20 16:36:56 · 222 阅读 · 0 评论 -
大数的加减法—C++
#include #include #include using namespace std; void sw(string& a) { int len = a.size(); for (int i = 0; i { char tmp = a[i]; a[i] = a[len - 1 - i]; a[len - 1 - i] = tmp; } return; } s原创 2016-09-20 15:58:49 · 376 阅读 · 0 评论 -
华为oj上的购物单问题,背包问题的变种,动态规划
原题可以看华为oj上的购物单问题。 牛客上也有 #define max(x,y) (x)>(y)?(x):(y) using namespace std; int main() { int N, m; //N 总钱数 m为购买物品个数 int weight[61][3] = { 0 }; int value[61][3] = { 0 }; whi转载 2016-09-20 15:58:15 · 1333 阅读 · 0 评论 -
n枚硬币问题,先拿后拿问题,或者像完美世界16年笔试题,打boos的智力游戏
/*有n枚硬币按照0到n-1对它们进行编号,其中编号为i的硬币面额为vi,两个人轮流从剩下硬币中取出一枚硬币归自己所有,但每次取硬币的时候只能取剩下的硬币中编号最小的硬币或者编号最大的硬币,在两个都采用最优策略的情况下,作为先取硬币的你请编写程序计算出你能获得硬币总面额的最大值?(请简述算法原理,时间复杂度并实现具体的程序),语言不限。*/ #include #include using n转载 2016-09-20 15:56:47 · 2599 阅读 · 0 评论 -
利用动态规划解决-字符串的匹配问题
也可以找字符串的最大子序列,然后相减即可,但没有验证过。 int main () { string s1,s2; while( cin>>s1>>s2) { int len1 = s1.size(); int len2 = s2.size(); vector> dp(len1+1, vector(le原创 2016-09-20 15:53:21 · 2624 阅读 · 0 评论 -
最小生成树
比较适合图中的遍历最小路径 ,这里只有一种方法,是Prim算法实现 int main() { int n; while (cin >> n) { vector> dic(n,vector(n,0)); for (int i = 0; i { for (int j = 0; j { int tmp = 0; cin >> tmp; dic[i原创 2016-09-20 15:48:23 · 217 阅读 · 0 评论 -
归并排序
归并排序的思想是将多个有序序列,组合成新的有序表。 运用分治法思想解决排序问题。 分治法介绍:分治法就是将原问题分解为多个独立的子问题,且这些子问题的形式和原问题相似,只是规模上减少了,求解完子问题后合并结果构成原问题的解。 分治法通常有3步:Divide(分解子问题的步骤) 、 Conquer(递归解决子问题的步骤)、 Combine(子问题解求出来后合并成原问题解的步骤)。 假原创 2016-09-21 11:18:26 · 208 阅读 · 0 评论