码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构:倒排索引

时间:2015-08-17 14:04:37      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:数据结构   索引   

#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[N];
        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;
}

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

数据结构:倒排索引

标签:数据结构   索引   

原文地址:http://blog.csdn.net/liuhuiyan_2014/article/details/47723749

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!