Uva1597 -Searching the Web
来源:互联网 发布:网宿科技 阿里云 编辑:程序博客网 时间:2024/05/19 20:47
Uva1597 -Searching the Web
输入n篇文章和m个询问,格式如下:
1、A:搜索包含关键字A的文章,按顺序输出所有包含关键字的行 2、A AND B:搜索包含关键字A和B的文章,按顺序输出所有包含关键字的行
3、A OR B:搜索包含关键字A或B的文章,按顺序输出所有包含关键字的行 4、NOT A:搜索不包含关键字A的文章,按顺序输出所有不包含关键字的文章
分析:简单的模拟题,直接对于每一篇文章map查找即可,不过这题有个细节就是查询的关键字都是小写字母,所以在输入的时候要处理一下,全部转换成小写,使用结构体保存每一篇文章的信息,之后对于每一篇文章查询即可。
不过在做这题时,当询问为AND或者OR的形式时,刚开始是写成遍历每一行查找子串了,其实应该是查询有没有包含那个单词,可能会出现这个查询的关键字在这一行中不是单词,而是某一个单词的子串的情况……无尽的WA后好不容易找到了这个傻逼的错误,又wa了十次左右,最后错误居然是因为输出的分隔符错了,应该是十个字符,可是题目给的样例输出中写成9个了,然后我就直接复制的,真是坑爹无下限啊….
使用各种stl容器写的,跑了820ms,想必如果对每个单词维护其处在哪篇文章的哪一行的信息的话,肯定会快了不少,而不需要对每一篇文章去查找了。
这种模拟题居然错了25次才A,整整卡了一天的时间,像这种没法跑数据对拍的题,错了也是很无奈,然后总喜欢去乱改,改来改去还是WA,其实是没思考自己整个的思路的缺陷与细节方面的处理问题,必须得改正才这个不好的习惯了,哎…
#include<cstdio>#include<cstring>#include<algorithm>#include <map>#include <cctype>#include <string>#include <set>#define INF 0x3f3f3f3f#define LL long long#define N 5005using namespace std;typedef map<string, int>::iterator mit;typedef set<int>::iterator sit;struct document{ char text[1510][120]; multimap<string, int> mp; int num;}dou[110];char s[1000];set<int> se;int main(){ int n, q; scanf("%d", &n); getchar(); for (int i = 0; i < n; i++){ int j = 1; while (gets(dou[i].text[j++]), strcmp(dou[i].text[j-1], "**********")){ char *p = dou[i].text[j-1]; int len = strlen(p); for (int x = 0; x < len; ){ if (isalpha(p[x])){ string tm = ""; while (x < len && isalpha(p[x])) tm += tolower(p[x]), x++; dou[i].mp.insert(make_pair(tm, j-1)); } else x++; } } dou[i].num = j-1; } scanf("%d", &q); getchar(); while (q--){ int cnt = 0, k = 0; char t[10][100]; gets(s); char *p = strtok(s, " "); while (p) strcpy(t[k++], p), p = strtok(NULL, " "); if (k == 1){ for (int i = 0; i < n; i++){ if (dou[i].mp.count(t[0])){ se.clear(); if (cnt++) puts("----------"); mit s = dou[i].mp.lower_bound(t[0]), e = dou[i].mp.upper_bound(t[0]); for (mit p = s; p != e; p++) se.insert(p->second); for (sit p = se.begin(); p != se.end(); p++) puts(dou[i].text[*p]); } } } else if (k == 2){ for (int i = 0; i < n; i++){ if (!dou[i].mp.count(t[1])){ if (cnt++) puts("----------"); for (int j = 1; j < dou[i].num; j++) puts(dou[i].text[j]); } } } else { if (strcmp(t[1], "AND") == 0){ for (int i = 0; i < n; i++){ if (dou[i].mp.count(t[0]) && dou[i].mp.count(t[2])){ se.clear(); if (cnt++) puts("----------"); mit s = dou[i].mp.lower_bound(t[0]), e = dou[i].mp.upper_bound(t[0]); for (mit p = s; p != e; p++) se.insert(p->second); s = dou[i].mp.lower_bound(t[2]), e = dou[i].mp.upper_bound(t[2]); for (mit p = s; p != e; p++) se.insert(p->second); for (sit p = se.begin(); p != se.end(); p++) puts(dou[i].text[*p]); } } } else{ for (int i = 0; i < n; i++){ if (dou[i].mp.count(t[0]) || dou[i].mp.count(t[2])){ se.clear(); if (cnt++) puts("----------"); mit s = dou[i].mp.lower_bound(t[0]), e = dou[i].mp.upper_bound(t[0]); for (mit p = s; p != e; p++) se.insert(p->second); s = dou[i].mp.lower_bound(t[2]), e = dou[i].mp.upper_bound(t[2]); for (mit p = s; p != e; p++) se.insert(p->second); for (sit p = se.begin(); p != se.end(); p++) puts(dou[i].text[*p]); } } } } if (!cnt) puts("Sorry, I found nothing."); puts("=========="); } return 0;}
0 0
- UVa1597 Searching the Web
- UVa1597 - Searching the Web
- Uva1597 -Searching the Web
- 习题5-10 在Web中搜索(Searching the Web, UVa1597)
- [刷题]算法竞赛入门经典(第2版) 5-10/UVa1597 - Searching the Web
- 算法竞赛入门经典 第二版 习题5-10 在Web中搜索 Searching the Web uva1597
- searching the Deep web
- 1597 - Searching the Web
- 《searching the web》学习笔记
- POJ 2050 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(模拟)
- uva 1597 Searching the Web (wrong)
- 搜索引擎早期重要论文推荐系列【7】《Searching the Web》
- Searching the String zju3228
- cocos2d-js + webstrom 的安装与配置
- uuid udid区别
- golang windows下 调用外部程序隐藏cmd窗口
- Java classloader机制
- spring项目中,web.xml中的 ContextLoaderListener监听器的原理
- Uva1597 -Searching the Web
- NDK开发系列之环境的搭建
- 基于地图的短信追踪小练习(1)百度地图的初创建
- 欢迎使用CSDN-markdown编辑器
- Calendar 基本使用方式
- Java中IO总结
- vue.js入门实例
- 获取沙盒路径
- c++使用libiconv