UVA 1597 Search the Web
来源:互联网 发布:天人网络电视官网 编辑:程序博客网 时间:2024/06/02 19:36
这个题就是考察STL容器的综合运用,由于文章比较长并且请求很多,直接暴力扫肯定会超时,所以需要用map作个映射。
我的思路:1.用一个map<int,map<string,set<int>>>,第一个int代表第几篇文章,第二个map把单词及其行数对应起来,因为一个单词可能在同一行中出现两次,所以用了 一 个 set来存单词的行位置.
因为要输出相应的文本,所以用一个web数组存下所有的输入,行数用line计算。
lines存储每篇文章的起始行和终止行。
2.由于要查询的关键词都是由字母组成的,并且忽略大小写,所以可以把输入的字符串处理一下,把字母都变成小写,非字母变成空格,再用stringstream读取。
#include<iostream>#include<string>#include<cstring>#include<vector>#include<map>#include<sstream>#include<cstdio>#include<set>using namespace std;map<int,map<string,set<int> > >Search; //int表示第几篇文章,第二个map中的string代表文章中的单词,最里面的int代表行数vector<string>web; //存放所有文本vector<pair<int,int> >lines;string trans(string &s){ int len = s.length(); for(int i = 0; i < len; i++) if(!isalpha(s[i])) s[i] = ' '; else s[i] = tolower(s[i]); return s;}int main(){ int i,j,k,line = 1; int n; cin >> n; pair<int,int>ha; getchar(); string s1,s2; for(i = 1; i <= n; i++){ ha.first = line-1; while(1){ getline(cin,s1); web.push_back(s1); ++line; if(s1[0] == '*') {ha.second = line - 3; lines.push_back(ha); break;} s2 = trans(s1); stringstream ss(s2); while(ss >> s1) { if(!Search[i].count(s1)) Search[i][s1] = set<int>(); Search[i][s1].insert(line-2); } } } int m; cin >> m; getchar(); for(i = 1; i <= m; i++){ string s; getline(cin,s); int no = 1,first = 1; int k1 = s.find(" AND "),k2 = s.find(" OR "),k3 = s.find("NOT "); if(k1 != -1 || k2 != -1){ s1 = s.substr(0,(k1 > 0 ? k1 : k2)); s2 = s.substr(k1 > 0 ? k1+5 : k2+4); set<int>cnt; for(j = 1; j <= n; j++){ cnt.clear(); int x = 0, y = 0; if(Search[j].count(s1)){ for(set<int>::iterator it = Search[j][s1].begin(); it != Search[j][s1].end(); ++it) cnt.insert(*it); x = 1; } if(Search[j].count(s2)){ for(set<int>::iterator it = Search[j][s2].begin(); it != Search[j][s2].end(); ++it) cnt.insert(*it); y = 1; } if((k1 != -1 && x && y) || (k2 != -1 && (x || y))){ if(!first) cout << "----------\n";first = no = 0; for(set<int>::iterator it = cnt.begin(); it != cnt.end(); ++it) cout << web[*it] << endl; } } } else if(k3 != -1){ s1 = s.substr(4); for(j = 1; j <= n; j++) if(!Search[j].count(s1)){ if(!first) cout << "----------\n"; for(k = lines[j-1].first; k <= lines[j-1].second; k++) cout << web[k] << endl; first = no = 0; } } else{ for(j = 1; j <= n; j++){ if(Search[j].count(s)){ if(!first) cout << "----------\n"; no = first = 0; for(set<int>::iterator it = Search[j][s].begin(); it != Search[j][s].end(); ++it) cout << web[*it] << endl; } } } if(no)cout << "Sorry, I found nothing.\n"; cout << "==========\n"; } return 0;}
0 0
- UVA 1597 Search the Web
- UVa 1597-Searching the Web
- UVa 1597 - Searching the Web
- UVa:1597 - Searching the Web
- Uva - 1597 - Searching the Web
- UVa 1597 - Searching the Web(模拟)
- uva 1597 Searching the Web (wrong)
- 5-10 UVA 1597 Searching the Web在Web中搜索
- The Anatomy of a Large-Scale Hypertextual Web Search Engine
- The Anatomy of a Large-Scale Hypertextual Web Search Engine
- A web search analysis considering the intention behind queries
- Determining the Unser Intent of Web Search Engine Queries
- Data Mining, Search, and the World Wide Web
- The Anatomy of a Large-Scale Hypertextual Web Search Engine
- The Anatomy of a Large-Scale Hypertextual Web Search Engine
- The Anatomy of a Large-Scale Hypertextual Web Search Engine
- password search uva 902
- UVa:1554 Binary Search
- 2016蓝桥杯假期任务之《分糖果》
- 图像处理(一)图像变形(1)矩形全景图像还原-Siggraph 2014
- 2个更新UI的方法--activity.runOnUiThread及handler.post
- Android APP分享功能实现
- 求一个生成四位随机数的PHP代码
- UVA 1597 Search the Web
- MFC 基本操作 --修改对话框、静态文本背景颜色
- 构造数组结构
- 数据结构 、 多线程 系列文章
- 图像处理(二)Seam Carving算法-Siggraph 2007
- Linux内核工程导论——UIO
- C++中static用法详解
- 学习 ui-router - 路由控制
- [Java]单项链表与双端链表[原]