当前位置: 首页 > article >正文

202403-02-相似度计算 csp认证

在这里插入图片描述

其实这个问题就是求两篇文章的词汇的交集和并集,首先一说到并集,我就想到了set集合数据结构,set中的元素必须唯一。
STL之set的基本使用–博客参考

  • 所以将两个文章的词汇全部加入set中,并求出set的大小,即为并集的大小。
#include <iostream>
#include <string>
#include <set>using namespace std;void toupper(string &str)
{for (int i = 0; i < str.size(); i++){if (str[i] >= 'a' && str[i] <= 'z'){str[i] = str[i] - ('a' - 'A');}}
}int main() {int n, m;cin >> n >> m;string word;set<string> first_set;  // 存储第一篇文章的单词set<string> union_set;  // 存储并集// 读取第一篇文章for(int i = 0; i < n; i++){cin >> word;toupper(word);first_set.insert(word);union_set.insert(word);} int intersection = 0;  // 交集数量// 读取第二篇文章for(int i = 0; i < m; i++){cin >> word;toupper(word);// 判断是否在第一篇文章中出现过if(first_set.find(word) != first_set.end()){intersection++;first_set.erase(first_set.find(word)); // 在第一篇文章的set中删除,这样第二篇文章中出现 连续两个the的时候只会统计一次 }// 加入并集union_set.insert(word);} // 输出结果cout << "交集数量: " << intersection << endl;cout << "并集数量: " << union_set.size() << endl;system("pause");  // 仅用于调试环境,提交代码时建议移除return 0;
}

但是我一开始选用的是unordered_map。。。我也不知道为什么、

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
void toUpperCase(string &str)
{for (int i = 0; i < str.size(); i++){if (char(str[i]) >= 'a' && char(str[i])  'z'){str[i] = str[i] - (char('a') - 'A');}}
}void PrintMap(const unordered_map<string, int>& rd)
{cout << "------------------" << endl;for (auto i : word){cout << i.first << " " << i.second << dl;}}
int main()
{int n, m; // 两篇文章的单词个数cin >> n >> m;string word;unordered_map<string, int> nword;unordered_map<string, int> mword;unordered_map<string, int> mixed;for (int i = 0; i < n; i++){cin >> word;toUpperCase(word);nword[word] = 1;mixed[word]++;}int sum = 0; // 并集数量 for (int i = 0; i < m; i++){cin >> word;toUpperCase(word);mixed[word]++;if(nword[word] > 0 && mword[word] == 0){ // 如果在第一篇文章已经存在 而且是第二篇文章第一次读取到 sum++;}mword[word] = 1;}PrintMap(nword), PrintMap(mword);PrintMap(mixed);cout << sum << endl;cout << mixed.size() << endl;system("pause");return 0;
}//更加节省空间的方法 
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
void toUpperCase(string &str)
{for (int i = 0; i < str.size(); i++){if (char(str[i]) >= 'a' && char(str[i]) <= 'z'){str[i] = str[i] - (char('a') - 'A');}}
}void PrintMap(const unordered_map<string, int>& word)
{cout << "------------------" << endl;for (auto i : word){cout << i.first << " " << i.second << endl;}}
int main()
{int n, m; // 两篇文章的单词个数cin >> n >> m;string word;    unordered_map<string, int> mixed;for (int i = 0; i < n; i++){cin >> word;toUpperCase(word);mixed[word] = 1; // 表示在两篇文章中第一篇出现 }int sum = 0; // 并集数量 for (int i = 0; i < m; i++){cin >> word;toUpperCase(word);if(mixed[word] == 1){sum++;mixed[word] = 2; // 表现在第二篇出现 } }PrintMap(mixed);cout << sum << endl;cout << mixed.size() << endl;return 0;
}
http://www.lryc.cn/news/2401071.html

相关文章:

  • 【Oracle】游标
  • MySQL 中 char 与 varchar 的区别
  • DeepSeek 赋能智能零售,解锁动态定价新范式
  • 在Flutter中定义全局对象(如$http)而不需要import
  • <4>, Qt窗口
  • 6.04打卡
  • 【基于SpringBoot的图书购买系统】操作Jedis对图书图书的增-删-改:从设计到实战的全栈开发指南
  • Ubuntu中TFTP服务器安装使用
  • Spring Boot微服务架构(十):Docker与K8S部署的区别
  • 接口重试的7种常用方案!
  • vue3:Table组件动态的字段(列)权限、显示隐藏和左侧固定
  • pikachu靶场通关笔记13 XSS关卡09-XSS之href输出
  • MCP客户端Client开发流程
  • 学习日记-day21-6.3
  • C语言探索之旅:深入理解结构体的奥秘
  • uniapp 开发企业微信小程序,如何区别生产环境和测试环境?来处理不同的服务请求
  • Dockerfile常用指令介绍
  • Docker 容器化:核心技术原理与实践
  • 不确定性分析在LEAP能源-环境系统建模中的整合与应用
  • 经典算法回顾之最小生成树
  • Ubuntu下实现nginx反向代理
  • c++ QicsTable使用实例
  • 在WordPress上添加隐私政策页面
  • 二维 根据矩阵变换计算镜像旋转角度
  • 你工作中涉及的安全方面的测试有哪些怎么回答
  • 阿里云ACP云计算备考笔记 (3)——云服务器ECS
  • Eigen实现非线性最小二乘拟合 + Gauss-Newton算法
  • 区块链技术:原理、应用与发展趋势
  • 从零开始:用Tkinter打造你的第一个Python桌面应用
  • Web开发主流前后端框架总结