统计单词出现次数--hash表,二叉树,标准库

我一直很喜欢用标准库,非常非常方便。但是有时候你根本不知道它为你做了什么,我喜欢知道自己写的程序每一步都干了什么!!!

// / //标准模板库 // /* #include <time.h> #include <map> #include <string> #include <fstream> #include <iostream> using namespace std; int main() { double beginTime = clock(); ifstream in("source.txt"); map<string, int> M; map<string, int>::iterator j; string t; while( in >> t ) { M[t]++; } for ( j = M.begin(); j != M.end(); j++) { cout << j->first << " " << j->second << endl; } double endTime = clock(); cout << "time: " << endTime - beginTime << "ms " << endl; return 0; } */ // / // 编程珠玑十五章,珍珠字符串 //用Hash表实现之 // /* #include <time.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #define NHASH 29989 #define MULT 31 typedef struct node *nodeptr; typedef struct node { char *word; int count; nodeptr next; }node; nodeptr bin[NHASH]; unsigned int hash( char *p) { unsigned int h = 0; for( ; *p; p++) { h = MULT * h + *p; } return h % NHASH; } void incword( char *s) { int h = hash( s ); nodeptr p = NULL; for ( p = bin[h]; p != NULL; p = p->next) { if ( strcmp( s, p->word) == 0) { p->count ++; return; } } p = NULL; p = new node; if ( p == NULL) { return; } p->count = 1; p->word = new char[ strlen(s) + 1]; strcpy( p->word, s); p->next = bin[h]; bin[h] = p; } int main() { double beginTime = clock(); int i = 0; FILE *fp = NULL; fp = fopen("source.txt","r"); if ( fp == NULL ) { printf("文件打开错误"); return 1; } //ifstream in("source.txt"); for ( i = 0; i < NHASH; i++) { bin[i] = NULL; } char tempWord[100]; while( fscanf(fp, "%s", tempWord) != EOF ) { incword( tempWord ); } nodeptr p = NULL; for ( i = 0; i < NHASH; i++) { p = NULL; for ( p = bin[i]; p != NULL; p = p->next) { //cout << p->word<< " " << p->count << endl; printf("%s %d\n", p->word, p->count); } } double endTime = clock(); printf("time:%g ms", endTime - beginTime ); return 0; } */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> typedef struct node *nodeptr; typedef struct node { char *word; int count; nodeptr lchild; nodeptr rchild; }node; nodeptr setUp( nodeptr root, char *s) { if ( root == NULL ) { root = (nodeptr)malloc( sizeof(node)); root->word = (char *)malloc(strlen(s) + 1); strcpy( root->word, s); root ->lchild = NULL; root ->rchild = NULL; root->count = 1; return root; } int flag = strcmp( root->word, s); if( !flag) { root ->count ++; return root; } else if ( flag < 0 ) { root ->rchild = setUp( root->rchild, s); } else { root->lchild = setUp(root->lchild, s); } return root; } void Traverse( nodeptr root) { if ( root != NULL) { if ( root->rchild != NULL) { Traverse(root->rchild); } printf("%s %d\n", root->word, root->count); if ( root->lchild != NULL) { Traverse(root->lchild); } } return ; } int main() { nodeptr root = NULL; double beginTime = clock(); int i = 0; FILE *fp = NULL; fp = fopen("source.txt","r"); if ( fp == NULL ) { printf("文件打开错误"); return 1; } char tempWord[100]; while( fscanf(fp, "%s", tempWord) != EOF ) { root = setUp(root, tempWord ); } Traverse(root); double endTime = clock(); printf("time:%g ms", endTime - beginTime ); return 0; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答关于使用哈希实现单词检查的问题。哈希是一种常用的数据结构,它可以快速地插入、查找和删除数据。在单词检查中,我们可以使用哈希来存储所有合法的单词,然后检查输入的文本中是否存在非法单词。 具体实现方法如下: 1. 构建一个空的哈希,并将所有合法的单词插入其中。每个单词作为一个键,对应的值可以是任意非空的值。 2. 对于输入的文本,将其按照空格分割成单词,然后逐个检查每个单词是否存在于哈希中。如果存在,则该单词是合法的;否则,该单词是非法的。 3. 如果需要统计非法单词的数量,可以在第二步中记录非法单词的个数。 需要注意的是,在实际开发中,还需要考虑以下问题: 1. 哈希的大小如何确定?如果哈希太小,可能会导致哈希冲突,从而影响检索速度;如果哈希太大,可能会浪费内存空间。 2. 如何处理单词中的大小写、标点符号等问题?一种解决方法是将所有单词转换为小写,并去除标点符号。 3. 如何处理单词的变形形式?例如,单数和复数形式、动词的各种时态等。一种解决方法是将所有单词都转换为其基本形式,例如将动词转换为原形,将名词转换为单数形式等。 希望这些信息能够帮助您理解如何使用哈希实现单词检查。如果您有任何问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值