您的位置:首页 > 编程语言 > C语言/C++

厦大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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: