POJ 2050 Searching the Web
题意简述:做一个极其简单的搜索系统,对以下四种输入进行分析与搜索:
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的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web
题意:不难理解,照搬题意的解法. 代码:(Accepted,0.190s) //UVa1597 - Searching the Web //#define _XIENAOBAN_ #include&l ...
- Searching the Web论文阅读
Searching the Web (Arvind Arasu etc.) 1. 概述 2000年,23%网页每天更新,.com域内网页40%每天更新.网页生存半衰期是10天.描述方法可用Pois ...
- uva 1597 Searching the Web
The word "search engine" may not be strange to you. Generally speaking, a search engine se ...
- Searching the Web UVA - 1597
The word "search engine" may not be strange to you. Generally speaking, a search engine ...
- 【习题 5-10 UVA-1597】Searching the Web
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map < string,vector < int > >mmap[100];来记录每一个数据段某个字符串 ...
- poj很好很有层次感(转)
OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...
- POJ题目分类推荐 (很好很有层次感)
著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...
- 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 ...
- 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 ...
随机推荐
- golang(5):编写WebSocket服务,client和html5调用
本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/46882777 转载请必须注明出处! 1.关于websocket HTML5定义了 ...
- Introduction to Probability (5) Continus random variable
CONTINUOUS RANDOM VARIABLES AND PDFS 连续的随机变量,顾名思义.就是随机变量的取值范围是连续的值,比如汽车的速度.气温.假设我们要利用这些參数来建模.那么就须要引 ...
- POJ2151-Check the difficulty of problems(概率DP)
Check the difficulty of problems Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4512 ...
- MSSQL常用函数大全
一.字符转换函数1.ASCII()返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用‘’括起来,但含其它字符的字符串必须用‘’括起来使用,否则会出错. 2.CHA ...
- .net-一般处理程序及生命周期
IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHandler):是 ...
- C++运算符重载为非成员函数
#include<iostream> using namespace std; class Complex{ public: Complex(double r=0.0,double i=0 ...
- 五子棋Web版的开发(一)---搭建IDEA SSH环境
最近公司都没啥事,我在完成了控制台版的本地五子棋后(github地址:https://github.com/lkq51/wuziqi_console2),想将他升级成为一个web版的五子棋游戏.因为公 ...
- 青蛙跳台阶问题——剑指offer
题目:一只青蛙一次可以跳上1级台阶,也可以跳上2级台阶,求该青蛙跳上一个n级台阶总共有多少中跳法. http://www.nowcoder.com/books/coding-interviews?pa ...
- C语言的指针变量
C语言的指针变量 在C语言中,变量是固定范围的存储空间,它存储的是赋给他的值, 比如: ; /* 这里是定义一个整型变量a,并把12这个值存储在a的地址空间上 这个地址空间是系统随机分配的,对用户是透 ...
- Linux Mysql Client 查询中文乱码
1.mysql client 端设置编码为utf8 set character_set_results=utf8; 2.连接linux的客户端的编码也要设置为utf8(比如xshell,putty等)