当前位置:   article > 正文

一篇文章中求出现频率最高的10个单词(C++实现tanglanting)_c++ 统计文件中每个单词出现的次数 最大10个

c++ 统计文件中每个单词出现的次数 最大10个

Write a program for displaying the ten most frequent words in a file such that your program should be efficient in all complexity measures.

 

Requirement: C/C++.

 

Submission: Source code and a document to describe your solution.

Source

思路:用hashmap 进行每个word进行记录,然后就可以在利用堆排序中找到10个最频率最高的。

1:记录每个单词的个数;

一种做法:

其实C++有提供hashmap来进行记录key_value进行存放。用法像:map

  二种做法:

   自己定义hash函数进行key_value的存放:

2

利用记录过的《单词,次数》进行排序;

第一种做法:每次找一个最大的,这样找10次那就效率是On*10;

第二种做法:用堆来进行:效率为On*log10)这个效率比较高一点。

 

 

下面是选用:自己定义hash函数进行key_value的存放:第二种做法:用堆来进行:效率为On*log10)这个效率比较高一点。下面代码是实现:代码的优化还有很大空间如:单词的分割只考虑空格,其它的方式先不考虑。

//dazhengshumore.cpp

 

C++

 

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <fstream>
  6. #include <sstream>
  7. #include <stdexcept>
  8. #define MAX 10000
  9. #define M 10
  10. #define DEBUG
  11. using namespace std;
  12. class node_C{
  13. private: char *data;
  14. int coutnum;
  15. public :
  16. node_C():data(NULL),coutnum(0){};
  17. node_C(const char *t):coutnum(0){
  18. data=new char[strlen(t)+1];
  19. strcpy(data,t);
  20. };
  21. node_C(const node_C &node_t){
  22. if(node_t.data==NULL)
  23. return ;
  24. data=new char[strlen(node_t.data)+1];
  25. strcpy(data,node_t.data);
  26. coutnum=node_t.coutnum;
  27. }
  28. node_C & operator =(const node_C & node_t){
  29. if(this==&node_t)
  30. return *this;
  31. node_C temp=node_C(node_t);
  32. char *temp_t=temp.data;
  33. temp.data=data;
  34. data=temp_t;
  35. coutnum=temp.coutnum;
  36. }
  37. bool operator ==(const node_C &node_t){
  38. if(this==&node_t) return true;
  39. if((node_t.coutnum==coutnum)&&(strcmp(node_t.data,data)==0))
  40. return true;
  41. return false;
  42. }
  43. int operator++(){
  44. return ++coutnum;
  45. }
  46. int operator++(int){
  47. int temp=coutnum;
  48. coutnum++;
  49. return temp;
  50. }
  51. void display(){
  52. cout<<data<<" "<<coutnum<<endl;
  53. }
  54. int get_coutnum(){
  55. return coutnum;
  56. }
  57. void set_coutnum(int s_c){
  58. coutnum=s_c;
  59. }
  60. char *get_data(){
  61. return data;
  62. }
  63. void set_data(char *s_d){
  64. data=s_d;
  65. }
  66. ~node_C(){
  67. delete []data;
  68. }
  69. };
  70. //typedef struct node{
  71. // char *data;
  72. //int coutnum;
  73. //}node;
  74. unsigned int simple_hash(const char *str)
  75. {
  76. register unsigned int hash;
  77. register const char *p;
  78. for(hash = 0, p =str; *p ; p++)
  79. hash = 31 * hash + *p;
  80. return (hash & 0x7FFFFFFF)%MAX;
  81. }
  82. //读单词文件,并把它列出来
  83. ifstream& open_file(ifstream &in,const string &file){
  84. in.close();
  85. in.clear();
  86. in.open(file.c_str());
  87. return in;
  88. }
  89. class MakeminHeap{
  90. public:
  91. MakeminHeap(node_C *arr,int len);
  92. void MinHeapFixdown(node_C a[], int i, int n);
  93. void displayHead();
  94. void justHead(node_C *arr);
  95. ~MakeminHeap(){delete []counum;}
  96. private:
  97. node_C *counum;
  98. int num_len;
  99. int arr_i;
  100. };
  101. MakeminHeap::MakeminHeap(node_C *arr,int len):num_len(len){
  102. int i;
  103. arr_i=0;
  104. counum=new node_C[len];
  105. for(i=0;i<M;i++,arr_i++){
  106. while(arr_i<MAX){
  107. if(arr[arr_i].get_data()!=NULL) {
  108. counum[i]=arr[arr_i];
  109. break;}
  110. else arr_i++;
  111. }
  112. }
  113. for (i = len / 2 - 1; i >= 0; i--)
  114. MinHeapFixdown(counum, i, len);
  115. }
  116. void MakeminHeap::MinHeapFixdown(node_C a[], int i, int n)
  117. {
  118. int j, temp;
  119. node_C temp_n;
  120. temp_n= a[i];
  121. j = 2 * i + 1;
  122. while (j < n)
  123. {
  124. if (j + 1 < n && a[j + 1].get_coutnum() < a[j].get_coutnum()) //在左右孩子中找最小的
  125. j++;
  126. if (a[j].get_coutnum() >= temp_n.get_coutnum())
  127. break;
  128. // a[i] = a[j]; //把较小的子结点往上移动,替换它的父结点
  129. a[i]=a[j];
  130. i = j;
  131. j = 2 * i + 1;
  132. }
  133. a[i]=temp_n;
  134. }
  135. void MakeminHeap::justHead(node_C *arr){
  136. int i;
  137. for(i=arr_i;i<MAX;i++){
  138. if(arr[i].get_coutnum()>counum[0].get_coutnum()){
  139. counum[0]=arr[i];
  140. MinHeapFixdown(counum, 0, num_len);
  141. }
  142. }
  143. }
  144. void MakeminHeap::displayHead(){
  145. int i;
  146. for(i=0;i<num_len;i++)
  147. cout<<"<"<<counum[i].get_data()<<">"<<" has currented:"<<counum[i].get_coutnum()<<"times "<<endl;
  148. cout<<endl;
  149. }
  150. int main(int argc, char **argv)
  151. {
  152. int i;
  153. ifstream map_file;
  154. node_C *arr=new node_C[MAX];
  155. if(argc!=2)
  156. throw runtime_error("wrong number of argument");
  157. if(!open_file(map_file,argv[1])) throw runtime_error("wrong open of file");
  158. string word,line;
  159. while(getline(map_file,line)){
  160. istringstream stream(line);
  161. while(stream>>word)
  162. {
  163. //cout<<word<<" ";//word 的string以空格为结尾。
  164. const char *wor_temp=word.c_str();
  165. unsigned int temp=simple_hash(wor_temp);
  166. while(arr[temp].get_data()!=NULL){
  167. if(strcmp(arr[temp].get_data(),word.c_str())==0)
  168. break;
  169. else
  170. temp++;
  171. }
  172. if(arr[temp].get_data()==NULL) {
  173. // char *arr_t=new char[strlen(wor_temp)+1];
  174. //strcpy(arr_t,wor_temp);
  175. //arr[temp].set_data(arr_t);
  176. node_C temp_node=node_C(wor_temp);
  177. arr[temp]=temp_node;
  178. }
  179. // ++arr[temp];//有点不规范!
  180. arr[temp].set_coutnum(arr[temp].get_coutnum()+1);
  181. }
  182. }
  183. #ifdef DEBUG
  184. for(i=0;i<MAX;i++)//进行对于arr进行堆的排序提取全10位
  185. if(arr[i].get_data()!=NULL)
  186. cout<<"<"<<arr[i].get_data()<<">"<<" have currented:"<<arr[i].get_coutnum()<<"times "<<endl;
  187. cout<<"*************************************************"<<endl;
  188. #endif
  189. MakeminHeap tempHeap(arr,M);
  190. //tempHeap.displayHead();
  191. tempHeap.justHead(arr);
  192. cout<<"display the ten most frequent words:"<<endl;
  193. tempHeap.displayHead();
  194. return 0;
  195. }


 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/864126
推荐阅读
相关标签
  

闽ICP备14008679号