数据结构:倒排索引
2015-08-17 13:12
507 查看
#include <iostream> #include <map> #include <vector> #include <string.h> using namespace std; //倒排索引,以属性值来存储关键字。 //找出两个人相似度最高的人,相似度=(相同爱好数)/总的爱好数。 struct Node { string like; int count; Node() { count = 0; } map<string,bool> mp; }; template<int N> class MyClass { public: MyClass() { node = new Node ; index = 0; } ~MyClass() { if (node) delete[] node; } void Insert(char (*str)[10],int n) { int j = 0; string _name = str[j++]; for (; j < n; j++) { int i = 0; string _like = str[j]; while (node[i].count != 0 && (node[i].like != _like))i++; if (node[i].count == 0) { node[i].mp.insert(pair<string, bool>(_name, true)); node[i].like = _like; node[i].count++; } else { node[i].count++; node[i].mp.insert(pair<string, bool>(_name,true)); } } } void Printf() { int i = 0; for (; node[i].count!=0; i++) { cout << node[i].like.c_str() <<" : "; map<string,bool> :: iterator it = node[i].mp.begin(); while (it != node[i].mp.end()) { cout << (*it).first.c_str() << " "; it++; } cout << endl; } } private: int index; Node *node; }; int main() { MyClass<10> mc; char People1[][10] = { "小A", "自拍", "运动", "游戏" }; char People2[][10] = { "小B", "游戏", "听歌", "看电影" }; char People3[][10] = { "小C", "逛街", "听歌", "运动"}; char People4[][10] = {"小D","游戏","听歌","看电影","爬山"}; mc.Insert(People1,4); mc.Insert(People2,4); mc.Insert(People3,4); mc.Insert(People4,5); mc.Printf(); return 0; }
相关文章推荐
- Treap 模板
- 数据结构实验之栈二:一般算术表达式转换成后缀式 SDUT 2132
- 数据结构 二路归并排序的实现
- POJ 3750 小孩报数问题 数据结构水
- 《数据结构与算法分析——c语言描述》读后笔记 7
- 使用逆波兰式(后缀表达式)实现多功能计算器
- 数据结构:快状链表(数组链表联合)
- 数据结构 字符串二分查找正确实现
- 数据结构-二叉搜索树(Binary Search Tree)的C++实现模板
- 数据结构之二叉树的遍历
- 数据结构 堆排序的实现
- 数据结构 二叉树遍历之前序、中序、后序以及层次遍历实现
- 数据结构 基数排序的实现
- 数据结构-树的存储结构
- 自己动手实现数据结构——排序算法2 (希尔、快速、堆)(C++实现)
- 数据结构-树
- 数据结构-队列2
- 数据结构-队列1
- 【四】nginx的数据结构(2)——自己动手重写红黑树
- 数据结构和算法系列15 线索二叉树