Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb1fed3c

Browse files
authored
Merge pull requestTheAlgorithms#228 from danghai/algo
Add Boyer Moore search algo and simple Makefile
2 parents24b5a4e +9001ae9 commitb1fed3c

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

‎searching/pattern_search/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CC = gcc
2+
FLAG = -o
3+
4+
all: naive_search rabin_karp_search boyer_moore_search
5+
6+
naive_search : naive_search.c
7+
$(CC)$(FLAG) naive_search naive_search.c
8+
rabin_karp_search : rabin_karp_search
9+
$(CC)$(FLAG) rabin_karp_search rabin_karp_search.c
10+
boyer_moore_search: boyer_moore_search boyer_moore_search.c
11+
$(CC)$(FLAG) boyer_moore_search boyer_moore_search.c
12+
13+
clean:
14+
rm naive_search rabin_karp_search boyer_moore_search
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<stdio.h>
2+
#include<string.h>
3+
4+
#defineNUM_OF_CHARS 256
5+
6+
intmax(inta,intb) {return (a>b)?a:b;}
7+
8+
voidcomputeArray(char*pattern,intsize,intarr[NUM_OF_CHARS])
9+
{
10+
inti;
11+
12+
for(i=0;i<NUM_OF_CHARS;i++)
13+
arr[i]=-1;
14+
/* Fill the actual value of last occurrence of a character */
15+
for(i=0;i<size;i++)
16+
arr[(int)pattern[i]]=i;
17+
}
18+
/* Boyer Moore Search algorithm */
19+
voidboyer_moore_search(char*str,char*pattern)
20+
{
21+
intn=strlen(str);
22+
intm=strlen(pattern);
23+
intshift=0;
24+
intarr[NUM_OF_CHARS];
25+
26+
computeArray(pattern,m,arr);
27+
while(shift <= (n-m))
28+
{
29+
intj=m-1;
30+
while (j >=0&&pattern[j]==str[shift+j])
31+
j--;
32+
if (j<0)
33+
{
34+
printf("--Pattern is found at: %d\n",shift);
35+
shift+= (shift+m<n) ?m-arr[str[shift+m]] :1;
36+
}else {
37+
shift+=max(1,j-arr[str[shift+j]]);
38+
}
39+
}
40+
}
41+
42+
intmain()
43+
{
44+
charstr[]="AABCAB12AFAABCABFFEGABCAB";
45+
charpat1[]="ABCAB";
46+
charpat2[]="FFF";/* not found */
47+
charpat3[]="CAB";
48+
49+
printf("String test: %s\n",str);
50+
printf("Test1: search pattern %s\n",pat1);
51+
boyer_moore_search(str,pat1);
52+
printf("Test2: search pattern %s\n",pat2);
53+
boyer_moore_search(str,pat2);
54+
printf("Test3: search pattern %s\n",pat3);
55+
boyer_moore_search(str,pat3);
56+
return0;
57+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp