poj4093:倒排索引查詢

來源:互聯網
上載者:User

標籤:style   blog   color   os   資料   io   

總時間限制: 1000ms 記憶體限制: 131072kB描述現在已經對一些文檔求出了倒排索引,對於一些詞得出了這些詞在哪些文檔中出現的列表。要求對於倒排索引實現一些簡單的查詢,即查詢某些詞同時出現,或者有些詞出現有些詞不出現的文檔有哪些。輸入第一行包含一個數N,1 <= N <= 100,表示倒排索引表的數目。接下來N行,每行第一個數ci,表示這個詞出現在了多少個文檔中。接下來跟著ci個數,表示出現在的文檔編號,編號不一定有序。1 <= ci <= 1000,文檔編號為32位整數。接下來一行包含一個數M,1 <= M <= 100,表示查詢的數目。接下來M行每行N個數,每個數表示這個詞要不要出現,1表示出現,-1表示不出現,0表示無所謂。資料保證每行至少出現一個1。輸出共M行,每行對應一個查詢。輸出查詢到的文檔編號,按照編號升序輸出。如果查不到任何文檔,輸出"NOT FOUND"。範例輸入33 1 2 31 21 331 1 11 -1 01 -1 -1範例輸出NOT FOUND1 31

這題直接用stl的set集合可以解決,直接貼代碼,但是這題在剛開始的時候遇到了bug詳見下一篇隨筆。

#include <iostream>#include <stdio.h>#include <set>#include <algorithm>using namespace std;set<int> s[100];int n,m;int main(){    scanf("%d",&n);    for(int i = 0; i < n; i++)    {        int k;        scanf("%d",&k);        for(int j = 0; j < k; j++)        {            int x;            scanf("%d",&x);            s[i].insert(x);        }    }    scanf("%d",&m);    for(int i = 0; i < m; i++)    {        set<int> a,b;        bool flag  = true;        for(int j = 0; j < n; j++)        {            int f;            scanf("%d",&f);            if(f == 1&&flag)            {                flag = false;                set<int>::iterator itr1 = s[j].begin();                set<int>::iterator itr2 = s[j].end();                while(itr1 != itr2)                {                    a.insert(*itr1);                    itr1++;                }            }            else if(f == 1&& !flag )            {                set<int>::iterator itr1 = a.begin();                set<int>::iterator itr2 = a.end();                while(itr1 != itr2)                {                    if(s[j].find(*itr1) == s[j].end())                        //itr1 = a.erase(itr1);  這個地方有bug                        a.erase(itr1++);                    else                        itr1++;                }                            }            else if(f == -1)            {                set<int>::iterator itr1 = s[j].begin();                set<int>::iterator itr2 = s[j].end();                while(itr1 != itr2)                {                    b.insert(*itr1);                    itr1++;                }            }        }        set<int>::iterator itr1 = b.begin();        set<int>::iterator itr2 = b.end();        while(itr1 != itr2)        {            a.erase(*itr1);            itr1++;        }        if(a.empty())            printf("NOT FOUND\n");        else        {            set<int>::iterator itr = a.begin();            while(itr != a.end())            {                printf("%d ",*itr);                itr++;            }            printf("\n");        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.