-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhashtable.c
70 lines (70 loc) · 1.51 KB
/
hashtable.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define p 997
struct Node{
int data;
struct Node*next;
};
struct Node*hashArray[p]={0};
int hashValue(int key)
{
return key%p;
}
void InsertAtBeg(struct Node**head,int key)
{
struct Node*temp=malloc(sizeof(struct Node));
temp->data=key;
temp->next=*head;
*head=temp;
}
void printList(struct Node*node)
{
while(node!=NULL)
{
printf("%d",node->data);
node=node->next;
}
printf("\n");
}
void traverse(struct Node*node,char*text,char*pat,int n,int m)
{
while(node!=NULL)
{
for(int i=node->data,k=0;k<m;k++)
{
if(text[i+k]!=pat[k])
{
break;
}
}
printf("There is a match at %d.\n",node->data);
node=node->next;
}
}
int main(void)
{
char *text=malloc(255*sizeof(char));
char *pat=malloc(255*sizeof(char));
scanf("%s %s",text,pat);
int n=strlen(text),m=strlen(pat);
int T=text[0]-'0',P=pat[0]-'0',x=2;
for(int j=1;j<m;j++)
{
x=hashValue(x*2);
T=hashValue(T*2+(text[j]-'0'));
P=hashValue(P*2+(pat[j]-'0'));
}
int required_hash=P;
printf("%d\n",required_hash);
printf("%d\n",T);
InsertAtBeg(&hashArray[T],0);
for(int i=1;i<=n-m;i++)
{
T=hashValue(T*2+(text[i+m-1]-'0')-x*(text[i-1]-'0'));
InsertAtBeg(&hashArray[T],i);
}
printList(hashArray[required_hash]);
traverse(hashArray[required_hash],text,pat,n,m);
return 0;
}