在Web中搜索(Searching the Web, ACM/ICPC Beijing 2004, UVa1597)rust解法

2023-10-24 04:28

本文主要是介绍在Web中搜索(Searching the Web, ACM/ICPC Beijing 2004, UVa1597)rust解法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

输入n篇文章和m个请求(n<100,m≤50000),每个请求都是以下4种格式之一。
A:查找包含关键字A的文章。
A AND B:查找同时包含关键字A和B的文章。
A OR B:查找包含关键字A或B的文章。
NOT A:查找不包含关键字A的文章。
处理询问时,需要对于每篇文章输出证据。前3种询问输出所有至少包含一个关键字的行,第4种询问输出整篇文章。关键字只由小写字母组成,查找时忽略大小写。每行不超过80个字符,一共不超过1500行。

样例:
输入

4
A manufacturer, importer, or seller of
digital media devices may not (1) sell,
or offer for sale, in interstate commerce,
or (2) cause to be transported in, or in a
manner affecting, interstate commerce,
a digital media device unless the device
includes and utilizes standard security
technologies that adhere to the security
system standards.
**********
Of course, Lisa did not necessarily
intend to read his books. She might
want the computer only to write her
midterm. But Dan knew she came from
a middle-class family and could hardly
afford the tuition, let alone her reading
fees. Books might be the only way she
could graduate
**********
Research in analysis (i.e., the evaluation
of the strengths and weaknesses of
computer system) is essential to the
development of effective security, both
for works protected by copyright law
and for information in general. Such
research can progress only through the
open publication and exchange of
complete scientific results
**********
I am very very very happy!
What about you?
**********
6
computer
books AND computer
books OR protected
NOT security
very
slick

输出

want the computer only to write her
----------
computer system) is essential to the
==========
intend to read his books. She might
want the computer only to write her
fees. Books might be the only way she
==========
intend to read his books. She might
fees. Books might be the only way she
----------
for works protected by copyright law
==========
Of course, Lisa did not necessarily
intend to read his books. She might
want the computer only to write her
midterm. But Dan knew she came from
a middle-class family and could hardly
afford the tuition, let alone her reading
fees. Books might be the only way she
could graduate
----------
I am very very very happy!
What about you?
==========
I am very very very happy!
==========
not found
==========

解法:

