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;
}
相关推荐
凳子(刘博浩)4 小时前
决策树的笔记
笔记
喜欢你,还有大家6 小时前
Linux笔记7——shell编程基础-1
linux·运维·笔记
山中月侣7 小时前
Java多线程编程——基础篇
java·开发语言·经验分享·笔记·学习方法
m0_678693338 小时前
深度学习笔记34-YOLOv5调用官方权重进行检测
笔记·深度学习·yolo
AI视觉网奇11 小时前
zsh 使用笔记 命令行智能提示 bash智能
linux·运维·笔记
于越海12 小时前
Python工程师向项目管理转型的深度分析与学习道路规划
笔记·python·学习
风和日丽 随波逐流15 小时前
java17学习笔记
笔记·学习
ReedFoley1 天前
【笔记】动手学Ollama 第五章 Ollama 在 LangChain 中的使用 - Python 集成
笔记·langchain
Mr Sorry1 天前
Non-stationary Diffusion For Probabilistic Time Series Forecasting论文阅读笔记
论文阅读·笔记
南猿北者1 天前
Cmake学习笔记
笔记·学习·策略模式