在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);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/115651.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL中如何处理重复数据(Duplicate)?

在MySQL中&#xff0c;处理重复数据&#xff08;Duplicate&#xff09;的主要方式是使用INSERT INTO ... ON DUPLICATE KEY UPDATE语句或REPLACE INTO语句。 INSERT INTO ... ON DUPLICATE KEY UPDATE语句&#xff1a;该语句在插入数据时&#xff0c;如果遇到唯一键冲突&#x…

医药电商行业想要精准获客?媒介盒子分享三大技巧

随着医疗需求的不断增长&#xff0c;健康成为社会关注的重点&#xff0c;消费者对医药保健产品和需求正在不断增长&#xff0c;数字化时代的来临使医药行业逐渐电商化&#xff0c;线上零售渠道成为医药行业销售额的主要来源&#xff0c;那当下医药电商行业如何抓住机遇&#xf…

怎么快速、合法地挣到一百万?

大部分的人都想要在互联网上面赚到属于自己的那份钱 这类分为两类&#xff0c;一种是纯新人&#xff0c;没有接触过任何行业&#xff0c;所要做的是能够让人帮你根据自己的情况完成筛选&#xff0c;看哪些基础简单的事情可以去做。 第二类已经有项目经验的人&#xff0c;这时…

AB试验(六)A/B实验常见知识点的Python计算

AB试验&#xff08;六&#xff09;A/B实验常见知识点的Python计算 前面理论知识上提到了很多的知识点需要计算&#xff0c;作为一个实用主义的博主&#xff0c;怎么可以忍受空谈呢&#xff1f;所以本期就给大家分享如何利用Python对这些知识点进行计算。 均值类指标 import …

上架用的SDK三方应用隐私

SDK名称&#xff1a;华为推送 使用目的&#xff1a;用于向华为手机用户推送消息 使用场景&#xff1a;用户账号相关促销活动、消息提醒更新时 信息收集类型&#xff1a;设备相关信息&#xff08;Android_ID&#xff09;使用的敏感权限&#xff1a;不涉及 使用的敏感权限&am…

一起学数据结构(11)——快速排序及其优化

上篇文章中&#xff0c;解释了插入排序、希尔排序、冒泡排序、堆排序及选择排序的原理及具体代码实现本片文章将针对快速排序&#xff0c;快速排序的几种优化方法、快速排序的非递归进行解释。 目录 1. 快速排序原理解析以及代码实现&#xff1a; 2. 如何保证相遇位置的值一…

嵌入式硬件库的基本操作方式与分析

本次要介绍的开源软件是 c-periphery&#xff1a; https://github.com/vsergeev/c-periphery一个用 C 语言编写的硬件外设访问库。 我们可以用它来读写 Serial、SPI、I2C 等&#xff0c;非常适合在嵌入式产品上使用。 我们可以基于它优秀的代码框架&#xff0c;不断地扩展出更…

Prometheus接入AlterManager配置邮件告警(基于K8S环境部署)

文章目录 一、配置AlterManager告警发送至邮箱二、Prometheus接入AlterManager配置三、部署PrometheusAlterManager(放到一个Pod中)四、测试告警 注意&#xff1a;请基于 PrometheusGrafana监控K8S集群(基于K8S环境部署)文章之上做本次实验。 一、配置AlterManager告警发送至邮…

C++——特殊类设计

目录 一.不能被拷贝的类 1.C98做法 2.C11做法 二.只能在堆上实例化的类 1.实现方式一 2.实现方式二 三.只能在栈上创建的对象 四.不能被继承的类 1.C98方式 2.C11方式 五.只能创建一个对象的类 1.设计模式 2.单例模式 一.不能被拷贝的类 拷贝只会放在两个场景中&a…

visual studio Qt 开发环境中手动添加 Q_OBJECT 导致编译时出错的问题

问题简述 创建项目的时候&#xff0c;已经添加了类文件&#xff0c;前期认为不需要信号槽&#xff0c;就没有添加宏Q_OBJECT,后面项目需要&#xff0c;又加入了宏Q_OBJECT&#xff0c;但是发现只是添加了一个宏Q_OBJECT&#xff0c;除此之外没有改动其它的代码&#xff0c;原本…

