-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.c
405 lines (264 loc) · 5.96 KB
/
hash.c
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include"hash.h"
//hash 1
int upTlw(char* str){
int i , m = 1, n = 1 , o = 1;
//Alternate lower to upper from i = 0
for (i = 0; str[i] != '\0'; i=i+2) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
m = (i+1) * str[i] + m;
}
//Alternate lower to upper from i = 1
for (i = 1; str[i] != '\0'; i=i+2) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
n = (i+1) * str[i] + n;
}
//upper to lower and lower to upper
for (i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32;
}
if (str[i] >='A' && str[i] <= 'Z'){
str[i] = str[i] + 32 ;
}
o = (i+1) * str[i] + o;
}
m = m * o + n * o + o * m * n;
if(m < 0){
m = -m;
}
if(m > 1000000){
m = m % 1000000;
}
return m;
}
//hash 2
//sum of characters present in the string
int sum(char * str , int length){
int sum = 0;
for(int i=0; i<length; i++){
sum = sum + str[i];
sum = sum ^ i;
}
return sum;
}
//multiplication of string character with its index number
int mult(char* str , int length){
int s=0;
for(int i=0; i<length; i++){
s = s + i*str[i] ;
}
return s;
}
//reverse multiplication of string character with its index number
int revmult(char*str , int length){
int s=0;
for(int i=0; i<length; i++){
s = s + i*str[length-i] ;
}
return s;
}
//multiplication of above three values
int multsumrev(char *str , int length){
int a = mult(str , length);
int b = sum(str , length);
int c = revmult(str , length);
a = a*b*c ;
if(a < 0){
a = -a;
}
if(a > 1000000){
a = a % 1000000;
}
return a;
}
//hash 3
//squaring and adding each character
int square(char*str , int length){
int s=0;
for(int i=0; i<length; i++){
s = s + str[i]*str[i]*i;
}
return s;
}
//xoring and multiplying with string character
int xor_mul(char*str , int length){
int s=0;
for(int i=0; i<length; i++){
s = s + s ^ (i+1)*str[i];
}
return s;
}
//shifting the and xoring number by its ascii character
int shifor(char*str , int length){
int s=0;
for(int i=0; i<length; i++){
s = s*i ^ str[i] + s>>str[i];
}
return s;
}
//getting value for hash3
int calc_hash3(char*str , int length){
int a = square(str , length);
int b = xor_mul(str , length);
int c = shifor(str , length);
a = a * b * c ;
a = a + length ;
if(a < 0){
a = -a;
}
if(a > 1000000){
a = a % 1000000;
}
return a;
}
//hash 4
//rotating number to left
int rot1(char*str , int length){
int a = 3123 ;
int b = 0 ;
for(int i = 0 ; i<length ; i++){
b = a << 11 + str[i] + b;
}
return b ;
}
//rotating number to right
int rot2(char*str , int length){
int a = 7301 ;
int b = 0 ;
for(int i = 0 ; i<length ; i++){
b = a >> 11 + str[i] + b;
}
return b ;
}
//shifting numbers
int func4(char*str , int length){
int a = rot1(str , length);
int b = rot2(str , length);
int c = 0 ;
while(c!=b){
a = (a | b) >> b ;
b = (a | b) << a ;
c++;
}
a = a ^ b;
if(a < 0){
a = -a;
}
if(a > 1000000){
a = a % 1000000;
}
return a;
}
//hash 5
//getting sum of prime numbers
int prime_sum(int x){
int i , j , k , s = 0;
for(j = 1; j < x; j++){
k = 0;
for(i = 2; i <= j/2 ; i++ ){
if(j % i == 0){
k = k + 1;
break;
}
}
if(k == 0 && j!=1){
s = s + j;
}
}
return s;
}
//getting hash value
int func5(char* str , int length){
int a = sum(str , length);
int b = prime_sum(a);
int c = (INT_MIN + b)%100 ;
if(a < 0){
c = -c;
}
if(c > 1000000){
c = c % 1000000;
}
return ~c;
}
//hash 6
//performing various operations such as rotation , arithmetic operations etc to get unique value
int m_hash(char*str, int x){
int s = 1;
int a = rot1(str , x);
for(int i=0; i<x; i++){
int a = rot1(str , i);
s = ( str[i] - i ) * ( str[i] + i ) ^ a + s;
}
a = rot2(str , x);
s = s|a;
if(s<0){
s = -s;
}
if(s>1000000){
s = s%1000000 + 1234;
}
return s;
}
//hash 7
int n_hash(char *str , int length){
int s=1 , c=0;
if(length%2!=0){
c=1;
}
for(int t=0; t<length/2; t++){
s = s + (str[t] * str[length-t])*99;
}
s = ~s;
if(s<0){
s = -s;
}
if(s>1000000){
s = s%1000000 + 87;
}
return s;
}
//hash 8
int max(char*str , int x){
int m = sum(str , x);
int n = mult(str , x);
int o = revmult(str , x);
int p = multsumrev(str , x);
int q = square(str , x);
int r = xor_mul(str , x);
int a = INT_MAX ;
for(int i=0; i<x; i++){
a = a & str[i] ^ m + a;
a = a * n + o | a ;
}
a = a^p | p^q ;
a = a + r;
a = ~a ;
if(a<0){
a = -a;
}
if(a>1000000){
a = a%1000000 - 111;
}
return a;
}
/*void set_array(int *arr , char* ch , int length){
int a = upTlw(ch);
int b = multsumrev(ch , length);
int c = calc_hash3(ch , length);
int d = func4(ch , length);
int e = func5(ch , length);
arr[a] = 1;
arr[b] = 1;
arr[c] = 1;
arr[d] = 1;
arr[e] = 1;
}
*/