CCF20240302——相似度计算
CCF20240302——相似度计算
代码如下:
#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;
}
原文地址:https://blog.csdn.net/m0_60890170/article/details/139103238
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!