在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

相关文章

认识、理解、分类——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

Java Web指的是什么

Java Web指的是使用Java技术进行Web开发的一种方式。Java在Web开发领域有着广泛的应用,主要通过Java EE(Enterprise Edition)平台来实现。  主要特点和技术包括: 1. Servlets和JSP:     Servlets 是Java编写的服务器端程序,用于处理客户端请求和生成动态网页内容。     JSP(JavaServer Pages)

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

hdu 4517 floyd+记忆化搜索

题意: 有n(100)个景点,m(1000)条路,时间限制为t(300),起点s,终点e。 访问每个景点需要时间cost_i,每个景点的访问价值为value_i。 点与点之间行走需要花费的时间为g[ i ] [ j ] 。注意点间可能有多条边。 走到一个点时可以选择访问或者不访问,并且当前点的访问价值应该严格大于前一个访问的点。 现在求,从起点出发,到达终点,在时间限制内,能得到的最大

AI基础 L9 Local Search II 局部搜索

Local Beam search 对于当前的所有k个状态,生成它们的所有可能后继状态。 检查生成的后继状态中是否有任何状态是解决方案。 如果所有后继状态都不是解决方案,则从所有后继状态中选择k个最佳状态。 当达到预设的迭代次数或满足某个终止条件时,算法停止。 — Choose k successors randomly, biased towards good ones — Close

hdu4277搜索

给你n个有长度的线段,问如果用上所有的线段来拼1个三角形,最多能拼出多少种不同的? import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;

EasyPlayer.js网页H5 Web js播放器能力合集

最近遇到一个需求,要求做一款播放器,发现能力上跟EasyPlayer.js基本一致,满足要求: 需求 功性能 分类 需求描述 功能 预览 分屏模式 单分屏(单屏/全屏) 多分屏(2*2) 多分屏(3*3) 多分屏(4*4) 播放控制 播放(单个或全部) 暂停(暂停时展示最后一帧画面) 停止(单个或全部) 声音控制(开关/音量调节) 主辅码流切换 辅助功能 屏