PAT5-08. 迷你搜索引擎

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<unordered_map>
#include<bitset>
using namespace std;
string line[10000];
unordered_map<string,bitset<100> >infile;
string filename[100];
unordered_map<string,bitset<10000> >in_line;

void readfile(int fileth){
  char pool[200];gets(pool);
  filename[fileth]=pool;
  // handle lines in the file
  for(int lineth=0;gets(pool),pool[0]!='#';++lineth){
    string _line(pool);
    line[fileth*100+lineth]=_line;
    int wordcnt=count(_line.begin(),_line.end(),' ')+1;
    istringstream iss(_line);
    // handle a word in the line
    while(wordcnt--){
      string _word;iss>>_word;
      for(auto&x:_word)x=tolower(x); 
            infile[_word].set(fileth);
      in_line[_word].set(fileth*100+lineth);
    }//while(wordcnt--)
  }//for(int lineth...)
}//readfile

void check(){
  char pool[200];gets(pool);string _line(pool);
  for(auto&x:_line)x=tolower(x);
  int wordcnt=count(_line.begin(),_line.end(),' ')+1;
  vector<string> words(wordcnt);
  istringstream iss(_line);
  for(auto &x:words)iss>>x;
  
  bitset<100>_infile;_infile.set();
  for(auto &x:words)_infile&=infile[x];

  int matched_cnt=_infile.count();
  printf("%d\n",matched_cnt);
  if(matched_cnt==0){
    printf("Not Found\n");
  }else{
    bitset<10000>_in_line;
    for(auto &x:words)_in_line|=in_line[x];
    for(int fileth=0;fileth<100;++fileth)
      if(_infile[fileth])
	{
	  printf("%s\n",filename[fileth].c_str());
	  for(int lineth=0;lineth<100;++lineth)
	    if(_in_line[fileth*100+lineth])
	      printf("%s\n",line[fileth*100+lineth].c_str());
	}//if(_infile...)
  }//else
  
  
}
int main(){
  int n;
  scanf("%d",&n);getchar();
  for(int i=0;i<n;++i)  readfile(i);
  scanf("%d",&n);getchar();
  while(n--)check();
  return 0;
}
用bitset保存状态来解决,调试时发生过两个错误,一个是“variable  has initializer but incomplete type”,意思是有声明无定义,查了一下发现相应的头文件忘了加。另一个是“declaration does not declare anything”,原来是inline关键字冲突,所以应该是in_line[ ]而不是inline[ ]



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值