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;
}
相关推荐
EchoL、26 分钟前
Obsidian使用学习
笔记·学习
XFF不秃头1 小时前
力扣刷题笔记-旋转图像
c++·笔记·算法·leetcode
铜豌豆_Y2 小时前
【实用】GDB调试保姆级教程|常用操作|附笔记
linux·c语言·驱动开发·笔记·嵌入式
wdfk_prog3 小时前
[Linux]学习笔记系列 -- [fs]file
linux·笔记·学习
其美杰布-富贵-李4 小时前
Python 反射完整学习笔记
笔记·python·学习·反射
charlie1145141914 小时前
快速在WSL上开发一般的C++上位机程序
开发语言·c++·笔记·学习·环境配置·工程
jimmyleeee4 小时前
人工智能基础知识笔记二十八:几款有用的LLM管理工具
人工智能·笔记·python
客梦5 小时前
数据结构-线性表
数据结构·笔记
W.KN6 小时前
关于论文如何开始的学习笔记
人工智能·笔记·学习
Lv11770086 小时前
Visual Studio中的接口
ide·笔记·c#·visual studio