CCF/CSP認證-第33次-相似度計算

DawnTraveler發表於2024-05-24

1.題目

題目背景

兩個集合的 Jaccard 相似度定義為:
\(Sim(A,B)=\frac{|A\cap B|}{|A\cup B|}\)

\(\text{即交集的大小除以並集的大小。當集合 }A\text{ 和 }B\text{ 完全相同時,}Sim(A,B)=1\text{ 取得最大值;當二者交集為空時,}Sim(A,B)=0\text{ 取得最小值。}\)

題目描述

除了進行簡單的詞頻統計,小P還希望使用 Jaccard 相似度來評估兩篇文章的相似性。具體來說,每篇文章均由若干個英文單片語成,且英文單詞僅包含“大小寫英文字母”。對於
給定的兩篇文章,小P 首先需要提取出兩者的單詞集合\(A\)\(B\) ,即去掉各自重複的單詞。然後計算出:
\(\cdot|A\cap B|\),即有多少個不同的單詞同時出現在兩篇文章中;
\(\bullet\left|A\cup B\right|\),即兩篇文章一共包含了多少個不同的單詞。
最後再將兩者相除即可算出相似度。需要注意,在整個計算過程中應當忽略英文字母大小寫的區別,比如 the、The 和 THE 三者都應被視作同一個單詞。
試編寫程式幫助小P完成前兩步,計算出\(|A\cap B|\)\(|A\cup B|\);小P將親自完成最後一步的除法運算。

輸入格式

從標準輸入讀入資料。

輸入共三行。

輸入的第一行包含兩個正整數\(n\)\(m\),分別表示兩篇文章的單詞個數。
第二行包含空格分隔的\(n\)個單詞,表示第一篇文章; 第三行包含空格分隔的\(m\)個單詞,表示第二篇文章。

輸出格式

輸出到標準輸出。

輸出共兩行。

第一行輸出一個整數\(|A\cap B|\),即有多少個不同的單詞同時出現在兩篇文章中;
第二行輸出一個整數\(|A\cup B|\),即兩篇文章一共包含了多少個不同的單詞。

樣例1

樣例1輸入

3 2
The tHe thE
the THE

樣例1輸出

1
1

樣例1解釋

\[A=B=A\cap B=A\cup B=\text{\{the\}} \]

樣例2

樣例2輸入

9 7
Par les soirs bleus dete jirai dans les sentiers
PICOTE PAR LES BLES FOULER LHERBE MENUE

樣例2輸出

2
13

樣例2解釋

\(A=\) {bleus, dans, dete, jirai, les, par, sentiers, soirs} \(|A|=8\)
\(B=\) {bles, fouler, les, lherbe, menue, par, picote} \(|B|=7\)
\(A\cap B=\{\)les, par\(\}\mid A\cap B\mid=2\)

樣例3

樣例3輸入

15 15
Thou that art now the worlds fresh ornament And only herald to the gaudy spring
Shall I compare thee to a summers day Thou art more lovely and more temperate

樣例3輸出

4
24

子任務

\(80\%\)的測試資料滿足:\(n,m\leq100\)且所有字母均為小寫;
全部的測試資料滿足:\(n,m\leq10^4\)且每個單詞最多包含 10 個字母。

2.題解

2.1

思路

看題解即可理解

程式碼

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n, m;
    int allCnt = 0, commonCnt = 0;;
    cin >> n >> m;

    // 記錄第一篇文章中的不重複元素
    unordered_set<string> uset;
    for(int i = 0; i < n; i++){
        // 處理字串--小寫化
        string str;
        cin >>  str;
        for(char &ch : str){
            ch = tolower(static_cast<unsigned char>(ch));
        }
        // 如果出現不重複元素,總個數+1
        if(!uset.count(str)){
            uset.emplace(str);
            allCnt++;
        }
    }
    
    // 記錄第二篇文章中的不重複元素
    unordered_set<string> st;
    for(int i = 0; i < m; i++){
        // 處理字串--小寫化
    	string str;
    	cin >> str;
    	for(char &ch : str){
            ch = tolower(static_cast<unsigned char>(ch));
        }
        
        // 計算兩篇文章重複元素個數,但是同一元素不可多次重複!!! 
        if(uset.count(str) && !st.count(str)){
        	commonCnt++;
		}
		st.emplace(str); // 記錄元素,set並不記錄重複元素
		
	}
    // 總個數 += 第二篇文章不重複元素個數 - 與第一篇文章重複元素個數(即第二篇文章獨有的元素(且非重複))
	allCnt += st.size() - commonCnt; 
	cout << commonCnt << endl << allCnt;
}

2.2 使用現成函式

思路

先分別求出兩個字串的字串集合,然後使用set_intersection 和 set_union 分別求交集和並集即可。
set_intersection(setA.begin(), setA.end(), setB.begin(), setB.end(), inserter(intersection, intersection.begin()));
set_union(setA.begin(), setA.end(), setB.begin(), setB.end(), inserter(unionSet, unionSet.begin()));

程式碼

#include <iostream>
#include <sstream>
#include <set>
#include <string>
#include <algorithm>
using namespace std;

// 小寫化字串 
string toLowerCase(const string &str) {
    string result = str;
    transform(result.begin(), result.end(), result.begin(), ::tolower);
    return result;
}

set<string> extractWords(const string &text) {
    set<string> words;
    stringstream ss(text);
    string word;
	// 提取字串到set集合中 
    while (ss >> word) {
        words.insert(toLowerCase(word));
    }

    return words;
}

int main() {
    string text1, text2;
	int n, m;
	cin >> n >> m;
	cin.ignore(); 
    getline(cin, text1);
    getline(cin, text2);
    
    // 解析字串
    set<string> setA = extractWords(text1);
    set<string> setB = extractWords(text2);
    
    // 求集合交集
    set<string> intersection;
    set_intersection(setA.begin(), setA.end(), setB.begin(), setB.end(), inserter(intersection, intersection.begin()));
    
    // 求集合並集
    set<string> unionSet;
    set_union(setA.begin(), setA.end(), setB.begin(), setB.end(), inserter(unionSet, unionSet.begin()));

    cout << intersection.size() << endl;
    cout << unionSet.size() << endl;

    return 0;
}

相關文章