CCF20240302——相似度计算
CCF20240302——相似度计算
代码如下:
#include <stdio.h>
#include <string.h>
#include <ctype.h>#define MAX_WORD_LEN 100
#define MAX_WORDS 10000int 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;
}