基于springboot实现地方废物回收机构平台管理系统【项目源码+论文说明】

基于springboot实现地方废物回收机构管理系统演示 摘要 网络的广泛应用给生活带来了十分的便利。所以把地方废物回收机构管理与现在网络相结合&#xff0c;利用java技术建设地方废物回收机构管理系统&#xff0c;实现地方废物回收机构的信息化。则对于进一步提高地方废物回收机…

如何提高广告投放转化率?Share Creators 资产库与Appsflyer营销数据的全面结合

如何提高广告投放转化率&#xff1f;Share Creators 资产库与Appsflyer营销数据的全面结合 全球经济进入了低迷期。 营销成本越来越高&#xff0c; 营销需要更务实&#xff0c;注重投入产出比。众所周知&#xff0c;除了渠道、客群画像以外&#xff0c; 优秀的广告设计图&#…

c进阶测试题

选择题 1.请问该程序的输出是多少&#xff08;C&#xff09; #include<stdio.h> int main(){unsigned char i 7;int j 0;for(;i > 0;i - 3){ j;} printf("%d\n", j);return 0; }A. 2 B. 死循环 C. 173 D. 172 首先unsigned char型是不会为负数&#xff…

flask入门(四)前后端数据传输

文章目录 1、flask后端接收来自前端的数据1&#xff09;如果前端提交的方法为POST2&#xff09;如果前段提交的方法是GET 2、flask后端向前端传数据3、案例参考文献 1、flask后端接收来自前端的数据 1&#xff09;如果前端提交的方法为POST 后端接收时的代码&#xff1a; xx…

pojo之vo_dto_po的一些理解

一次扫盲VO、DTO、DO和PO区别、用法、概念~-腾讯云开发者社区-腾讯云 (tencent.com) Java学习笔记——实体类&#xff08;ENTITY&#xff0c;VO&#xff0c;DTO&#xff0c;BO&#xff09;_dto继承entity_路言汐的博客-CSDN博客 说清楚PO、DTO、VO、BO与使用场景_业务逻辑层p…

nvm 常用命令

记录一下常使用的nvm命令&#xff0c;希望对大家也有所帮助&#xff01;&#xff01;&#xff01; nvm 帮助 nvm --help 版本查询 nvm -v 查看可用node版本 nvm list 下载最新node nvm install node 使用v21.0.0版本的node nvm use v21.0.0 切换node默认版本 nvm alias …

我试图扯掉这条 SQL 的底裤。只能扯一点点,不能扯多了

之前不是写分页嘛,分页肯定就要说到 limit 关键字嘛。 然后我啪的一下扔了一个链接出来: https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html 这个链接就是 MySQL 官方文档,这一章节叫做“对 Limit 查询的优化”,针对 limit 和 order by 组合的场景进行了较…

【MySQL】存储引擎

存储引擎 查看存储引擎设置表的存储引擎创建表时指定存储引擎修改表的存储引擎 引擎介绍InnoDB引擎: 具备外键支持的十五存储引擎MyISAM引擎: 主要的非事务处理存储引擎Archive引擎: 用于数据存档Blackhole引擎: 丢弃写操作,读操作返回空内容CSV引擎: 读取数据时,以逗号分隔各个…

软件设计原则-依赖倒置原则讲解以及代码示例

依赖倒置原则 一&#xff0c;介绍 1.前言 依赖倒置原则&#xff08;Dependency Inversion Principle&#xff0c;DIP&#xff09;是面向对象设计中的一个重要原则&#xff0c;由Robert C. Martin提出。 依赖倒置原则的核心思想是&#xff1a;高层模块不应该依赖于低层模块&…

aiohttp ssl.SSLError: [SSL: SSLV3_ALERT_HANDSHAKE_FAILURE] 错误处理

这个问题原因吧其实就是3.10 开始官网更新了TLS 堆栈默认安全设置 感兴趣的可以看下链接 python官网叙述: Python 3.10 增加了 TLS 堆栈的默认安全设置 解决也很简单&#xff0c;将ssl安全等级降下来就行&#xff0c;例如&#xff1a; import ssl import aiohttp ctx ssl.cr…