use std::{collections::{BTreeMap, BTreeSet, HashMap},io,
};
#[derive(PartialEq)]
enum WordOp {AND,OR,None,NOT,
}fn get_words(s: &String) -> Vec<String> {let w: String = s.chars().map(|x| {if x.is_alphabetic() {x.to_ascii_lowercase()} else {' '}}).collect();let wds: Vec<String> = w.split_whitespace().map(|x| x.to_string()).collect();wds
}
fn main() {let mut buf = String::new();io::stdin().read_line(&mut buf).unwrap();let n: usize = buf.trim().parse().unwrap();let mut articles: Vec<Vec<String>> = vec![];//每篇文章的所有行let mut words: Vec<HashMap<String, BTreeSet<usize>>> = vec![];//每篇文章的所有单词和单词所在的行号for _i in 0..n {let mut article: Vec<String> = vec![];let mut wd: HashMap<String, BTreeSet<usize>> = HashMap::new();loop {let mut buf = String::new();io::stdin().read_line(&mut buf).unwrap();if buf.trim() == "*".repeat(10) {break;}article.push(buf.trim().to_string());//count wordslet v: Vec<String> = get_words(&buf);let line_idx = article.len() - 1;for w in v.iter() {wd.entry(w.to_string()).and_modify(|x| {x.insert(line_idx);}).or_insert(BTreeSet::from([line_idx]));}}articles.push(article);words.push(wd);}let mut buf = String::new();io::stdin().read_line(&mut buf).unwrap();let m: usize = buf.trim().parse().unwrap();let mut cmds = vec![];for _i in 0..m {let mut buf = String::new();io::stdin().read_line(&mut buf).unwrap();cmds.push(buf.trim().to_string());}for cmd in cmds.iter() {if let Some(idx) = cmd.find("OR") {let word1 = cmd[0..idx - 1].to_string();let word2 = cmd[idx + 3..].to_string();find_word(&articles, &words, &word1, &word2, WordOp::OR);} else if let Some(idx) = cmd.find("AND") {let word1 = cmd[0..idx - 1].to_string();let word2 = cmd[idx + 4..].to_string();find_word(&articles, &words, &word1, &word2, WordOp::AND);} else if let Some(idx) = cmd.find("NOT") {let word1 = cmd[idx + 4..].to_string();find_word(&articles, &words, &word1, &"".to_string(), WordOp::NOT);} else {let word1 = cmd;find_word(&articles, &words, word1, &"".to_string(), WordOp::None);}println!("{}", "=".repeat(10));}
}fn print_result(find_result: BTreeMap<usize, BTreeSet<usize>>, articles: &Vec<Vec<String>>) {if find_result.is_empty() {println!("not found");} else {let mut cnt = 0;for (k, v) in find_result.iter() {for i in v.iter() {println!("{}", articles[*k][*i]);}cnt += 1;if cnt != find_result.len() {println!("{}", "-".repeat(10));}}}
}fn find_word(articles: &Vec<Vec<String>>,words: &Vec<HashMap<String, BTreeSet<usize>>>,word1: &String,word2: &String,op: WordOp,
) {let mut find_result: BTreeMap<usize, BTreeSet<usize>> = BTreeMap::new();for (aidx, lines) in articles.iter().enumerate() {let mut find_line_idx: BTreeSet<usize> = BTreeSet::new();let ws = words.get(aidx).unwrap();if op == WordOp::OR {if ws.contains_key(word1) || ws.contains_key(word2) {if let Some(idx) = ws.get(word1) {find_line_idx.append(&mut idx.clone());}if let Some(idx) = ws.get(word2) {find_line_idx.append(&mut idx.clone());}find_result.insert(aidx, find_line_idx);}} else if op == WordOp::AND {if ws.contains_key(word1) && ws.contains_key(word2) {let idx = ws.get(word1).unwrap();find_line_idx.append(&mut idx.clone());let idx = ws.get(word2).unwrap();find_line_idx.append(&mut idx.clone());find_result.insert(aidx, find_line_idx);}} else if op == WordOp::None {if ws.contains_key(word1) {let idx = ws.get(word1).unwrap();find_line_idx.append(&mut idx.clone());find_result.insert(aidx, find_line_idx);}} else if op == WordOp::NOT {if !ws.contains_key(word1) {find_line_idx.append(&mut (0..lines.len()).collect());find_result.insert(aidx, find_line_idx);}}}print_result(find_result, &articles);
}

这篇关于在Web中搜索(Searching the Web, ACM/ICPC Beijing 2004, UVa1597)rust解法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/272686

相关文章

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

SpringBoot项目Web拦截器使用的多种方式

《SpringBoot项目Web拦截器使用的多种方式》在SpringBoot应用中,Web拦截器(Interceptor)是一种用于在请求处理的不同阶段执行自定义逻辑的机制,下面给大家介绍Sprin... 目录一、实现 HandlerInterceptor 接口1、创建HandlerInterceptor实

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

Python使用Reflex构建现代Web应用的完全指南

《Python使用Reflex构建现代Web应用的完全指南》这篇文章为大家深入介绍了Reflex框架的设计理念,技术特性,项目结构,核心API,实际开发流程以及与其他框架的对比和部署建议,感兴趣的小伙... 目录什么是 ReFlex?为什么选择 Reflex?安装与环境配置构建你的第一个应用核心概念解析组件

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

rust 中的 EBNF简介举例

《rust中的EBNF简介举例》:本文主要介绍rust中的EBNF简介举例,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 什么是 EBNF?2. 核心概念3. EBNF 语法符号详解4. 如何阅读 EBNF 规则5. 示例示例 1:简单的电子邮件地址

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加

Python使用DeepSeek进行联网搜索功能详解

《Python使用DeepSeek进行联网搜索功能详解》Python作为一种非常流行的编程语言,结合DeepSeek这一高性能的深度学习工具包,可以方便地处理各种深度学习任务,本文将介绍一下如何使用P... 目录一、环境准备与依赖安装二、DeepSeek简介三、联网搜索与数据集准备四、实践示例:图像分类1.