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 ...
随机推荐
- 深入理解 IE haslayout
转载自Bubblings Blog 原文地址:http://riny.net/2013/haslayout/ 1.什么是haslayout layout是windows IE的一个私有概念,它决定了元 ...
- 浅谈Struts2(四)
一.Struts2的拦截器(Intercept) 作用:把多个Action中的共有代码,提取至拦截器,从而减少Action中的冗余代码. 1.Action拦截器 a.编写interceptor类 pu ...
- [Swust OJ 402]--皇宫看守(树形dp)
题目链接:http://acm.swust.edu.cn/problem/402/ Time limit(ms): 5000 Memory limit(kb): 65535 Description ...
- JavaEE学习之类加载器
类装载子系统 在JAVA虚拟机中,负责查找并装载类型的那部分被称为类装载子系统. JAVA虚拟机有两种类装载器:启动类装载器和用户自定义类装载器.前者是JAVA虚拟机实现的一部分,后者则是Java程序 ...
- 关于playframework2.5
加入了很多新东西: 1.用akka streams 替换了大部分 iteratee-based async io,当然还有一些模块在用iteratees 2.java 的一些API 做了调整升级,以及 ...
- RAC ORA-12170 ora-12535/tns-12535
现象:开发人员抱怨RAC数据库出现了时连得上时连不上的情况,用SQLPLUS一试,果然有这样的情况: SQL> conn system/*******@bjyd 已连接. SQL> con ...
- Python 第三篇(上):python文件基础操作、json模块、lambda、map、filter、reduce和函数位置参数
python一切皆对象,linux一切皆文件,python操作文件是很常见的O/I操作,其内置来open()函数可以完成文件的基本操作: 一:使用内置open()函数操作文件,基本语法如下: with ...
- GoWithTheFlow
GoWithTheFlow http://notes.jetienne.com/2011/07/17/gowiththeflow.js-async-flow-control-with-a-zen-to ...
- 【Kill】两条Linux命令彻底杀死Oracle
今天编写的两条极具杀伤力的命令,它可以瞬间将Oracle杀死在无形之中.后面我将给出简单注释并展示一下它的威力.$ ps -ef |grep $ORACLE_SID|grep -v grep|awk ...
- ie浏览器css中的行为expression详解
CSS中的行为——expression (ie only) 最近对CSS中的行为比较感兴趣,虽然是不符合标准的也只有ie才能识别,但是他确实给css的功能扩展了不少.下面是摘自互联网上的文字和例子,因 ...