您的位置:首页 > 理论基础 > 数据结构算法

数据结构:倒排索引

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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: