forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp_demo.cpp
More file actions
40 lines (33 loc) · 724 Bytes
/
kmp_demo.cpp
File metadata and controls
40 lines (33 loc) · 724 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "kmp.h"
using namespace alg;
int main(void)
{
srand(time(NULL));
char * S = (char*)malloc(1001);
char * W = (char*)malloc(5);
memset(S,0, 1001);
memset(W,0, 5);
// random genrate a pattern for A, G, C,T
const char P[] = {'A', 'G','C','T'};
for (int i=0;i<1000;i++) {
int k = rand()%4;
S[i] = P[k];
}
for (int i=0;i<4;i++) {
int k = rand()%4;
W[i] = P[k];
}
// do a search for W from S
int pos = kmp_search(S, W);
printf("to be searched:%s\n", W);
if (pos > 0) {
printf("found in pos:%d\n", pos);
printf("text:\n%.*s", pos, S);
printf("\033[31m%s\033[0m", W);
printf("%s\n",&S[pos + strlen(W)]);
}
}