4093:倒排索引查询--bitmap并查集

总时间限制: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”。
样例输入

3
3 1 2 3
1 2
1 3
3
1 1 1
1 -1 0
1 -1 -1

样例输出

NOT FOUND
1 3
1

思路 1
对应每个单词 维护一个文章set集合

n = int(input())
info = dict((i,set()) for i in range(n))
for wordId, articles in info.items():
    articles.update(input().split()[1:])

m = int(input())
for _ in range(m):
    ans = None
    for wordId, v in enumerate(input().split()):
        if v == '1':
            ans = ans.intersection(info[wordId]) if ans is not None else info[wordId]
        elif v == '-1' and ans:
            ans = [articleIndex for articleIndex in ans if articleIndex not in info[wordId]]
    if not ans:
        print("NOT FOUND")
    else:
        ans = list(ans)
        ans.sort()
        print(" ".join(ans))

思路二 借用bitmap思想
假定最多10000篇文章
对于第i个word 维护一个 10000个bit位Ai 表示8000篇文章 1–文章包含单词 0 –不包含

算法
查 包含 第i 个单词 和 第j 个单词的集合
A = Ai&Aj
查 包含 第i个单词 但 不包含第j 个单词的集合
A = Ai&(~Aj)

解决问题

  1. 10000个bit 和 文章编号articleId 对应关系;

    用int map[10000]来存储 map[i]=articleId ;

  2. 位图的存储;

    char bit[N][10000/8]; // 第i个word的位图 用10000/8个字节表示 即bit[i];

  3. 位图操作;
    1、第i个单词 出现在文档map[k]=articleId中, 对应位=1
    *((int *)(bit[i])+k/INTBL) |= 1<<(k%INTBL); // eg k=32 32bit个一组 内存分布 00…00 00..01 00..00
    2、&操作 以4个字节=1个int型为基本单位进行求&;
    for(k=0; k<10000/32; k++)
    ((int *)(bit[i])+k) & ((int *)(bit[j])+k)

代码如下 内存 1632kB 时间 51ms

#include <stdio.h>
#include <stdlib.h>

#define BYTEL (sizeof(byte))
#define INTL (sizeof(int))
#define INTBL (INTL*BYTEL)

#define N 100
#define AN (INTBL*1000) // 文章数量 INTBL的倍数 方便后续处理


char bit[N][AN/BYTEL];
int map[AN];    // 存储文章编号-索引映射; 把文章编号映射为0-AN的索引编号
int ansOrd[AN]; // 有序答案

/*
* 打印 地址p 长度为length*8 的内存中的 bit值
*/
int pp(char *p, int length)
{
    printf("%d ", p);
    int i,j, *q=(int *)p;
    for(i=0; i<length/INTL; i++, q++)
    {
        for(j=INTBL-1;j>=0;j--)
        {
            if( (*q) & (1<<j) )
                printf("1");
            else
                printf("0");
        }
        printf(" ");
    }
    printf("\n");
}

// 把文章编存放到map中 保证map的长度>=文章数
int getId(int n)
{
    int i=0, begin=0;
    begin = n%AN;
    for(i=begin; map[i]!=-1 && map[i]!=n; )
    {
        i = (i+1)%AN;
        if( i==begin ) return -1;
    }
    map[i] = n; 
    return i;   //返回对应的索引
}


