相似度计算 第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;
}
相关推荐
佚名涙14 分钟前
go中锁的入门到进阶使用
开发语言·后端·golang
猫猫的小茶馆15 分钟前
【PCB工艺】软件是如何控制硬件的发展过程
开发语言·stm32·单片机·嵌入式硬件·mcu·51单片机·pcb工艺
勘察加熊人1 小时前
wpf+c#路径迷宫鼠标绘制
开发语言·c#·wpf
小黄人软件2 小时前
C# ini文件全自动界面配置:打开界面时读ini配置到界面各控件,界面上的控件根据ini文件内容自动生成,点保存时把界面各控件的值写到ini里。
开发语言·c#
二进制人工智能3 小时前
【QT5 网络编程示例】TCP 通信
网络·c++·qt·tcp/ip
Android洋芋5 小时前
C语言深度解析:从零到系统级开发的完整指南
c语言·开发语言·stm32·条件语句·循环语句·结构体与联合体·指针基础
bjxiaxueliang5 小时前
一文详解QT环境搭建:Windows使用CLion配置QT开发环境
开发语言·windows·qt
莫有杯子的龙潭峡谷5 小时前
3.31 代码随想录第三十一天打卡
c++·算法
Run_Teenage5 小时前
C语言 【初始指针】【指针一】
c语言·开发语言
苹果.Python.八宝粥6 小时前
Python第七章02:文件读取的练习
开发语言·python