随笔分类 - [41]Algorithm算法
-
推荐算法实践
摘要:记我的初步推荐算法实践前一阵子参加了百度的电影推荐系统创新比赛。http://openresearch.baidu.com/activitycontent.jhtml?channelId=284。 之前没有实现过推荐算法,想趁这次机会锻炼一下。虽然成绩并不好,RMSE只有0.6214,没有挤进前30。任务描述:从用户的历史评分数据:userid,movieid,rating, 即用户对某个电影的评分,预测用户将会对一个未评分的电影打多少分。主办方还提供了用户的朋友关系,电影的类别等信息,但是这些我都没用上(原因最后分析),所以就只关心用户的历史行为数据。 这是一个评分预测问题。主要有两种途径. 阅读全文
-
阮一峰:字符串匹配的Boyer-Moore算法
摘要:阮一峰:字符串匹配的Boyer-Moore算法投递人itwriter发布于 2013-05-03 16:04评论(10)有818人阅读原文链接[收藏]«» 上一篇文章,我介绍了KMP 算法。 但是,它并不是效率最高的算法,实际采用并不多。各种文本编辑器的"查找"功能(Ctrl+F),大多采用Boyer-Moore 算法。 Boyer-Moore 算法不仅效率高,而且构思巧妙,容易理解。1977 年,德克萨斯大学的 Robert S. Boyer 教授和 J Strother Moore 教授发明了这种算法。 下面,我根据 Moore 教授自己的例子来解释 阅读全文
-
leetcode刷题笔录-6
摘要:leetcode刷题笔录-6验证二叉查找树给出一棵树,验证其是否是一棵二叉查找树。二叉查找树应当满足,对于每个节点:左子树中的所有节点的值小于该节点的值;右子树中的所有节点的值大于该节点的值;左子树和右子树都必须是二叉查找树。思路:递归检查每个节点的值是不是在某个区间 [min, max] 中,对根节点,检查其是不是在区间 [INT_MIN, INT_MAX] 中。对某个节点的检查区间,如此确定:如果该节点是父节点的左子结点,则 min 继承父节点检查区间的 min ,而 max 则为父节点的值;右子树类似。实现:/** * Definition for binary tree * struc 阅读全文
-
算法:“森林的遍历” 在 “企业应用” 的常见场景
摘要:算法:“森林的遍历” 在 “企业应用” 的常见场景森林的两种遍历方法前序(先根)遍历森林找到森林中的所有的根结点。前序遍历森林中的每个根节点对应的树。后序(后根)遍历森林找到森林中的所有的根结点。后序遍历森林中的每个根节点对应的树。树的两种遍历方法前序(先根)遍历树访问树的根结点。找到根节点的所有子节点。前序遍历每个子节点。后序(后根)遍历树找到根节点的所有子节点。后续序遍历每个子节点。访问树的根结点。企业应用场景 之 前序遍历效果图代码示例 1 /// <reference path="Ext/ext-all-debug-w-comments.js" /> 2 阅读全文
-
Tair的桶分布策略介绍及新的机器级位置安全优先策略实现
摘要:Tair的桶分布策略介绍及新的机器级位置安全优先策略实现Tair在其intro wiki上介绍了其现有的桶分布策略: 程序提供了两种生成分配表的策略, 一种叫做负载均衡优先, 一种叫做位置安全优先。 负载均衡优先 当采用负载优先策略的时候, config server会尽量的把桶均匀的分布到各个data server上. 所谓尽量是指在不违背下面的原则的条件下尽量负载均衡. 1 每个桶必须有COPY_COUNT份数据 2 一个桶的各份数据不能在同一台主机上; 位置安全优先 位置安全优先原则是说, 在不违背上面两个原则的条件下, 还要满足位置安全条件, 然后再考虑负载均衡. 位置信息的... 阅读全文
-
二分搜索的基本相关问题
摘要:二分搜索的基本相关问题二分查找是基于分治思想的一种算法,所谓分治思想就是将一些规模很大难于直接解决的问题,缩小为一个较小的问题就很容易解决的思想,(当然它的子问题仍可以继续分解为相同的子问题)。归结为一句话就是:“以大化小,各个击破,分而治之,组合取果”。二分查找作为一种高效的查找算法,是解决一些有序序列查找的不二之选。但他的缺点也就是使用于有序的数组,有一定的局限性。但二分在一些高效的程序设计中往往被用作优化的利器。因此,熟练应用二分查找是必须的。二分查找的实现:比如有一列数从1到100,已经排好序,我们要查找25,按照传统的逐个遍历,需要查找25次,而采用二分查找的方法,首先那25和这组数 阅读全文
-
找出一个整数数组中,第二大的数(整数数组作为函数参数)
摘要:找出一个整数数组中,第二大的数(整数数组作为函数参数)static void Main(string[] args) { int[] nums = { 1, 2, 10, 1, 5, 5, 3, 101, 11, 12, -1, 12 }; int secmax = SecNum(nums, nums.Length); #region 数组不做参数时,直接在主函数中写 //int max = nums[0]; ////int secmax = -1; //for (int i = 0; i < nums.Length; i++) //{ ... 阅读全文
-
leetcode刷题笔录
摘要:leetcode刷题笔录-4不同的子序列给定字符串 S 和 T ,计算 T 作为 S 的不同子序列的个数(不同,指子序列每个元素在原序列中的位置)。比如,给出 S="rabbbit" 和 T="rabbit" ,应当返回 3 ,应为有这样 3 种不同的子序列:"rabbbit","rabbbit","rabbbit"。思路:动态规划。T(1...n) 作为 S(1...m) 的不同子序列的数量(问题),就等于:如果 T[n]!=S[m] ,为:T(1...n) 作为 S(1...m-1) 的不同 阅读全文
-
欧拉公式
摘要:初等数论中的欧拉公式 求小于n的数里,与n互为素数的个数一. 奇数和偶数是否一定互素(排除1);1和不和任意数互素(比如6采用欧拉定理验证下)。 若n已经进行唯一分解,直接欧拉公式。 如果n的标准素因子分解式是p1^a1*p2^a2*……*pm^am,其中众pj(j=1,2,……,m)都是素数,而且两两不等。则有 φ(n)=n(1-1/p1)(1-1/p2)……(1-1/pm) 利用容斥原理可以证明它。二.不知唯一分解 1 #include<iostream> 2 #include<stdio.h> 3 using namespace std; 4 5 int main 阅读全文
-
相似字符串
摘要:编程之美 2013 全国挑战赛 初赛第一场 题目二 相似字符串题目二 相似字符串时间限制: 4000ms 内存限制: 256MB描述对于两个长度相等的字符串,我们定义其距离为对应位置不同的字符数量,同时我们认为距离越近的字符串越相似。例如,“0123”和“0000”的距离为 3,“0123”和“0213”的距离则为 2,所以与“0000”相比,“0213”和“0123”最相似。现在给定两个字符串 S1 和 S2,其中 S2 的长度不大于 S1。请在 S1 中寻找一个与 S2 长度相同的子串,使得距离最小。输入输入包括多组数据。第一行是整数 T,表示有多少组测试数据。每组测试数据恰好占两行,第一 阅读全文
-
算法学习笔记005——栈及应用
摘要:算法学习笔记005——栈及应用栈(stack)是一种实现后进先出(last-in, first out; LIFO)的容器。它是一种线性结构,当和向量、链表等线性数据结构不同的是,栈仅允许在一端进行操作:插入和删除。允许操作的一端成为栈顶(top),另一端就是栈低(bottom)。栈(stack)是一种很重要的数据结构,用途也很广泛,如:表达式计算,函数调用的现场保护,递归调用所需要的递归工作栈等等都是基于栈这种数据结构。(除此之外还有很多应用)。栈(stack)在C++标准模板库(STL)中实现了,以下是栈(stack)容器中的一些操作: 1 template <class T> 阅读全文
-
编程之美 2013 全国挑战赛 资格赛
摘要:编程之美 2013 全国挑战赛 资格赛 题目三 树上的三角形题目三 树上的三角形时间限制: 2000ms 内存限制: 256MB描述有一棵树,树上有只毛毛虫。它在这棵树上生活了很久,对它的构造了如指掌。所以它在树上从来都是走最短路,不会绕路。它还还特别喜欢三角形,所以当它在树上爬来爬去的时候总会在想,如果把刚才爬过的那几根树枝/树干锯下来,能不能从中选三根出来拼成一个三角形呢?输入输入数据的第一行包含一个整数 T,表示数据组数。接下来有 T 组数据,每组数据中:第一行包含一个整数 N,表示树上节点的个数(从 1 到 N 标号)。接下来的 N-1 行包含三个整数 a, b, len,表示有一根长 阅读全文
-
Algorithms
摘要:Algorithms一个关于兑换零钱的豆瓣笔试题摘要: 前几天做了个豆瓣笔试题,时间是90分钟,共有6题,要做4道,难度如果没看过类似的着实做起来太慢了。由于豆瓣上面的邮件说不要泄露(还有人会在后期笔试),所以拖到现在才写博客。我先把题目贴出来:将10000块钱兑换成由5000块、2000块、1000块、500块、100块、50块、10块、5块、1块的组成的零钱,问有多少种兑换方式? 这个题,如果朋友们没做过,或许最开始就跟我一样,钱有9种,就做个9重循环,各层累加,当总钱等于10000时就计数器加1,这样是很简单没错,可惜在这数据量的前提下,我用c跑了5、6分钟也没跑出来,于是中止了这种天真 阅读全文
-
如何比较两篇文章的相似度
摘要:如何比较两篇文章的相似度其实这个题目已经有很多人写过了,数学之美里就有,最近阮一峰的博客里也写了,本文基本上遵循的就是他的思路,只是让其看起来再小白一点点。其实说白了就是用自己的话,再把同样一件事描述一下,顺便扩扩句,把其中跳跃比较大的部分再补充补充。 当然虽然题目是比较两篇文章的相似性,但我们也不会傻到真拿两篇篇文章来说明,为了简单起见,我们从句子着手。句子A:周杰伦是一个歌手,也是一个叉叉句子B:周杰伦不是一个叉叉,但是是一个歌手 如何比较相似性呢 ? 第一步分词句子A : 周杰伦/是/一个/歌手,也/是/一个/叉叉 (注:假设分词也是个牛叉叉,可以识别叉叉这个词) ... 阅读全文
-
最长公共子序列
摘要:最长公共子序列本文从三个层次分析最大公共子序列最大公共子序列长度最大公共子序列算法分析首先来个区别:单词"cnblogs"子序列:从单词中抽取字符,不能保证连续抽取。如”cn"、“cns"、”bgs"连续子序列:从单词中连续抽取字符。如“bolog"、”cnbl"最长公共子序列(LCS:Longest Common Subsequence)顾名思义,就是几个词语中最长的相同子序列。比如“cnblogs"和”belong"最大公共子序列是“blog" 最长公共子序列是个非常有用的算法,可以判断两段 阅读全文
-
算法之我见
摘要:算法之我见之前发了一篇博文“24点的所有组合的解法”,有人颇不以为然。我想说的是,发这篇文章是说明我可以用计算机求出24点的所有组合的解法。而在实际的运用中,如果要使用24点的算法有时还不见得利用查表法来得简单和快速。毕竟,要短时间内写出正确的算法并验证,也不是一件很容易的事。24点游戏的规则:给定4个正整数(1到10),利用加减乘除运算,得出运算结果为24的运算式例如:1,2,3,4——(1+2+3)*4=241,5,5,5——(5-1/5)*5=24网上比较常见的24点算法是动态规划算法(这个在博客园中也能搜索到)。定义6个二元运算符:加、减、乘、除、反减、反除然后从4个数中任选2个数,通 阅读全文
-
随笔- 3 文章- 0 评论- 5 基于用户评价的评分模型
摘要:随笔- 3 文章- 0 评论- 5基于用户评价的评分模型一、场景 豆瓣图书、时光电影等索引类站点的不考虑时间因素的产品评分,其核心是通过用户的评价计算出可量化的分数来衡量产品的受欢迎程度。使用威尔逊区间法进行评分,并使用贝叶斯平均法修正评分。二、威尔逊区间法 威尔逊区间法是基于二项分布的一种计算方法,其结果与好评率和评价次数相关。其假设只有“喜欢”和“不喜欢”两个可选项,使其符合二项分布,并根据置信水平得到结果。 计算公式如下: 其中Smax是最大评分,pmin是威尔逊区间下限值,p是好评率(通常为平均值/总分值),n是评价总数,K是统计量常数(表示某个置信水平下z的统计量,在90%的... 阅读全文
-
装配线调度
摘要:装配线调度前言:动态规划的概念 动态规划(dynamic programming)是通过组合子问题的解而解决整个问题的。分治算法是指将问题划分为一些独立的子问题,递归的求解各个问题,然后合并子问题的解而得到原问题的解。例如归并排序,快速排序都是采用分治算法思想。本书在第二章介绍归并排序时,详细介绍了分治算法的操作步骤,详细的内容请参考:http://www.cnblogs.com/Anker/archive/2013/01/22/2871042.html。而动态规划与此不同,适用于子问题不是独立的情况,也就是说各个子问题包含有公共的子问题。如在这种情况下,用分治算法则会重复做不必要的工作。采. 阅读全文
-
数据结构的扩张
摘要:数据结构的扩张前言:通常我们会遇到一些问题,采用一些标准的数据结构,如双链表、散列表或二叉查找数时,不能够满足操作要求,需要对这些数据结构进行扩张,添加一些额外的信息使得能够完成新的操作。附加的信息需要对数据结构的某些操作进行调整,这个是非常关键的步骤,决定着数据结构扩张是否能够实现。本章主要讨论了红黑树结构的扩张,介绍了两种扩张方式。第一种方式扩张使得红黑色能够支持动态集合上顺序统计,快速找出集合中第i小的数,或给出某个元素在集合的全序中的排名。第二种方式扩张使得红黑色能够进行区间操作,可以很快地找到集合中覆盖的区间。关于红黑色请参考第13章,http://www.cnblogs.com/A 阅读全文
-
[转] Facebook杯2013年编程挑战赛——预选赛题目及答案
摘要:[译] Facebook杯2013年编程挑战赛——预选赛题目及答案原文 https://www.facebook.com/notes/facebook-hacker-cup/qualification-round-solutions/598486173500621今年的预选赛已经在1月29日结束了,总共有10169名选手成功解决了至少一道问题。来自密歇根大学的Ryan在50分钟内解决了全部三道问题,预选赛排名第一。预赛排名中的前500名选手获得了进入下一轮比赛的资格。第一题美丽的字符串(20分)对于一个字符串,定义这个字符串的“美丽程度”是其所有字母“美丽程度”的总和(sum)。每个字母都有一 阅读全文