在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

相关文章

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下拉框实现搜索步骤一这

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

uva 11044 Searching for Nessy(小学数学)

题意是给出一个n*m的格子,求出里面有多少个不重合的九宫格。 (rows / 3) * (columns / 3) K.o 代码: #include <stdio.h>int main(){int ncase;scanf("%d", &ncase);while (ncase--){int rows, columns;scanf("%d%d", &rows, &col