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;
}
相关推荐
客梦20 分钟前
数据结构-栈与队列
数据结构·笔记
TL滕35 分钟前
从0开始学算法——第四天(题目参考答案)
数据结构·笔记·python·学习·算法
齐生11 小时前
iOS 知识点 - Category / Extension / Protocol 小合集
笔记·面试
TL滕2 小时前
从0开始学算法——第四天(练点题吧)
数据结构·笔记·学习·算法
moringlightyn3 小时前
进程控制(程序替换+自定义Shell)
linux·服务器·c++·笔记·c·shell·进程
不想写笔记3 小时前
C语言 操作符(下)
c语言·笔记
汝生淮南吾在北3 小时前
SpringBoot+Vue在线笔记管理系统
java·vue.js·spring boot·笔记·毕业设计·毕设
风123456789~3 小时前
【OceanBase专栏】OB不同模式自增的实现
数据库·笔记·oceanbase
逗豆逗3 小时前
数字IC设计工程师的testbench.v文件和UVM环境
笔记·芯片设计
摇滚侠4 小时前
零基础小白自学 Git_Github 教程,git 命令行操作1,笔记18
笔记·git·github