厦大C语言上机 1382 小明的生词本
2018-01-11 10:24
876 查看
1382.小明的生词本
时间限制: 1000 MS 内存限制: 65536 K
提交数: 2 (0 users) 通过数: 0 (0 users)
问题描述
小明知道自己的单词量远远不及他人,于是他想编程给自己建一个生词本,每次遇到新的单词的时候就加入到自己的生词本中。不过由于有时记性不好,本来已经存在生词本中的单词又被小明当成生词加入到了生词本中,对于这种情况,并不需要再给这个单词在分配一块内存来存放数据,也就是说,保证生词本中的单词之间的相异性。再加入一系列单词后,小明想知道自己的生词本中有多少个单词。
输入格式
第一行为一个正整数N,0 < N <= 1000,N代表小明加入的单词总数
接下来N行,每一行都是由小写字母组成的单词,单词长度不超过10
输出格式
输出生词本中的生词总数M
样例输入
9
the
quick
brown
fox
jumps
over
the
lazy
dog
样例输出
8
来源
xmu
时间限制: 1000 MS 内存限制: 65536 K
提交数: 2 (0 users) 通过数: 0 (0 users)
问题描述
小明知道自己的单词量远远不及他人,于是他想编程给自己建一个生词本,每次遇到新的单词的时候就加入到自己的生词本中。不过由于有时记性不好,本来已经存在生词本中的单词又被小明当成生词加入到了生词本中,对于这种情况,并不需要再给这个单词在分配一块内存来存放数据,也就是说,保证生词本中的单词之间的相异性。再加入一系列单词后,小明想知道自己的生词本中有多少个单词。
输入格式
第一行为一个正整数N,0 < N <= 1000,N代表小明加入的单词总数
接下来N行,每一行都是由小写字母组成的单词,单词长度不超过10
输出格式
输出生词本中的生词总数M
样例输入
9
the
quick
brown
fox
jumps
over
the
lazy
dog
样例输出
8
来源
xmu
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { char *word; struct Node *next; }; int insert(struct Node *vocabulary, char *word) { struct Node *p, *q, *r; int cmp; p = vocabulary->next; q = vocabulary; while (p) { cmp = strcmp(p->word, word); if (cmp == 0) return 0; else if (cmp < 0) { q = p; p = p->next; } else { r = (struct Node *)malloc(sizeof(struct Node)); r->word = (char *)malloc(sizeof(char) * 15); strcpy(r->word, word); q->next = r; r->next = p; return 1; } } r = (struct Node *)malloc(sizeof(struct Node)); r->word = (char *)malloc(sizeof(char) * 15); strcpy(r->word, word); q->next = r; r->next = p; return 1; } int main() { int n, count = 0, i; char word[15] = { 0 }; struct Node *vocabulary; vocabulary = (struct Node *)malloc(sizeof(struct Node)); vocabulary->next = NULL; scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%s", word); count += insert(vocabulary, word); } printf("%d\n", count); return 0; }
相关文章推荐
- 厦大C语言上机 1365 小明的自娱自乐
- 厦大C语言上机 1358 小明的考题3――必杀技
- 厦大C语言上机 1389 简单的统计
- 厦大C语言上机 1387 进制转换进阶版
- 厦大C语言上机 1490 高级模式匹配
- 厦大C语言上机 1390 伪ackerman函数
- 厦大C语言上机 1388 高精度乘法
- 厦大C语言上机 1491 子串删除
- 厦大C语言上机 1391 简单的几何问题
- 厦大C语言上机 1492 破译时间密码
- 厦大C语言上机 1392 数组的平衡
- 厦大C语言上机 1401 寻找最大内角
- 厦大C语言上机 1493 数据查找
- 厦大C语言上机 1496 文件存数
- 厦大C语言上机 1393 哥德巴赫猜想的验证
- 厦大C语言上机 1402 第K早之日
- 厦大C语言上机 1494 简单链表
- 厦大C语言上机 1498 文件复制
- 厦大C语言上机 1394 抛硬币
- 厦大C语言上机 1413 模式匹配