-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3_Stringhash.cpp
More file actions
239 lines (229 loc) · 5.58 KB
/
3_Stringhash.cpp
File metadata and controls
239 lines (229 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Name: 任务3 找出现次数最多的单词
Copyright:
Author: lizhimin
Date: 24/05/17 20:25
Description: String Hash
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cstdlib>
#define Debug_Switch 0
using namespace std;
int maxnum=0;
char str2low(char ch)
{
if ('A'<=ch && ch<='Z') return (ch+32);
if ('a'<=ch && ch<='z') return ch;
return (' ');
}
int getPrimeNumber(int num)
{
if(num <= 0)
{
return -1;
}
int* primeArr = new int[num + 1];
int* visitArr = new int[num + 1];
memset(primeArr , 0 , sizeof(primeArr) * (num + 1));
memset(visitArr , 0 , sizeof(visitArr) * (num + 1));
int k;
for(int i = 2 ; i <= num ; i++ )
{
k = i;
while(k * i <= num)
{
if(0 == visitArr[i])
{
visitArr[k * i] = 1;
primeArr[k * i] = 1;
}
k++;
}
}
int i;
for(i = num - 1; i >= 2 ; i--)
{
if(0 == primeArr[i])
{
break;
}
}
delete[] primeArr;
delete[] visitArr;
return i;
}
const int MULT = 31;
typedef struct Node
{
Node():_next(NULL),_word(""),_count(0),_isNULL(true){}
void setNULL(bool flag)
{
_isNULL = flag;
}
Node* _next;
string _word;
int _count;
bool _isNULL;
}Node;
int getHash(char* str , int primeNum)
{
unsigned int hashValue = 0;
if(NULL == str || primeNum < 2)
{
return hashValue;
}
for( ; *str ; str++)
{
char ch = *str;
hashValue = MULT * hashValue + ch;
}
return (hashValue % primeNum);
}
void countWords(Node* hashTable, int primeNum , vector<string> words)
{
if(NULL == hashTable || primeNum < 2 || words.empty())
{
return;
}
//对每个单词生成链表
int size = words.size();
string sWord;
char* word;
int hashIndex;
Node* node;
for(int i = 0 ; i < size ; i++)
{
sWord = words.at(i);
word = (char*)sWord.c_str();
hashIndex = getHash(word , primeNum);
if(hashTable[hashIndex]._isNULL)
{
Node* newNode = new Node();
newNode->_word = sWord;
newNode->_isNULL = false;
newNode->_count = 1;
hashTable[hashIndex] = *newNode;
}
else
{
bool isFind = false;
Node* previouNode = NULL;
for(node = &hashTable[hashIndex] ; node != NULL ; )
{
//如果找到该单词
if( node->_word == sWord)
{
node->_count++;
isFind = true;
break;
}
previouNode = node;
node = node->_next;
}
if(!isFind)
{
Node* newNode = new Node();
newNode->_word = sWord;
newNode->_isNULL = false;
newNode->_count = 1;
previouNode->_next = newNode;
}
}
}
}
void releaseHashTable(Node* hashTable , int size)
{
if(NULL == hashTable || size <= 0)
{
return;
}
for(int i = 0 ; i < size ; i++)
{
Node* node = &hashTable[i];
while(node)
{
Node* nextNode = node->_next;
delete node;
node = nextNode;
}
}
delete[] hashTable;
}
void print(Node* hashTable , int size)
{
for(int i = 0 ; i < size ; i++)
{
Node* node = &hashTable[i];
if(NULL == node || node->_isNULL )
{
continue;
}
while(node)
{
if (maxnum< node->_count)
maxnum=node->_count;
if (Debug_Switch)
cout << node->_word << ":" << node->_count <<endl;
node = node->_next;
}
}
return;
}
void travel(Node* hashTable , int size)
{
for(int i = 0 ; i < size ; i++)
{
Node* node = &hashTable[i];
if(NULL == node || node->_isNULL )
{
continue;
}
while(node)
{
if (maxnum== node->_count)
cout << node->_word << ":" << node->_count <<endl;
node = node->_next;
}
}
cout << endl;
}
void process()
{
int wordNum;
int max=0;
vector<string> words;
string word;
while(cin >> wordNum)
{
for(int i = 0 ; i < wordNum ; i++)
{
cin >> word;
for (int j=0;j<word.size();j++)
if (str2low(word[j])==' ')
word[j]='\0';
if (word[0]!='\0')
{
if (Debug_Switch)
cout<<word<<endl;
words.push_back(word);
}
}
int primeNum = getPrimeNumber(wordNum);
Node* hashTable = new Node[primeNum];
countWords(hashTable , primeNum , words);
print(hashTable , primeNum);
travel(hashTable , primeNum);
releaseHashTable(hashTable , primeNum);
}
}
int main(int argc , char* argv[])
{
freopen("input.txt","r",stdin);
freopen("output.txt","w+",stdout);
process();
fclose(stdin); fclose(stdout);
return 0;
}