#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[ ]