uva 1597 Searching the Web (wrong)

Uva1597 -Searching the Web Uva1597 -Searching the Web 输入n篇文章和m个询问,格式如下: 1、A:搜索包含关键字A的文章,按顺序输出所有包含关键字的行A AND         2、B:搜索包含关键字A和B的文章,按顺序输出所有包含关键字的行 3、A OR B:搜索包含关键字A或B的文章,按顺序输出所有包含关键字的行    4、NOT A:搜索不包含关键字A的文章,按顺序输出所有不包含关键字 阅读详情

题目:Searching the Web


思路:

做了几天,还是没过,先把错误记录下:

(1)每输入一个指令,在文章中查找一遍。(TLE)

(2)每个单词都对应一些文章号和行号,先处理后直接查找打印。(WA)


代码:

//TLE代码

#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

int n,m;
vector<string> Passage[105];
vector<string> p[105];

void FIND(string x) {
	vector<int> vec;
	for(int i=1; i<=n; i++) {
		for(int j=0; j<Passage[i].size(); j++) {
			if(p[i][j].find(x)!=string::npos) {
				vec.push_back(i);
				break;
			}
		}
	}
	if(vec.size()==0) {
		cout<<"Sorry, I found nothing.\n";
		return ;
	}
	for(int i=0; i<vec.size()-1; i++) {
		for(int j=0; j<Passage[vec[i]].size(); j++) {
			if(p[vec[i]][j].find(x)!=string::npos)
				cout<<Passage[vec[i]][j]<<endl;
		}
		cout<<"---------\n";
	}
	for(int j=0; j<Passage[vec[vec.size()-1]].size(); j++) {
		if(p[vec[vec.size()-1]][j].find(x)!=string::npos)
			cout<<Passage[vec[vec.size()-1]][j]<<endl;
	}
}

void AND(string x,string y) {
	vector<int> vec;
	for(int i=1; i<=n; i++) {
		bool f1=false,f2=false;
		for(int j=0; j<Passage[i].size(); j++) {
			if(p[i][j].find(x)!=string::npos) {
				f1=true;
			}
			if(p[i][j].find(x)!=string::npos) {
				f2=true;
			}
		}
		if(f1==true&&f2==true) vec.push_back(i);
	}
	if(vec.size()==0) {
		cout<<"Sorry, I found nothing.\n";
		return ;
	}
	for(int i=0; i<vec.size()-1; i++) {
		for(int j=0; j<Passage[vec[i]].size(); j++) {
			if(p[vec[i]][j].find(x)!=string::npos||p[vec[i]][j].find(y)!=string::npos)
				cout<<Passage[vec[i]][j]<<endl;
		}
		cout<<"---------\n";
	}
	for(int j=0; j<Passage[vec[vec.size()-1]].size(); j++) {
		if(p[vec[vec.size()-1]][j].find(x)!=string::npos||p[vec[vec.size()-1]][j].find(y)!=string::npos)
			cout<<Passage[vec[vec.size()-1]][j]<<endl;
	}
}

void OR(string x,string y) {
	vector<int> vec;
	for(int i=1; i<=n; i++) {
		for(int j=0; j<Passage[i].size(); j++) {
			if(p[i][j].find(x)!=string::npos||p[i][j].find(y)!=string::npos) {
				vec.push_back(i);
				break;
			}
		}
	}
	if(vec.size()==0) {
		cout<<"Sorry, I found nothing.\n";
		return ;
	}
	for(int i=0; i<vec.size()-1; i++) {
		for(int j=0; j<Passage[vec[i]].size(); j++) {
			if(p[vec[i]][j].find(x)!=string::npos||p[vec[i]][j].find(y)!=string::npos)
				cout<<Passage[vec[i]][j]<<endl;
		}
		cout<<"---------\n";
	}
	for(int j=0; j<Passage[vec[vec.size()-1]].size(); j++) {
		if(p[vec[vec.size()-1]][j].find(x)!=string::npos||p[vec[vec.size()-1]][j].find(y)!=string::npos)
			cout<<Passage[vec[vec.size()-1]][j]<<endl;
	}
}

void NOT(string x) {
	vector<int> vec;
	for(int i=1; i<=n; i++) {
		bool f=false;
		for(int j=0; j<Passage[i].size(); j++) {
			if(p[i][j].find(x)!=string::npos) {
				f=true;
			}
		}
		if(f==false) {
			vec.push_back(i);
		}
	}
	if(vec.size()==0) {
		cout<<"Sorry, I found nothing.\n";
		return ;
	}
	for(int i=0; i<vec.size()-1; i++) {
		for(int j=0; j<Passage[vec[i]].size(); j++) {
			cout<<Passage[vec[i]][j]<<endl;
		}
		cout<<"---------\n";
	}
	for(int j=0; j<Passage[vec[vec.size()-1]].size(); j++) {
		cout<<Passage[vec[vec.size()-1]][j]<<endl;
	}
}