int test4193()
{
    int i, j, k, v, u, n, m;
    int *p, *q;
    char ans[AN/BYTEL];
    memset(map, -1, sizeof(map));

    // 读数据
    i = 0;
    scanf("%d", &n);
    while( i<n )
    {
        scanf("%d", &c);
        while( c-- )
        {
            scanf("%d", &k);
            k = getId(k); //找到文档对应的map索引

            // 第i个单词 出现在文档索引k中
            *((int *)(bit+i)+k/INTBL) |= 1<<(k%INTBL);
        }
        // pp(bit+i, AN/BYTEL);
        i++;
    }

    // 判断
    scanf("%d", &m);
    while(m--)
    {
        memset(ans, -1, sizeof(ans)); //默认都符合条件

        // 对每一个单词遍历
        for(i=0; i<n; i++)
        {
            scanf("%d", &k);

            // 包含; 并操作
            if(k==1) for(j=0; j<AN/INTBL; j++)
            {
                *((int *)ans+j) &= *((int *)(bit+i)+j); // ans &= bit[i]
            }

            // 不包含; 取反后 求并集
            else if(k==-1) for(j=0; j<AN/INTBL; j++)
            {
                *((int *)ans+j) &= ~ *((int *)(bit+i)+j); // ans &= ~ bit[i]
            }
        }

        // 统计答案
        u=-1;
        // pp((char*)ans, sizeof(ans)); //显示内存bit值
        for(i=0, p=(int *)ans; i<AN/INTBL; i++, p++)
        {
            for(j=0;j<INTBL;j++) //对每一位遍历
            {
                // bit=1时 把对应的文章编号有序插入ansOrd中
                if( (*p) & (1<<j) )
                {
                    v=map[i*INTBL+j];
                    k=u;
                    while( k>=0 && ansOrd[k] > v )
                    {
                        ansOrd[k+1] = ansOrd[k];
                        k--;
                    }
                    ansOrd[k+1]=v;
                    u++;
                }
            }
        }

        // 打印答案
        if(u==-1)
        {
            printf("NOT FOUND\n");
        }
        else
        {
            printf("%d", ansOrd[0]);
            for(k=1; k<=u; k++)
                printf(" %d", ansOrd[k]);
            printf("\n");
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Delphi-OpenCV 库在 Delphi XE 将 IplImage 对象转换为Bitmap 对象的详细代码: ```delphiuses OpenCV_Core, OpenCV_ImageProc, // Delphi-OpenCV 库单元 Vcl.Graphics; // VCL 图形单元 function IplImageToBitmap(const Image: pIplImage): TBitmap; var Depth, Channels: Integer; LineSize: Integer; ImageData, SrcLine, DestLine: Pointer; Bitmap: TBitmap; Row, Col: Integer; Data: Byte; begin Depth := Image.depth; Channels := Image.nChannels; LineSize := Image.width * Channels; // 分配 Bitmap 对象 Bitmap := TBitmap.Create; Bitmap.PixelFormat := pf24bit; Bitmap.Width := Image.width; Bitmap.Height := Image.height; // 按行遍历 IplImage 数据并转换为 TBitmap 数据 ImageData := Image.imageData; for Row := 0 to Image.height - 1 do begin SrcLine := ImageData + Row * Image.widthStep; DestLine := Bitmap.ScanLine[Row]; case Depth of IPL_DEPTH_8U: begin for Col := 0 to Image.width - 1 do begin Data := PByte(SrcLine + Col * Channels)^; PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_8S: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PShortInt(SrcLine + Col * Channels)^); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_16U: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PWord(SrcLine + Col * Channels)^ shr 8); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_16S: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PShortInt(SrcLine + Col * Channels)^ shr 8 + 128); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_32S: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PInteger(SrcLine + Col * Channels)^ shr 24); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_32F: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PSingle(SrcLine + Col * Channels)^ * 255); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; IPL_DEPTH_64F: begin for Col := 0 to Image.width - 1 do begin Data := Byte(PDouble(SrcLine + Col * Channels)^ * 255); PByte(DestLine + Col * 3)^ := Data; PByte(DestLine + Col * 3 + 1)^ := Data; PByte(DestLine + Col * 3 + 2)^ := Data; end; end; end; end; Result := Bitmap; end; ``` 使用方法: ```delphi var Image: pIplImage; Bitmap: TBitmap; begin // 加载图像到 Image 变量 Image := cvLoadImage('image.jpg'); // 将 IplImage 对象转换为 TBitmap 对象 Bitmap := IplImageToBitmap(Image); // 将 TBitmap 对象显示在 TImage 组件上 Image1.Picture.Assign(Bitmap); // 释放 IplImage 对象内存 cvReleaseImage(@Image); end; ``` 需要注意的是,由于 Delphi-OpenCV 库的 IplImage 对象是指针类型的,因此需要传入指针的指针作为参数。在使用完毕后,需要手动释放 IplImage 对象的内存。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值