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 ...
随机推荐
- Android 调用相册 拍照 实现系统控件缩放 切割图片
android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家. package cn.m15.test; import java.io.B ...
- CSS通用编码规范
CSS通用编码规范 总结一部分前端编码规范,CSS部分先奉上,大多比较通用,应该是主流方式吧. 1 前言 本文档的目标是使 CSS 代码在团队中风格保持一致,容易被理解和被维护. 尽管本文档是针对 C ...
- (转)原子操作 Interlocked系列函数
上一篇<多线程第一次亲密接触 CreateThread与_beginthreadex本质区别>中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是 ...
- BZOJ 1221: [HNOI2001] 软件开发(最小费用最大流)
不知道为什么这么慢.... 费用流,拆点.... --------------------------------------------------------------------------- ...
- Android 手机上安装并运行 Ubuntu 12.04
ubuntu.sh脚本的原地址变动了,导致下载不了,现在更新了网盘地址.小技巧:遇到一些下载失效的时候可以试一试p2p下载工具(如 easyMule.迅雷等)试一试,说不定有人分享过~* —————— ...
- Strut2中的session和servlet中的session的区别
在jsp中,内通过内置对象 HttpServletRequest的getSession()方法可以获取到HttpSession,比如: <%@ page language="java& ...
- ORACLE备份手记
嘛的,最近一直写EPOLL的游戏服务端搞的头晕,BOSS说了要备份ORACLE,由于DBA离职了,搞这个事情搞的很蛋疼,关掉实例后备份数据库各种连接不到实例,本来今晚要完成泡泡堂游戏的DX版的,郁闷 ...
- contains 和 ele.compareDocumentPosition确定html节点间的关系
~~~ nodeA.contains(nodeB) //ie , nodeA.compareDocumentPosition(nodeB) //firefox opera 1.DOMElement ...
- php订单生成唯一Id
一般用到一个函数: uniqid(prefix,more_entropy) 参数 描述 prefix 可选.为 ID 规定前缀.如果两个脚本恰好在相同的微秒生成 ID,该参数很有用. more_ent ...
- Java学习之DAO设计模式
DAO设计模式是一个javaEE里的设计模式,DAO是Data Access Object 数据访问接口. 一个典型的DAO实现有三个组件: 1.一个DAO接口 2.一个DAO接口的具体类: 3.数据 ...