int main() {
//	freopen("5.5 1597.out","w",stdout);
	scanf("%d",&n);
	getchar();
	for(int i=1; i<=n; i++) {
		string x;
		while(getline(cin,x)&&x[0]!='*') {
			Passage[i].push_back(x);
			for(int j=0; j<x.size(); j++) {
				if('A'<=x[j]&&x[j]<='Z') x[j]=x[j]-'A'+'a';
			}
			p[i].push_back(x);
		}
	}

	scanf("%d",&m);
	string x;
	getchar();
	while(getline(cin,x)&&m--) {
		if(x[0]=='N') {
			x=x.substr(4,x.size()-4);
			NOT(x);
		} else if(x.find(" AND ")!=string::npos) {
			int y=x.find(" AND ");
			string one=x.substr(0,y);
			string two=x.substr(y+5,x.size()-y-5);
			AND(one,two);
		} else if(x.find(" OR ")!=string::npos) {
			int y=x.find(" OR ");
			string one=x.substr(0,y);
			string two=x.substr(y+4,x.size()-y-4);
			OR(one,two);
		} else {
			FIND(x);
		}
		cout<<"==========\n";
	}

	return 0;
}



//WA代码

#include<cstdio>
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;

struct Pair {
	int x,y;
	Pair() {}
	Pair(int one,int two) {
		x=one,y=two;
	}
	bool operator < (const Pair& other) const {
		if(x<other.x||(x==other.x&&y<other.y)) return true;
		return false;
	}
	bool operator == (const Pair& other) const {
		if(x==other.x&&y==other.y) return true;
		return false;
	}
};

int n,m;
vector<string> Passage[105];
map<string,vector<Pair> > mp;

int main() {
//	freopen("5.5 1597.out","w",stdout);
	scanf("%d",&n);
	getchar();

	for(int i=0; i<n; i++) {
		string x;
		while(getline(cin,x)&&x[0]!='*') {
			Passage[i].push_back(x);
			for(int j=0; j<x.size(); j++) {
				if('A'<=x[j]&&x[j]<='Z') x[j]=x[j]-'A'+'a';
			}
			x+=" ";
			string w;
			map<string,bool> a;
			for(int j=0; j<x.size(); j++) {
				if(isalpha(x[j])) {
					w+=x[j];
				} else if(j!=0&&isalpha(x[j-1])) {
					if(!a.count(w))
						mp[w].push_back(Pair(i,Passage[i].size()-1));
					a[w]=true;
					w.clear();
				}
			}
		}
	}

	for(map<string,vector<Pair> >::iterator it=mp.begin();it!=mp.end();it++){
		sort(mp[(it->first)].begin(),mp[(it->first)].end());
//		cout<<(it->first)<<" ----- \n";
//		for(int i=0;i<(it->second).size();i++){
//			cout<<"    "<<(it->second)[i].x<<' '<<(it->second)[i].y<<' '<<Passage[(it->second)[i].x][(it->second)[i].y]<<endl;
//		}
//		cout<<endl;
	}

	scanf("%d",&m);
	getchar();
	while(m--) {
		string x;
		getline(cin,x);
		if(x[0]=='N') {
			x=x.substr(4,x.size()-4);

			vector<Pair> vec=mp[x];
			bool a[105]= {0};
			for(int i=0; i<vec.size(); i++) {
				a[vec[i].x]=true;
			}
			bool flag=false;
			for(int i=0; i<n; i++) {
				if(!a[i]) {
					if(flag==true) printf("----------\n");
					else flag=true;
					for(int j=0; j<Passage[i].size(); j++) {
						cout<<Passage[i][j]<<endl;
					}
				}
			}
			if(flag==false) printf("Sorry, I found nothing.\n");
		} else if(x.find(" AND ")!=string::npos) {
			int y=x.find(" AND ");
			string one=x.substr(0,y);
			string two=x.substr(y+5,x.size()-y-5);

			if(!mp.count(one)||!mp.count(two)) printf("Sorry, I found nothing.\n");
			else {
				vector<Pair> vec=mp[one],t=mp[two];
				bool a[105]= {0},c[105]={0};
				for(int i=0; i<vec.size(); i++) {
					a[vec[i].x]=true;
				}
				for(int i=0; i<t.size(); i++) {
					c[t[i].x]=true;
				}
				for(int i=0;i<n;i++){
					a[i]&=c[i];
				}
				for(int i=0; i<t.size(); i++) {
					vec.push_back(t[i]);
				}
				sort(vec.begin(),vec.end());
				bool b[2000]= {0};
				for(int i=1; i<vec.size(); i++) {
					if(vec[i]==vec[i-1]) b[i]=true;
				}
				int lastp=-1;
				for(int i=0; i<vec.size(); i++) {
					if(b[i]) continue;
					int p=vec[i].x,r=vec[i].y;
					if(!a[p]) continue;
					if(p!=lastp&&lastp!=-1) {
						printf("----------\n");
					}
					lastp=p;
					cout<<Passage[p][r]<<endl;
				}
			}
		} else if(x.find(" OR ")!=string::npos) {
			int y=x.find(" OR ");
			string one=x.substr(0,y);
			string two=x.substr(y+4,x.size()-y-4);

			if(!mp.count(one)&&!mp.count(two)) printf("Sorry, I found nothing.\n");
			else {
				vector<Pair> vec=mp[one],t=mp[two];
				for(int i=0; i<t.size(); i++) {
					vec.push_back(t[i]);
				}
				sort(vec.begin(),vec.end());
				bool b[2000]= {0};
				for(int i=1; i<vec.size(); i++) {
					if(vec[i]==vec[i-1]) b[i]=true;
				}
				int lastp=-1;
				for(int i=0; i<vec.size(); i++) {
					if(b[i]) continue;
					int p=vec[i].x,r=vec[i].y;
					if(p!=lastp&&lastp!=-1) {
						printf("----------\n");
					}
					lastp=p;
					cout<<Passage[p][r]<<endl;
				}
			}
		} else {
			if(!mp.count(x)) printf("Sorry, I found nothing.\n");
			else {
				vector<Pair> vec=mp[x];
				int lastp=-1;
				for(int i=0; i<vec.size(); i++) {
					int p=vec[i].x,r=vec[i].y;
					if(p!=lastp&&lastp!=-1) {
						printf("----------\n");
					}
					lastp=p;
					cout<<Passage[p][r]<<endl;
				}
			}
		}
		cout<<"==========\n";
	}

	return 0;
}




