当前位置: 首页 > ai >正文

【PTA数据结构 | C语言版】我爱背单词

本专栏持续输出数据结构题目集,欢迎订阅。

文章目录

    • 题目
    • 代码

题目

作为一个勤奋的学生,你在阅读一段英文文章时,是否希望有个程序能自动帮你把没有背过的生词列出来?本题就请你实现这个程序。

输入格式:
输入第 1 行给出 1 个正整数 n(2≤n≤10^3),为已经背下来的单词的数量。
接下来输入的每行是不超过 20 个字符的、仅由小写英文字母组成的单词。题目保证没有重复的单词。

最后是一段整理好的英文文章,文章仅包含不超过 20 个字符的、仅由小写英文字母组成的单词,单词间以 1 个空格分隔。文章末尾以符号 # 表示结束,这个符号不属于文章的内容。题目保证文章中至少有 1 个生词,且全文一共包含不超过 10^3 个单词。

输出格式:
找出每个没有背过的生词,按照其在文章中出现的顺序输出,每行输出一个生词。注意:每个生词只输出一遍,不要重复输出。

输入样例:
5
a
your
is
case
correct
this is a test case that test the correctness of your program #

输出样例:
this
test
that
the
correctness
of
program

代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>#define MAX_WORD_LENGTH 21  // 最大单词长度 + 1
#define MAX_VOCABULARY 1000 // 最大词汇量
#define HASH_TABLE_SIZE 2003 // 哈希表大小,使用质数减少冲突// 哈希表节点结构
typedef struct Node {char word[MAX_WORD_LENGTH];struct Node* next;
} Node;Node* hashTable[HASH_TABLE_SIZE];// 简单哈希函数
unsigned int hash(const char* str) {unsigned int hashval = 0;for (int i = 0; str[i] != '\0'; i++)hashval = str[i] + (hashval << 6) + (hashval << 16) - hashval;return hashval % HASH_TABLE_SIZE;
}// 插入单词到哈希表
void insert(const char* word) {unsigned int index = hash(word);Node* newNode = (Node*)malloc(sizeof(Node));strcpy(newNode->word, word);newNode->next = hashTable[index];hashTable[index] = newNode;
}// 检查单词是否在哈希表中
int contains(const char* word) {unsigned int index = hash(word);Node* current = hashTable[index];while (current != NULL) {if (strcmp(current->word, word) == 0)return 1;current = current->next;}return 0;
}// 检查单词是否已在生词列表中
int isNewWord(const char**生词列表, int 生词数量, const char* word) {for (int i = 0; i < 生词数量; i++) {if (strcmp(生词列表[i], word) == 0)return 0;}return 1;
}int main() {int n;char word[MAX_WORD_LENGTH];const char** newWords = NULL;int newWordCount = 0;// 初始化哈希表for (int i = 0; i < HASH_TABLE_SIZE; i++) {hashTable[i] = NULL;}// 读取已背单词scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%s", word);insert(word);}// 读取文章并处理while (scanf("%s", word) == 1) {if (word[0] == '#') break; // 遇到结束符号// 检查是否为生词if (!contains(word) && isNewWord(newWords, newWordCount, word)) {// 动态扩容生词列表newWords = (const char**)realloc(newWords, (newWordCount + 1) * sizeof(const char*));char* newWord = (char*)malloc((strlen(word) + 1) * sizeof(char));strcpy(newWord, word);newWords[newWordCount++] = newWord;}}// 输出结果for (int i = 0; i < newWordCount; i++) {printf("%s\n", newWords[i]);}return 0;
}
http://www.xdnf.cn/news/15688.html

相关文章:

  • 五分钟掌握 TDengine 数据文件的工作原理
  • 鸿蒙开发--端云一体化--云对象
  • C++ 程序设计考量表
  • 人工智能day9——模块化编程概念(模块、包、导入)及常见系统模块总结和第三方模块管理
  • SGLang 推理框架核心组件解析:请求、内存与缓存的协同工作
  • mpiigaze的安装过程一
  • 美团闪购最新版 mtgsig1.2
  • 语音大模型速览(三)- cosyvoice2
  • Maven学习总结(62)—— Maven 打包瘦身和提速解决方案
  • 应急响应-Windows资源监视器
  • HTTPie: 开发者友好的http客户端工具
  • 深度学习零基础入门(3)-图像与神经网络
  • 读书笔记(学会说话)
  • 嵌入式系统内核镜像相关(十六)
  • 数据查找 二叉查找树
  • # Redis-stable 如何在Linux系统上安装和配置
  • java常见的jvm内存分析工具
  • C语言-一维数组,二维数组
  • 菱形继承 虚继承
  • 快速安装GitLab指南
  • go安装使用gin 框架
  • web3 区块链技术与用
  • 【论文精读】基于共识的分布式量子分解算法用于考虑最优传输线切换的安全约束机组组合
  • Django母婴商城项目实践(五)- 数据模型的搭建
  • UniApp TabBar 用户头像方案:绕过原生限制的实践
  • Selenium 攻略:从元素操作到 WebDriver 实战
  • STM32之L298N电机驱动模块
  • 【iOS】MRC与ARC
  • Fish Speech:开源多语言语音合成的革命性突破
  • 伺服电机与步进电机要点详解