TopK代码
Hash表
#ifndef _HASH_H
#define _HASH_H #include<string.h>
#include<stdio.h>
class HashTable
{
public:
HashTable(unsigned int size);
~HashTable();
int get(const char *key,unsigned int *value);
int set(const char *key,unsigned int value);
int push(const char *key);
int MAXTopK(unsigned int k);
int MINTopK(unsigned int k);
private:
struct Node
{
char *key;
unsigned int value;
Node *next; Node(const char *str,unsigned int v)
{
key = new char[strlen(str)+];
strcpy(key,str);
value = v;
next = NULL;
}
~Node()
{
delete[] key;
}
};
int Init();
int Destroy();
int Hash(const char *key,unsigned int *hashcode);
int ClearChain(Node *Head); Node **TableHead;
unsigned int TableSize;
char **kWords;
};
#endif
#include"Hash.h"
#include"Heap.h" HashTable::HashTable(unsigned int size)
{
TableHead = NULL;
TableSize = size;
Init();
}
HashTable::~HashTable()
{
Destroy();
} int HashTable::Init()
{
if(TableHead != NULL)
{
printf("HashTable has been initialized\n");
return -;
}
TableHead = new Node*[TableSize];
for(unsigned int i=;i<TableSize;i++)
{
TableHead[i]=NULL;
}
return ;
} int HashTable::Destroy()
{
for(unsigned int i=;i<TableSize;i++)
{
if(ClearChain(TableHead[i]) < )
{
printf("ClearChain error\n");
return -;
}
}
delete[] TableHead;
TableHead = NULL;
return ;
} int HashTable::get(const char *key,unsigned int *value)
{
unsigned int hashcode=;
if(Hash(key,&hashcode) < )
{
printf("generate hashcode error");
return -;
}
unsigned int index = hashcode%TableSize;
Node *p = TableHead[index];
while(p!=NULL && (strcmp(key,p->key)!=))
{
p=p->next;
}
if(p!=NULL)
{
*value = p->value;
}
else
{
*value = ;
}
return ;
} int HashTable::set(const char *key,unsigned int value)
{
unsigned int hashcode=;
if(Hash(key,&hashcode) < )
{
printf("generate hashcode error");
return -;
}
unsigned int index = hashcode%TableSize;
Node *p = TableHead[index];
while(p!=NULL && (strcmp(key,p->key)!=))
{
p=p->next;
}
if(p!=NULL)
{
p->value = value;
}
else
{
Node *q = TableHead[index];
TableHead[index] = new Node(key,value);
TableHead[index]->next = q;
}
return ;
} int HashTable::push(const char *key)
{
unsigned int hashcode=;
if(Hash(key,&hashcode) < )
{
printf("generate hashcode error");
return -;
}
unsigned int index = hashcode%TableSize;
Node *p = TableHead[index];
while(p!=NULL && (strcmp(key,p->key)!=))
{
p=p->next;
}
if(p!=NULL)
{
p->value = p->value+;
}
else
{
Node *q = TableHead[index];
TableHead[index] = new Node(key,);
TableHead[index]->next = q;
}
return ;
} int HashTable::Hash(const char *str,unsigned int *hashcode)
{
*hashcode = ;
unsigned int hashseed = ;
while(*str != '\0')
{
*hashcode += *hashcode*hashseed + *str;
str++;
}
(*hashcode) & 0x7FFFFFFF;
return ;
} int HashTable::ClearChain(Node *Head)
{
Node *p=Head;
Node *q;
while(p != NULL)
{
q=p->next;
delete p;
p=q;
}
Head = NULL;
return ;
} int HashTable::MAXTopK(unsigned int k)
{
Pair *heap = new Pair[k];
for(unsigned int i=;i<TableSize;i++)
{
Node *p=TableHead[i];
while(p!=NULL)
{
if(p->value > heap[].cnt)
{
heap[]=Pair(p->key,p->value);
minHeapIFY(,heap,k);
}
p=p->next;
}
}
printf("MAX TopK:\n");
for(unsigned int j=;j<k;j++)
{
printf("%s:%d\n",heap[j].word,heap[j].cnt);
}
delete[] heap;
return ;
} int HashTable::MINTopK(unsigned int k)
{
Pair *heap = new Pair[k];
int s=k;
for(unsigned int i=;i<TableSize;i++)
{
Node *p=TableHead[i];
while(p!=NULL)
{
if(s>)
{
s--;
heap[s]=Pair(p->key,p->value);
}
if(s == )
{
s--;
buildMaxHeap(heap,k);
}
else
{
if(p->value < heap[].cnt)
{
heap[]=Pair(p->key,p->value);
maxHeapIFY(,heap,k);
}
}
p=p->next;
}
}
printf("MIN TopK:\n");
for(unsigned int j=;j<k;j++)
{
printf("%s:%d\n",heap[j].word,heap[j].cnt);
}
delete[] heap;
return ;
}
堆
#ifndef _HEAP_H
#define _HEAP_H
#include<string.h>
struct Pair
{
char *word;
unsigned int cnt;
Pair()
{
word = NULL;
cnt = ;
}
Pair(const char *str,unsigned int num)
{
word = new char[strlen(str)+];
strcpy(word,str);
cnt = num;
}
~Pair()
{
delete[] word;
word=NULL;
}
const Pair& operator=(const Pair& p)
{
delete[] word;
if(p.word != NULL)
{
word = new char[strlen(p.word)+];
strcpy(word,p.word);
}
else
{
word = NULL;
}
cnt = p.cnt;
return *this;
}
}; unsigned int Parent(unsigned int i);
unsigned int Left(unsigned int i);
unsigned int Right(unsigned int i); void maxHeapIFY(unsigned int i,Pair *p,unsigned int len);
void minHeapIFY(unsigned int i,Pair *p,unsigned int len); void buildMaxHeap(Pair *p,unsigned int len);
void bulidMinHeap(Pair *p,unsigned int len); #endif
#include"Heap.h" unsigned int Parent(unsigned int i)
{
return (i-)>>;
} unsigned int Left(unsigned int i)
{
return (i<<)+;
} unsigned int Right(unsigned int i)
{
return (i<<)+;
} void maxHeapIFY(unsigned int i,Pair *p,unsigned int len)
{
if(i>=len)
{
return;
}
unsigned int largest = i;
unsigned int leftidx = Left(i);
if(leftidx<len && p[i].cnt<p[leftidx].cnt)
{
largest = leftidx;
}
unsigned int rightidx = Right(i);
if(rightidx<len && p[largest].cnt<p[rightidx].cnt)
{
largest = rightidx;
}
if(largest != i)
{
Pair temp(p[i].word,p[i].cnt);
p[i] = p[largest];
p[largest]=temp;
maxHeapIFY(largest,p,len);
}
} void minHeapIFY(unsigned int i,Pair *p,unsigned int len)
{
if(i>=len)
{
return;
}
unsigned int smallest = i;
unsigned int leftidx = Left(i);
if(leftidx<len && p[i].cnt>p[leftidx].cnt)
{
smallest = leftidx;
}
unsigned int rightidx = Right(i);
if(rightidx<len && p[smallest].cnt>p[rightidx].cnt)
{
smallest = rightidx;
}
if(smallest != i)
{
Pair temp(p[i].word,p[i].cnt);
p[i] = p[smallest];
p[smallest]=temp;
maxHeapIFY(smallest,p,len);
}
} void buildMaxHeap(Pair *p,unsigned int len)
{
for(int i=len/-;i>=;i--)
{
maxHeapIFY(i,p,len);
}
} void buildMinHeap(Pair *p,unsigned int len)
{
for(int i=len/-;i>=;i--)
{
minHeapIFY(i,p,len);
}
}
主函数
#include<stdio.h>
#include"Hash.h" int main()
{
char *A[]={"hello","world","spicy","hot","delete","great","spicy","great","great","hello","hot","hello"};
unsigned int len=sizeof(A)/sizeof(char*);
HashTable oTable(len); for(unsigned int i=;i<len;i++)
{
if(oTable.push(A[i])<)
{
printf("push error\n");
return -;
}
} for(unsigned int i=;i<len;i++)
{
unsigned int cnt;
if(oTable.get(A[i],&cnt)<)
{
printf("get error\n");
return -;
}
printf("%s:%d\n",A[i],cnt);
} oTable.MAXTopK();
oTable.MINTopK(); return ;
}
TopK代码的更多相关文章
- TOPK 问题
TOPK 问题 描述 如从海量数字中寻找最大的 k 个,这类问题我们称为 TOPK 问题,通常使用堆来解决: 求前 k 大,用最小堆 求前 k 小,用最大堆 例子 现有列表 [1, 2, 0, 3, ...
- qsort代码(pascal/c/c++)与思想及扩展(随机化,TopK)
1.快速排序思想:从一堆数A中找到一个数x,然后把这堆数x分成两堆B,C,B堆的数小于(或小于等于)该数,放在左边,C堆的数大于(或大于等于)该数,放在右边,有可能把该数x单独分开,放在中间.然后对小 ...
- 从海量数据中寻找出topK的最优算法代码
package findMinNumIncludedTopN;/** * 小顶堆 * @author TongXueQiang * @date 2016/03/09 * @since JDK 1.8 ...
- [数据结构]——堆(Heap)、堆排序和TopK
堆(heap),是一种特殊的数据结构.之所以特殊,因为堆的形象化是一个棵完全二叉树,并且满足任意节点始终不大于(或者不小于)左右子节点(有别于二叉搜索树Binary Search Tree).其中,前 ...
- MapReduce实现TopK的示例
由于开始学习MapReduce编程已经有一段时间了,作为一个从编程中寻找自信和乐趣以及热爱编程的孩子来讲,手开始变得很“痒”了,很想小试一下身手.于是自己编写了TopK的代码.TopK的意思就是从原文 ...
- Topk引发的一些简单的思考
软件工程课程的一个题目:写一个程序,分析一个文本文件中各个词出现的频率,并且把频率最高的10个词打印出来.文本文件大约是30KB~300KB大小. 首先说一下这边的具体的实现都是在linux上实现的. ...
- Python使用heapq实现小顶堆(TopK大)、大顶堆(BtmK小)
Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) | 四号程序员 Python使用heapq实现小顶堆(TopK大).大顶堆(BtmK小) 4 Replies 需1求:给出N长 ...
- Mapreduce TopK
思想比较简单,就是每个通过map来获取当前的数据块中的的topk个数据,然后将他们以相同的key值放到reduce中,最后通过reduce来对这n*k个数据排序并获得topk个数据.具体的就是建立 ...
- scala写算法-用小根堆解决topK
topK问题是指从大量数据中获取最大(或最小)的k个数,比如从全校学生中寻找成绩最高的500名学生等等. 本问题可采用小根堆解决.思路是先把源数据中的前k个数放入堆中,然后构建堆,使其保持堆序(可以简 ...
随机推荐
- c# 图片资料
- [CodeForces] CF226D The table
Harry Potter has a difficult homework. Given a rectangular table, consisting of n × m cells. Each ce ...
- exgcd扩展欧几里得求解的个数
知识储备 扩展欧几里得定理 欧几里得定理 (未掌握的话请移步[扩展欧几里得]) 正题 设存在ax+by=gcd(a,b),求x,y.我们已经知道了用扩欧求解的方法是递归,终止条件是x==1,y==0: ...
- 优化JAVA查询Mongodb数量过大,查询熟读慢的方法
前言:2018年的时候优化了一个项目,该项目从MOngodb中获取数据的时候一次去十万百万千万的数据过慢,往往每次都要二十秒,三十秒,今天提出了一个代码优化的方案 项目查从mongodb中获取数据:代 ...
- String formatting in Python
| \n | 换行 || \t | 制表符 || \ | 转义 || \\ | \ | the '%' operator is used to format a set of va ...
- springcloud(五):Eureka提供数据的客户端连接Docker的mysql
一.提供数据的客户端需要连接数据了,因此需要我们使用mybatis了,等下使用idea生成mybaits和web的依赖 二.提供数据的客户端项目 1.创建项目 2.选择idea自动给我们生成的依赖 3 ...
- 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 5
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...
- 一个电商项目的Web服务化改造6:单元测试4步走,构造数据、执行操作、断言、回滚
最近一直在做一个电商项目,需要把原有单系统架构的项目,改造成基于服务的架构,SOA. 有点挑战,做完了,会有很大进步. 单元测试,在很早之前的文章已经介绍过. 可以在这里看到相关的 ...
- nagios监控ganglia指标,并配置告警
背景:nagios的监控插件在轻量级方面不如ganglia的gmond,且ganglia在指标监控方面尤其突出的优势,而nagios在配置告警方面也比较方便,正好可以弥补ganglia无法配置告警通知 ...
- Java基础学习总结(69)——匿名内部类与Lambda表达式
前言 Java Labmda表达式的一个重要用法是简化某些匿名内部类(Anonymous Classes)的写法.实际上Lambda表达式并不仅仅是匿名内部类的语法糖,JVM内部是通过invokedy ...