题意简述:做一个极其简单的搜索系统,对以下四种输入进行分析与搜索:

    1. 只有一个单词:如 term, 只需找到含有这个单词的document,然后把这个document的含有这个单词term的那些行输出。

    2.term1 AND term2, 找到同时含有term1 和 term2 的document,然后把这个document的含有这个单词term1 或 term2 的那些行输出。

    3.term1 OR term2, 找到含有term1 或 term2 的document,然后把这个document的含有这个单词term1 或 term2 的那些行输出。

    4.NOT term,将不含有 term的document全部输出

思路简述:

    做一个set集合 Src, 记录了一个单词出现在哪些文件的哪些行;用一个map做映射,指出一个单词出现在哪些文件中,这样子,如果是两个单词,可以分别求出各个单词属于哪些文件,根据“AND”则进行set_intersection运算, 根据“OR”进行set_union运算。

/*
Poj 2050
Emerald
10 May 2015
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm> using namespace std; class Figure{ // meaning : a word can be found in the lineOrder'th line of the docOrder'th Doc
public:
int docOrder, lineOrder;
Figure() {}
Figure( int d, int l ) {
docOrder = d;
lineOrder = l;
}
}; bool operator < ( const Figure f1, const Figure f2 ) { // the comparisions used in set
if( f1.docOrder!=f2.docOrder ) {
return f1.docOrder < f2.docOrder;
} else {
return f1.lineOrder < f2.lineOrder;
}
} class Doc{ // meaning : the order of a Doc, and how many lines the Doc contains
public :
int docOrder, lineLimit;
Doc() {}
Doc( int d, int l ) {
this->docOrder = d;
this->lineLimit = l;
}
}; // set < Figure > src; // this defination defines an accurate instant
typedef set < Figure > Src; // a src contains the figures of a word
typedef set < int > docSrc;
map < string, docSrc > docMap; // referring to a word, wordLine get a line
map < string, Src > dict;
map < Figure, string > wordLine; // referring to a word, wordLine get a line
vector < Doc > docs; const string DOC_END = "**********";
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) void Standard( string &line ); // make words tolower and other chars to whitespace
void WordRecord( string &line, int docOrder, int lineOrder ); // transfer words into src
void PrintSrc( Src &src ); // print as the problem commands
void WordsToFigure( Src &src, docSrc &dsrc, string& w1, string& w2 ); // know the docs and words, get target figures
void NotWord( string& word, Src &src ); // not the word w int main() {
int allDocs, queries; // input docs
scanf( "%d", &allDocs );
cin.get();
for( int i=0; i<allDocs; i++ ) {
string line;
int lineCounter = 0;
while( getline( cin, line ) && line != DOC_END ) { // read until end
wordLine[ Figure( i, lineCounter ) ] = line;
Standard( line );
WordRecord( line, i, lineCounter ++ );
}
docs.push_back( Doc( i, lineCounter ) );
} // input queries
string command;
scanf( "%d", &queries );
cin.get();
while( queries -- ) {
getline( cin, command );
Standard( command );
Src src;
if( command.find_last_of( ' ' ) == string::npos ) { // no whitespace
Standard( command );
src = dict[ command ];
} else if( command.find_last_of( ' ' ) != command.find_first_of( ' ' ) ) { // if there're two different whitespaces
stringstream ss( command ); // xxx AND/OR xxx
string w1, w2, connected;
ss >> w1 >> connected >> w2;
docSrc dSrc1 = docMap[ w1 ];
docSrc dSrc2 = docMap[ w2 ]; docSrc dsrc;
if( connected == "and" ) {
set_intersection( ALL( dSrc1 ), ALL( dSrc2 ), INS( dsrc ) ); // intersection
} else {
set_union( ALL( dSrc1 ), ALL( dSrc2 ), INS( dsrc ) ); // union
} WordsToFigure( src, dsrc, w1, w2 );
} else { // only one whitespace -> Not xxx
stringstream ss( command );
string w1;
ss >> w1 >> w1;
NotWord( w1, src );
}
PrintSrc( src );
}
return 0;
} void Standard( string &line ) {
int length = line.length();
for( int i=0; i<length; i++ ) {
if( isalpha( line[i] ) ) {
line[i] = tolower( line[i] ); // tolower, such as 'A' to 'a'
} else {
line[i] = ' '; // if c isn't a alpha, c will be transferred to a whitespace
}
}
} void WordRecord( string &line, int docOrder, int lineOrder ) {
stringstream ss( line );
string word;
while( ss >> word ) {
if( dict.count( word ) ) { // whether the word has been found in the total input
if( !dict[word].count( Figure( docOrder, lineOrder ) ) ) { // whether the word has been found in this line
dict[word].insert( Figure( docOrder, lineOrder ) ) ;
}
} else {
Src src;
src.insert( Figure( docOrder, lineOrder ) );
dict[ word ] = src;
}
if( docMap.count( word ) ) { // whether the word has been found in this document
docMap[word].insert( docOrder );
} else {
docSrc ds;
docMap[word] = ds;
docMap[word].insert( docOrder );
}
}
} void PrintSrc( Src &src ) { // print the result
if( src.size() == 0 ) {
printf("Sorry, I found nothing.\n");
printf( "==========\n" );
return ;
}
Src :: iterator it = src.begin(), bef; // bef represents the former one
printf( "%s\n", wordLine[ *it ].c_str() );
bef = it++;
while( it != src.end() ) {
if( it->docOrder != bef->docOrder ) {
printf( "----------\n" );
}
printf( "%s\n", wordLine[ *it ].c_str() );
bef = it;
it ++;
}
printf( "==========\n" );
} void WordsToFigure( Src &src, docSrc &dsrc, string& w1, string& w2 ) {
docSrc :: iterator it;
for( it=dsrc.begin(); it != dsrc.end(); it ++ ) {
Src :: iterator it2 ;
for( it2 = dict[ w1 ].begin(); it2 !=dict[w1].end(); it2 ++ ) {
if( *it == it2 -> docOrder ) {
src.insert( *it2 ); // the w1 appears in this line of this document
}
}
for( it2 = dict[ w2 ].begin(); it2 !=dict[w2].end(); it2 ++ ) {
if( *it == it2 -> docOrder ) {
src.insert( *it2 ); // the w1 appears in this line of this document
}
}
}
} void NotWord( string& word, Src &src ) { // not this word
docSrc dsrc = docMap[ word ];
vector< Doc > :: iterator it;
for( it = docs.begin(); it != docs.end(); it ++ ) {
if( !dsrc.count( it->docOrder ) ) {
for( int i=0; i< it->lineLimit; i ++ ) {
src.insert( Figure( it->docOrder, i ) );
}
}
}
}

POJ 2050 Searching the Web的更多相关文章

  1. [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web

    题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...

  2. Searching the Web论文阅读

    Searching the Web   (Arvind Arasu etc.) 1. 概述 2000年,23%网页每天更新,.com域内网页40%每天更新.网页生存半衰期是10天.描述方法可用Pois ...

  3. uva 1597 Searching the Web

    The word "search engine" may not be strange to you. Generally speaking, a search engine se ...

  4. Searching the Web UVA - 1597

      The word "search engine" may not be strange to you. Generally speaking, a search engine ...

  5. 【习题 5-10 UVA-1597】Searching the Web

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map < string,vector < int > >mmap[100];来记录每一个数据段某个字符串 ...

  6. poj很好很有层次感(转)

    OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...

  7. POJ题目分类推荐 (很好很有层次感)

    著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...

  8. Multiple actions were found that match the request in Web Api

    https://stackoverflow.com/questions/14534167/multiple-actions-were-found-that-match-the-request-in-w ...

  9. zz A list of open source C++ libraries

    A list of open source C++ libraries < cpp‎ | links http://en.cppreference.com/w/cpp/links/libs Th ...

随机推荐

  1. HTTP初步注解

    搜集了一下网上的资源和自己看过的一些书,小小总结了一波HTTP,现在也只是很肤浅的了解,期望以后深入理解后能写出更有营养的笔记. HTTP协议的主要特点 + 支持客户/服务器模式.+ 简单快速:客户向 ...

  2. redis在java项目中的使用

    在上一篇文章中已经讲了redis的spring配置,这篇将会描述redis在java项目中的使用. redis存储形式都是key-value(键值对),按照存储的内容分为两种,一种是存简单数据,即数字 ...

  3. awk的用法(转)

    awk 用法:awk ' pattern {action} ' 变量名 含义 ARGC 命令行变元个数 ARGV 命令行变元数组 FILENAME 当前输入文件名 FNR 当前文件中的记录号 FS 输 ...

  4. protel99se中做拼板图解

    很多时候我们要在protel99se中做拼板, 但是通常在复制进行拼版的时候会出现如下的效果,元件被重新命名了. 而无法达到我们需要的像下图的效果 那我们怎么办,才能达到上图的效果呢?其实操作很简单. ...

  5. ThinkPHP 3.1.2 查询方式的一般使用2

    //select id1> and id2< 默认是and $data['id']=array(array('gt',$id1),array('lt',$id2)); // $data[' ...

  6. C++中struct和class的总结

    一.在语法上的一些区别 由于C++是从C发展而来,C++中的struct更多的是去做了兼容的C的部分.在语法层面他们有以下的区别: 1. struct中所有的成员是是public,也就是说你可以对一个 ...

  7. IPTABLES 映射问题

    今天要做一个新的映射:将内网的一个8090口映射到外网的8087口. 在 /ETC/RC.LOCAL中最后插入: iptables -t nat -A PREROUTING -d outIP -p t ...

  8. 发一个讨论帖,如果结果被采纳的话可以给一份adb 代码,以及我封装的ADBLIB

    如何在手机没有root 的情况下,获取系统的一些文件,比如 /data/data/xxxx 目录下的文件. 有任何想法的请说出来.

  9. HDU 2527

    题目描述          HDU 2527 分析         霍夫曼编码的应用.         本题没有必要构造一棵完整的霍夫曼树.只需利用霍夫曼编码的原理,每次挑选频率最低的两个元素进行合并 ...

  10. 详解iOS开发之自定义View

    iOS开发之自定义View是本文要将介绍的内容,iOS SDK中的View是UIView,我们可以很方便的自定义一个View.创建一个 Window-based Application程序,在其中添加 ...