Stanford 算法入门 week 6 Bloom Filter, Hash Function, Search Tree

来源:互联网 发布:网络主题团日活动 编辑:程序博客网 时间:2024/05/16 16:54

Bloom Filter 讲解:http://blog.csdn.net/jiaomeng/article/details/1495500


-----------------------------------------------------------------------------------------------------------------------

Programming Question - 6

Question 1

Download the text file here. (Right click and save link as).

The goal of this problem is to implement a variant of the 2-SUM algorithm (covered in the Week 5 lecture on hash table applications)

The file contains 500,000 positive integers (there might be some repetitions!).This is your array of integers, with theith row of the file specifying the ith entry of the array.

Your task is to compute the number of target values t in the interval [2500,4000] (inclusive) such that there are distinct numbersx,y in the input file that satisfy x+y=t. (NOTE: ensuring distinctness requires a one-line addition to the algorithm from lecture.)

Write your numeric answer (an integer between 0 and 1501) in the space provided.

As an optional exercise, you might try implementing your own hash table for this question.


Question 2

Download the text file here.

The goal of this problem is to implement the "Median Maintenance" algorithm (covered in the Week 5 lecture on heap applications). The text file contains a list of the integers from 1 to 10000 in unsorted order; you should treat this as a stream of numbers, arriving one by one. Letting xi denote the ith number of the file, the kth median mk is defined as the median of the numbers x1,…,xk. (So, if k is odd, then mk is ((k+1)/2)th smallest number among x1,…,xk; if k is even, then mk is the (k/2)th smallest number among x1,…,xk.)

In the box below you should type the sum of these 10000 medians, modulo 10000 (i.e., only the last 4 digits). That is, you should compute(m1+m2+m3+⋯+m10000)mod10000.

As an optional exercise, you might compare the performance achieved by heap-based and search-tree-based implementations of the algorithm.
-----------------------------------------------------------------------------------------------------------------------
第一题的代码
#include <iostream>#include <fstream>#define MAX 1000000#define MIN 4000using namespace std;int hash[MIN + 1] = { 0 };int count = 0;void readData() {ifstream fin("HashInt.txt");int temp = 0;while(fin>>temp) {if(temp < MIN) hash[temp]++;}}bool hashMap(int n) {if(n > MIN) return false;if(hash[n]) return true;else return false;}int main() {readData();for(int i = 2500; i <= 4000; i++) {for(int j = 1; j <= (i - 1) / 2; j++) {if(hashMap(j) && hashMap(i - j)) {count++;break;}}}cout<<count<<endl;return 0;}


第二题的思路:http://www.cnblogs.com/lienhua34/archive/2011/12/06/2381299.html

利用大顶堆+小顶堆的方法应该是最高效的
main.cpp的代码, 我的“heap.h”用到的是这里的 http://blog.csdn.net/neostar2008/article/details/7769058

#include <iostream>#include <vector>#include <fstream>#include "heap.h"using namespace std;int main() {ifstream fin("Median.txt");MaxHeap maxh;MinHeap minh;int medianSum = 0;int temp;while(fin>>temp) {if(temp < maxh.top()) maxh.insert(temp);else minh.insert(temp);if(maxh.heapSize() - minh.heapSize() > 1) {minh.insert(maxh.extractMax());}if(minh.heapSize() > maxh.heapSize()) {maxh.insert(minh.extractMin());}medianSum += maxh.top();}cout<<medianSum % 10000<<endl;return 0;}





原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 和大学室友相处不愉快怎么办 和舍友三观不合怎么办 室友欠200钱忘了怎么办 和舍友闹矛盾了怎么办 被宿舍人孤立了怎么办 分到一个爱管事的舍友怎么办 添加了村人名录怎么办 打csgo出现卡顿怎么办 csgo创意工坊下载慢怎么办 电脑全是英文死机了怎么办 亿盛国际出金后不到账怎么办 玩游戏的界面很大怎么办电脑 文件缺失导致电脑无法启动怎么办 眼睛里拉出白丝怎么办 脚扭了脚面肿了怎么办 伪音唱歌嗓子疼怎么办 小孩吊水手肿了怎么办 棉质地的裙子起褶子怎么办 孕妇吃了马苋菜怎么办 三非黑人抓住了怎么办 33岁了写字好丑怎么办 裤子熨焦了发亮怎么办 黑裤子熨亮了怎么办 。P手机没有钤声怎么办 处处被小人其欠负怎么办??? 衣服开了一个口怎么办 脸上长痘痘留下的坑怎么办 脸部被打得皮肤怎么办 打脸引起耳朵疼怎么办 被打了耳痛耳鸣怎么办 苹果6视频锁屏怎么办 抠耳朵抠疼了怎么办 图库的相片没了怎么办 遇到打假牌的人怎么办 部队保障卡丢了怎么办 廊坊武警学院取消现役学员怎么办 孩子去当兵联系不上怎么办 军训戴眼镜晒痕怎么办 想进部队体检没过怎么办 大腿跟小腿不直怎么办 腿被车门夹了怎么办