在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

相关文章

解决JavaWeb-file.isDirectory()遇到的坑问题

《解决JavaWeb-file.isDirectory()遇到的坑问题》JavaWeb开发中,使用`file.isDirectory()`判断路径是否为文件夹时,需要特别注意:该方法只能判断已存在的文... 目录Jahttp://www.chinasem.cnvaWeb-file.isDirectory()遇

JavaWeb-WebSocket浏览器服务器双向通信方式

《JavaWeb-WebSocket浏览器服务器双向通信方式》文章介绍了WebSocket协议的工作原理和应用场景,包括与HTTP的对比,接着,详细介绍了如何在Java中使用WebSocket,包括配... 目录一、概述二、入门2.1 POM依赖2.2 编写配置类2.3 编写WebSocket服务2.4 浏

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

浅析Rust多线程中如何安全的使用变量

《浅析Rust多线程中如何安全的使用变量》这篇文章主要为大家详细介绍了Rust如何在线程的闭包中安全的使用变量,包括共享变量和修改变量,文中的示例代码讲解详细,有需要的小伙伴可以参考下... 目录1. 向线程传递变量2. 多线程共享变量引用3. 多线程中修改变量4. 总结在Rust语言中,一个既引人入胜又可

Rust 数据类型详解

《Rust数据类型详解》本文介绍了Rust编程语言中的标量类型和复合类型,标量类型包括整数、浮点数、布尔和字符,而复合类型则包括元组和数组,标量类型用于表示单个值,具有不同的表示和范围,本文介绍的非... 目录一、标量类型(Scalar Types)1. 整数类型(Integer Types)1.1 整数字

Spring常见错误之Web嵌套对象校验失效解决办法

《Spring常见错误之Web嵌套对象校验失效解决办法》:本文主要介绍Spring常见错误之Web嵌套对象校验失效解决的相关资料,通过在Phone对象上添加@Valid注解,问题得以解决,需要的朋... 目录问题复现案例解析问题修正总结  问题复现当开发一个学籍管理系统时,我们会提供了一个 API 接口去

使用IntelliJ IDEA创建简单的Java Web项目完整步骤

《使用IntelliJIDEA创建简单的JavaWeb项目完整步骤》:本文主要介绍如何使用IntelliJIDEA创建一个简单的JavaWeb项目,实现登录、注册和查看用户列表功能,使用Se... 目录前置准备项目功能实现步骤1. 创建项目2. 配置 Tomcat3. 项目文件结构4. 创建数据库和表5.

Rust中的Option枚举快速入门教程

《Rust中的Option枚举快速入门教程》Rust中的Option枚举用于表示可能不存在的值,提供了多种方法来处理这些值,避免了空指针异常,文章介绍了Option的定义、常见方法、使用场景以及注意事... 目录引言Option介绍Option的常见方法Option使用场景场景一:函数返回可能不存在的值场景

手把手教你idea中创建一个javaweb(webapp)项目详细图文教程

《手把手教你idea中创建一个javaweb(webapp)项目详细图文教程》:本文主要介绍如何使用IntelliJIDEA创建一个Maven项目,并配置Tomcat服务器进行运行,过程包括创建... 1.启动idea2.创建项目模板点击项目-新建项目-选择maven,显示如下页面输入项目名称,选择

C# ComboBox下拉框实现搜索方式

《C#ComboBox下拉框实现搜索方式》文章介绍了如何在加载窗口时实现一个功能,并在ComboBox下拉框中添加键盘事件以实现搜索功能,由于数据不方便公开,作者表示理解并希望得到大家的指教... 目录C# ComboBox下拉框实现搜索步骤一步骤二步骤三总结C# ComboBox下拉框实现搜索步骤一这