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;
}
相关推荐
丰锋ff40 分钟前
2013 年真题配套词汇单词笔记(考研真相)
笔记·学习·考研
小小程序媛(*^▽^*)1 小时前
第十二届全国社会媒体处理大会笔记
人工智能·笔记·学习·ai
铁手飞鹰2 小时前
VS2022创建项目工程笔记
c++·windows·笔记·visualstudio
Ethan learn English2 小时前
汽车零部件英语词汇 | 3000 最常用单词系列
笔记·学习·汽车·生活·英语·可理解性输入
聪明的笨猪猪3 小时前
Java Spring “核心基础”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
SccTsAxR3 小时前
[初学C语言]关于scanf和printf函数
c语言·开发语言·经验分享·笔记·其他
摇滚侠3 小时前
【IT老齐456】Spring Boot优雅开发多线程应用,笔记01
spring boot·redis·笔记
常州晟凯电子科技4 小时前
海思Hi3516CV610/Hi3516CV608开发笔记之环境搭建和SDK编译
人工智能·笔记·嵌入式硬件·物联网
弘毅 失败的 mian5 小时前
STM32 GPIO实战:LED与按键控制
经验分享·笔记·stm32·单片机·嵌入式硬件
Le1Yu7 小时前
2025-10-6学习笔记
java·笔记·学习