相似度计算 第33次CCF-CSP计算机软件能力认证

https://www.acwing.com/problem/content/5723/

注意点:

1.集合去重单词

2.getline()最后回车需要忽视

3.一些bulit in函数 如下:

字符转小写

#include <cctype>

tolower(ch)

字符串转小写

#include <algorithm>

transform(str.begin(), str.end(), str.begin(), ::tolower)

字符串提取单词

#include <sstream>

string sentence = "This is a test string.";

istringstream iss(sentence); // 构造输入流

string word;

vector<string> words;

while (iss >> word) {

words.push_back(word); // 提取每个单词

}

寻找自定义分隔符

size_t start = 0, end;

while ((end = sentence.find(' ', start)) != std::string::npos) {

words.push_back(sentence.substr(start, end - start));

start = end + 1;

}

words.push_back(sentence.substr(start)); // 最后一个单词

#集合取交集并集

#include <algorithm>

set<int> result;

set_union(set1.begin(), set1.end(),

set2.begin(), set2.end(),

inserter(result, result.begin()));

set_intersection(set1.begin(), set1.end(),

set2.begin(), set2.end(),

inserter(result, result.begin()));

set_difference()(差集)

vector寻找元素

find(words2.begin(),words2.end(),word)!=words2.end()


Accept代码:

cpp 复制代码
#include<iostream>
#include<string>
#include<set>
#include<sstream>
#include <algorithm>
#include<vector>
using namespace std;
int n, m;
string transfer(string s) {
    string result;
    for (char c : s) {
        if (c >= 'A' && c <= 'Z') {
            result += (char)(c - 'A' + 'a');  // 转成小写
        }
        else {
            result += c;  // 小写字母或其他字符原样加
        }
    }
    return result;
}

int main() {
    //初始化
    string s1, s2;
    vector<string> words1, words2;
    string word;
    set<string> u,u1,u2;//并集

    //输入流
    cin >> n >> m;
    cin.ignore();
    getline(cin, s1);
    getline(cin, s2);
    istringstream iss1(s1);
    istringstream iss2(s2);
    while (iss1 >> word) {
        word = transfer(word);
        words1.push_back(word);
    }
    while (iss2 >> word) {
        word = transfer(word);
        words2.push_back(word);
    }

    //解决问题
    for (string word : words1) {
        u1.insert(word);
        u.insert(word);
    }
    for (string word : words2) {
        u2.insert(word);
        u.insert(word);
    }
    int ii = 0, uu = u.size();
    for (string word : u1) {
        if (u2.find(word) != u2.end()) {
            ii++;
        }
    }
    cout << ii << endl;
    cout << uu;
    return 0;
}
相关推荐
Boilermaker19926 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
MM_MS6 小时前
Halcon变量控制类型、数据类型转换、字符串格式化、元组操作
开发语言·人工智能·深度学习·算法·目标检测·计算机视觉·视觉检测
꧁Q༒ོγ꧂7 小时前
LaTeX 语法入门指南
开发语言·latex
njsgcs7 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
alonewolf_997 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
古城小栈7 小时前
Rust 迭代器产出的引用层数——分水岭
开发语言·rust
ghie90907 小时前
基于MATLAB的TLBO算法优化实现与改进
开发语言·算法·matlab
恋爱绝缘体17 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
wuk9987 小时前
VSC优化算法MATLAB实现
开发语言·算法·matlab
AI小怪兽7 小时前
基于YOLOv13的汽车零件分割系统(Python源码+数据集+Pyside6界面)
开发语言·python·yolo·无人机