语言学家需要分析若干本英文小说里面的单词出现的频率,这些文件已经存储在文本文件中,你需要按字典编排顺序输出有多少单词,以及每个单词出现的次数。
设计要求:
①初步正确性测试的单词量在100以内,压力测试数据量达到100万单词以上。
②字母不区分大小写,不考虑以破折号或者空格分开的单词
③单词长度不限,以测试集中出现的单词长度为准
④提供查询词频的接口,能够在O(m)的时间复杂度内查询出结果,其中m为单词长度
源代码
#include<stdio.h>
#include <string.h>
#include<stdlib.h>
static int sum=0;
typedef struct word_node {
char s;
word_node *nextChar[26];
int count;
}word_node,*word_tree;
//遍历字符串,建立字典树
void addNode(word_tree &w_node, char *str, int &index)
{
if (w_node == NULL) {
w_node = (word_tree)malloc(sizeof(word_node));
for (int i = 0; i < 26; i++)
w_node->nextChar[i] = NULL;
w_node->count = 0;
}
w_node->s = str[index];
index++;
if (str[index] >= 'a'&&str[index] <= 'z')
addNode(w_node->nextChar