Win10系统下Protel 99 SE元件库导入报错?3步搞定File is not recognized问题 本文详细介绍了在Win10系统下解决Protel 99 SE元件库导入报错'File is not recognized'的3步终极方案。通过手动修改配置文件、使用专用工具和优化系统兼容性设置,帮助用户快速解决这一经典PCB设计软件的兼容性问题,提升工作效率。 阅读详情

相关推荐

Vienna整流器双PI闭环仿真:从SVPWM调制到中点电位平衡的实践解析

本文深入解析Vienna整流器双PI闭环仿真技术,涵盖SVPWM调制和中点电位平衡的实践细节。通过详细的MATLAB/Simulink仿真搭建流程和常见问题排查方法,帮助工程师掌握高效整流器控制策略,提升系统稳定性和动态响应性能。

weixin_29168153的博客 340

UVA1597 在Web中搜索 Searching the Web

主要的思路就是,输入的时候的处理,首先我们要把每个文章每一行都原封不动的储存起来,这需要一个数组,然后我们需要一个集合数组,每个文章里面的单词放到一个集合里面,然后我们还需要一个数组,用来存放每篇文章整理过后的每一行,整理的方法是,把所有的单词化成小写,然后后面加一个空格,拼成一个字符串,字符串的前面再加一个空格,这样我们想要查某个单词的时候就查前后加了空格的单词,因为不加空格,直接拿着原文那种去查,会因为单词包含单词的问题出错,然后其余的就比较简单了,然后如果查询没有查到东西要特殊输出。......

m0_73035684的博客 229

时序异常检测汇总

时序异常检测

Mark_Australia的博客 1万+

UVA 1597 Searching the Web

题目链接:https://vjudge.net/problem/UVA-1597 题目翻译摘自《算法禁赛入门经典》 题目大意   输入 n 篇文章和 m 个请求(n < 100,m ≤ 50000),每个请求都是以下 4 种格式之一。 A:查找包含关键字 A 的文章。 A AND B:查找同时包含关键字 A 和 B 的文章。 A OR B:查找包含关键字 A 或 B...

weixin_30548917的博客 524

避坑指南:用pygrib处理ERA5气象数据时遇到的5个典型问题

本文总结了使用pygrib库处理ERA5气象GRIB数据时常见的五个典型问题及解决方案,涵盖数据获取、内存管理、时间处理、空间插值和性能优化。重点针对气象数据插值这一核心挑战,提供了处理不规则网格、缺失值和跨日期变更线的实用代码示例与优化策略,帮助用户高效、稳定地完成气象数据分析工作。

weixin_28224217的博客 187

手把手教你用VM实现欧姆龙PLC数据采集(含Fins协议配置详解)

本文详细介绍了如何使用VM(VisionMaster)实现与欧姆龙PLC的数据采集,重点解析了Fins协议(包括Fins TCP/UDP)的配置步骤。从网络环境搭建、PLC的IP地址设置,到VM端Fins通讯通道的详细配置与数据点映射,提供了完整的实战指南。文章还涵盖了数据收发解析、常见问题排查清单,帮助工程师快速完成视觉系统与PLC的稳定集成。

weixin_29171087的博客 195

UVA - 1597 Searching the Web

/* 法一借鉴自bolg:http://blog.csdn.net/code4101/article/details/39587211 收获: 1. typedef定义数组类型,格式应为 typedef int int_array[80]; 此后int_array可作为有80个元素的int数组的简写 http://blog.csdn.net/huang_xw/artic

怕什么真理无穷?进一寸有一寸的欢喜~ 482

UniApp实战:从文件下载到本地保存与预览的一站式解决方案

本文深入解析了UniApp开发中文件下载、保存与预览的完整流程,提供了基于uni.downloadFile、uni.saveFile和uni.openDocument API的一站式实战解决方案。文章详细拆解了核心API的使用方法、多平台(App、H5)的兼容性处理、常见坑点(如文件路径、权限、格式支持)以及进阶优化技巧(如进度提示、文件管理),旨在帮助开发者构建健壮、用户体验良好的跨平台文件处理功能。

d3e4f的博客 625

UVa1597 Searching the Web

先扯点废话

代号4101的专栏 4570

基于STM32的嵌入式平台实现EtherCAT主站

本文将介绍如何在基于STM32的嵌入式平台上构建EtherCAT主站,并提供相应的源代码。使用SOEM库提供的API函数,我们可以实现与EtherCAT从站设备的实时通信和数据交换。在STM32的代码中,使用SOEM库提供的API函数初始化EtherCAT主站。在SOEM库中,我们可以找到EtherCAT主站的实现代码和相关的API函数。使用SOEM库提供的API函数实现EtherCAT主站的功能,包括发送和接收EtherCAT数据帧、处理从站设备的状态等。确保连接正常并且设备能够相互通信。

CodeSageX的博客 2844

实现端到端加密的WebRTC视频会议终端

需要注意的是,上述示例代码中没有涉及到具体的加密算法和密钥交换过程,而是通过WebRTC API自动处理了加密和密钥交换的细节。然后,我们通过信令服务器将本地SDP发送给对方,并接收对方的SDP。最后,我们将对方的SDP设置为远程描述,并将最终的连接建立起来。最后,我们将对方的SDP设置为远程描述,并建立最终的连接。在本文中,我们将探讨如何使用WebRTC构建一个加密的视频会议终端,并提供相应的源代码。在本文中,我们将探讨如何使用WebRTC构建一个加密的视频会议终端,并提供相应的源代码。

WaJulia的博客 437

uva 1597 web 查找

Uva 1597 题目大意 输入一个 n 接下来输入 n 篇文章,每篇文章以*********结尾,接下来有 m 个单词或单词与单词直接的关系  输出每个单词存在的行。 The word “search engine” may not be strange to you. Generally speaking, a search engine searches the web pages ava...

Mr_Right_zwu的博客 527

三相五柱式消弧线圈自动测控装置设计与实现

本文设计了一种基于STM32的三相五柱式消弧线圈自动测控装置。该装置采用信号注入法检测系统对地电容电流,通过PID自适应算法调节消弧线圈电感值,实现单相接地故障时的快速补偿。硬件系统包含信号采集、注入、控制执行等模块,软件采用Goertzel算法提取特征频率信号,结合增量式PID实现精确控制。实验结果表明,装置电容检测误差小于2%,调谐响应时间小于50ms,满足配电网对消弧线圈自动调谐的技术要求。

AI的内部世界的博客 242

【习题 5-10 UVA-1597】Searching the Web

【链接】 我是链接,点我呀:) 【题意】 在这里输入题意 【题解】 用map < string,vector < int > >mmap[100];来记录每一个数据段某个字符串出现的行数,以及用来判断这个字符串在这一段中存不存在。 ->这里有一个地方要注意,千万不要在未确定这个字符串是否存在之前,调用mmap[i][s],因为这样,不管s存...

AWCXV 86

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

uva-1597

#include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;map&gt; #include&lt;vector&gt; #include&lt;sstream&gt; using namespace std; map&lt;string,vector&lt;int&gt; &amp

sxy201658506207的博客 368

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

UVa 1597-Searching the Web

这个题比较长,如果按照题目要求一步一步做的话。也不是很难。但是如果想提高代码精简度和可读性高效性确实不容易,想轻松AC还是用C++,C语言的输入输出。Java很容易超时。普通算法要10s左右。 Java代码 : import java.util.*; public class Main1597 { public static void main(String[] args) { ...

weixin_33906657的博客 185

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

UVA ~ 1597 ~ Searching the Web (模拟,map+bitset)

题意:输入n篇文章和m个请求(n 1.A:查找包含关键字A的文章。(即如果某篇文章中含有A,输出含有A的行) 2.A AND B:查找同时包含关键字A和B的文章。(即如果某篇文章中含有A和B,输出含有A的行,或含有B的行) 3.A OR B:查找包含关键字A或B的文章。(即如果某篇文章中含有A或B,输出含有A的行,或含有B的行) 4.NOT A:查找不包含关键字A的文章。(即如果某篇文章

张松超的博客 436

UVa 1597 - Searching the Web(模拟)

模拟网页搜索,直接遍历查找会超时,需要使用映射或指针一类的简化查找。 直接用STL写的,原本准备超时之后再用指针重写,没想到2.595s过了,就懒得改了。

wcr1996的专栏 2291

UVA1597--Bug Hunt

题目: 输入并模拟一段程序,输出第一个bug所在的行。每行程序有两种可能: 数组定义:格式为arr[size],例如a[10]; 赋值语句:格式为arr[index]=value,例如a[0]=3或者a[a[0]]=3 赋值语句可能会出现两种bug:下标index越界,使用未初始化变量。 思路: 定义数组类A

wanna_wsl的博客 478

UVA 755 解题报告

题目意思: 找出重复的电话号码,并输出重复次数。用 c++的 map。打个字母到数字的映射。就可以直接粗暴地解决这题了。注意点: 1. 不同测试集中间的输出要加一空行。最后一个不需要。代码链接:UVA 755

学无止境 588

Uva - 1597 - Searching the Web

早晨写了好久,最后终于AC了,有一定的实际问题意义。 AC代码: 注意提交的时候选C++11,如果选C++会CE。 #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include &l...

anpi3191的博客 199

UVA 1597 - Searching the Web

有点小坑,一定要注意利用bool* 来存储map的查询结果,这样一优化后就不会导致超时的问题了,不然每次都用map查询会大大降低时间的效率。 就是建立每一行文本中出现的每个单词与对应行的行号的映射,后面再对查询内容进行分析,逐行判断即可。源代码如下,很好理解。 #include #include #include #include #include #include #include

zju2016的博客 350

UVa:1597 - Searching the Web

题目思路:首先需要构造出关键词的字典,提取关键字有一个小技巧:可以把字符串中所有非英文字母化为空格,然后用stringstream读取 void updatedic(string str,int doc_id,int row_id) { string word; for(int i=0;i<str.size();i++) { if(isalpha(str[i])) str[i

没有趟不过的河 2074

习题5-10 在Web中搜索(Searching the Web, UVa1597)

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define CLOSE() ios::sync_with_stdio(false) #defin

%%% 370

UVA 1597 Search the Web

这个题就是考察STL容器的综合运用,由于文章比较长并且请求很多,直接暴力扫肯定会超时,所以需要用map作个映射。 我的思路:1.用一个map>>,第一个int代表第几篇文章,第二个map把单词及其行数对应起来,因为一个单词可能在同一行中出现两次,所以用了 一 个                           set来存单词的行位置.                            

wangjinhaowjh的博客 602

UVa 1597 - Searching the Web

这道题仍然是做了3个月,一直超时和WA,最后参考某大神的AC代码终于过了。巧妙的地方就是运用||和&&来进行搜索时的“和”和“或”操作,另外分析文章后的存储数据的数据结构也很重要,还有代码中很多重复的地方不妨#define了,可以使代码更加简洁。不过最后AC还是用了1.1秒,貌似有用字典树做的方法会更快吧。 #include <bits/stdc++.h> u...

weixin_30301183的博客 161
上一篇: uva 230 Borrowers
下一篇: uva 12504 Updating a Dictionary
verdin黄大锤
博客等级 码龄10年 59粉丝 606原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值