CCF20240302——相似度计算

CCF20240302------相似度计算



代码如下:

javascript 复制代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX_WORD_LEN 100
#define MAX_WORDS 10000

int main() {
    int n, m;
    scanf("%d %d", &n, &m);

    char words1[MAX_WORDS][MAX_WORD_LEN];
    char words2[MAX_WORDS][MAX_WORD_LEN];
    char temp_word[MAX_WORD_LEN];

    int size1 = 0, size2 = 0;

    // 读取第一篇文章的单词
    for (int i = 0; i < n; i++) {
        scanf("%s", temp_word);
   
        // 转换为小写
        for (int j = 0; temp_word[j]; j++) {
            if (temp_word[j] >= 'A' && temp_word[j] <= 'Z') {
                temp_word[j] += 'a' - 'A';
            }
        }

        // 检查是否已经在words1中
        int found = 0;
        for (int j = 0; j < size1; j++) {
            if (strcmp(temp_word, words1[j]) == 0) {
                found = 1;
                break;
            }
        }
        if (!found) {
            strcpy(words1[size1++], temp_word);
        }
    }

    // 读取第二篇文章的单词
    for (int i = 0; i < m; i++) {
        scanf("%s", temp_word);

        // 转换为小写
        for (int j = 0; temp_word[j]; j++) {
            if (temp_word[j] >= 'A' && temp_word[j] <= 'Z') {
                temp_word[j] += 'a' - 'A';
            }
        }

        // 检查是否已经在words2中
        int found = 0;
        for (int j = 0; j < size2; j++) {
            if (strcmp(temp_word, words2[j]) == 0) {
                found = 1;
                break;
            }
        }
        if (!found) {
            strcpy(words2[size2++], temp_word);
        }
    }

    int intersection_count = 0;
    int union_count = size1;

    // 计算交集
    for (int i = 0; i < size2; i++) {
        int found = 0;
        for (int j = 0; j < size1; j++) {
            if (strcmp(words2[i], words1[j]) == 0) {
                found = 1;
                break;
            }
        }
        if (found) {
            intersection_count++;
        } else {
            union_count++;
        }
    }

    printf("%d\n", intersection_count);
    printf("%d\n", union_count);

    return 0;
}
相关推荐
优雅的潮叭18 小时前
c++ 学习笔记之 chrono库
c++·笔记·学习
不大姐姐AI智能体19 小时前
搭了个小红书笔记自动生产线,一句话生成图文,一键发布,支持手机端、电脑端发布
人工智能·经验分享·笔记·矩阵·aigc
浅念-21 小时前
C语言小知识——指针(3)
c语言·开发语言·c++·经验分享·笔记·学习·算法
burning_maple21 小时前
mysql数据库笔记
数据库·笔记·mysql
hkNaruto21 小时前
【AI】AI学习笔记:LangGraph 与 LangChain的关系以及系统性学习路线选择
笔记·学习·langchain
jrlong21 小时前
DataWhale大模型基础与量化微调task3学习笔记(第 5章:深入大模型架构_MoE 架构解析)
笔记·学习
wdfk_prog1 天前
[Linux]学习笔记系列 --[drivers][base]map
linux·笔记·学习
浅念-1 天前
链表经典面试题目
c语言·数据结构·经验分享·笔记·学习·算法
啥都会点的大秀1 天前
声学仿真学习笔记
笔记·学习
不会代码的小猴1 天前
Linux环境编程第三天笔记
linux·笔记