【习题 5-10 UVA-1597】Searching the Web
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
用map >mmap[100];来记录每一个数据段某个字符串出现的行数,以及用来判断这个字符串在这一段中存不存在。
**
->这里有一个地方要注意,千万不要在未确定这个字符串是否存在之前,调用mmap[i][s],因为这样,不管s存不存在,s都会加那么一个键值。
->而这就使得我们不能用更快的mmap[i].find(s)函数来寻找某个字符串在不在了.
->用mmap[i][s]访问,然后判断在不在的方式是会TLE的。
**
样例有9个'-'但实际上输出10个'-';
然后就是各个数据段之间的分隔符的输出。
or和and的输出都要用set来重新排序。
即从小的行开始到大的行依序输出。
【代码】
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
int n, m;
vector <string> v[N + 10];
set <int> vv;
map <string, vector<int> > mmap[N + 10];
string s, ts;
bool have(int idx, string s)
{
if (mmap[idx].find(s)==mmap[idx].end())
return false;
else
return true;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
ios::sync_with_stdio(0), cin.tie(0);
cin >> n; cin.get();
for (int ii = 1; ii <= n; ii++)
{
int tot = 0;
while (getline(cin, s))
{
if (s == "**********") break;
v[ii].push_back(s);
int len = s.size();
for (int i = 0; i < len; i++)
if (isalpha(s[i]))
s[i] = tolower(s[i]);
else
s[i] = ' ';
stringstream temp(s);
string x;
while (temp >> x)
{
mmap[ii][x].push_back(tot);
}
tot++;
}
}
cin >> m; cin.get();
for (int i = 0; i < m; i++)
{
getline(cin, ts);
int fi = ts.find(' ', 0);
if (fi == -1)//find x
{
bool ok = false;
for (int j = 1; j <= n; j++)
if (have(j, ts))
{
if (ok) cout << "----------" << endl;
ok = true;
vv.clear();
for (int x : mmap[j][ts]) vv.insert(x);
for (int x : vv) cout << v[j][x] << endl;
}
if (!ok) cout << "Sorry, I found nothing." << endl;
cout << "==========" << endl;
}
else
{
int fi2 = ts.find(' ', fi + 1);
if (fi2 == -1)//not x
{
bool ok = false;
ts = ts.substr(fi);
while (ts[0] == ' ') ts.erase(0, 1);
for (int j = 1; j <= n; j++)
if (!have(j, ts))
{
if (ok) cout << "----------" << endl;
ok = true;
int lenv = v[j].size();
for (int k = 0; k < lenv; k++)
cout << v[j][k] << endl;
}
if (!ok) cout << "Sorry, I found nothing." << endl;
cout << "==========" << endl;
}
else // x y z
{
stringstream ss(ts);
string x, y, z;
ss >> x; ss >> y; ss >> z;
if (y == "OR")
{
bool ok = false;
for (int j = 1; j <= n; j++)
if (have(j, x) || have(j, z))
{
if (ok) cout << "----------" << endl;
ok = true;
vv.clear();
if (have(j,x))for (int t : mmap[j][x]) vv.insert(t);
if (have(j,z))for (int t : mmap[j][z]) vv.insert(t);
for (int t : vv) cout << v[j][t] << endl;
}
if (!ok) cout << "Sorry, I found nothing." << endl;
cout << "==========" << endl;
}
else
{
bool ok = false;
for (int j = 1; j <= n; j++)
if (have(j, x) && have(j, z))
{
if (ok) cout << "----------" << endl;
ok = true;
vv.clear();
for (int t : mmap[j][x]) vv.insert(t);
for (int t : mmap[j][z]) vv.insert(t);
for (int t : vv) cout << v[j][t] << endl;
}
if (!ok) cout << "Sorry, I found nothing." << endl;
cout << "==========" << endl;
}
}
}
}
return 0;
}
【习题 5-10 UVA-1597】Searching the Web的更多相关文章
- uva 1597 Searching the Web
The word "search engine" may not be strange to you. Generally speaking, a search engine se ...
- [刷题]算法竞赛入门经典(第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 ...
- STL --- UVA 123 Searching Quickly
UVA - 123 Searching Quickly Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...
- ArcGIS Portal 10.4 本地坐标系的web 3d地形展示制作说明
原文:ArcGIS Portal 10.4 本地坐标系的web 3d地形展示制作说明 ArcGIS Portal 10.4 本地坐标系的web 3d地形展示制作说明 By 李远祥 ArcGIS Por ...
- 10 个基于 jQuery 的 Web 交互插件推荐
英文原文:10 jQuery for Web Interaction Plugins “用户交互”在现代的 Web 设计中占据了很大比例,这是互联网产品不可或缺的关键,对 Web 设计师也提出了更高的 ...
- 10个优秀的移动Web应用开发框架
在最近几年里,移动互联网高速发展.市场潜力巨大.继计算机.互联网之后,移动互联网正掀起第三次信息技术革命的浪潮,新技术.新应用不断涌现.今天这篇文章向大家推荐10大优秀的移动Web开发框架,帮助开发者 ...
- Searching the Web UVA - 1597
The word "search engine" may not be strange to you. Generally speaking, a search engine ...
- Nginx 1.10.2 发布,高性能 Web 服务器
Nginx 1.10.2 发布了.Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器 更新内容: Changes with n ...
随机推荐
- TensorFlow的学习
1.先判断python的版本(因为有些python版本自带pip,可以参考我写的对pip的认识的博客文章),选择是否安装pip,然后安装更新tensorflow如:sudo pip install - ...
- 关于 js 的框架方向
关于 js 的框架方向 http://www.breck-mckye.com/blog/2014/12/the-state-of-javascript-in-2015/?utm_source=ourj ...
- Java中Thread源码剖析
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 关于线程,用很长时间了,主线程下的子线程去做一些事情,就是一个代理模式,主线程分代理权给子线程,子线 ...
- 洛谷 P1358 扑克牌
P1358 扑克牌 题目描述 组合数学是数学的重要组成部分,是一门研究离散对象的科学,它主要研究满足一定条件的组态(也称组合模型)的存在.计数以及构造等方面的问题.组合数学的主要内容有组合计数.组合设 ...
- 【hadoop之翊】——CentOS6.5 Linux上面编译Hadoop2.4源代码
今天来说说编译hadoop源代码的事情吧~ 1.首先下载源代码 地址:http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-2.4.0/ 2.加压t ...
- Rpm另类用法加固Linux安全
Rpm另类用法加固Linux安全 RPM是Red Hat Package Manager的缩写即Red Hat软件管理器.它是一个开放的包管理软件,由Red Hat公司所开发和维护,可以在Red ...
- 使用Iperf调整网络
使用Iperf调整网络 Iperf 是一个 TCP/IP 和 UDP/IP 的性能测量工具,通过调谐各种参数可以测试TCP的最大带宽,并报告带宽.延迟,最大段和最大传输单元大小等统计信息.Ip ...
- BZOJ2555: SubString(后缀自动机,LCT维护Parent树)
Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支 ...
- CF-833B The Bakery(线段树优化Dp)
Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredie ...
- 洛谷 P2067 Cytus-Holyknight
P2067 Cytus-Holyknight 题目背景 本人最初作 以此纪念伟大的ios.安卓.PSV平台音乐游戏<cytus> 后续将不断更新. -------------Chapter ...