Commit ef73177c authored by Julien David's avatar Julien David
Browse files

Boyer-Moore

parent a5c7189a
......@@ -3,7 +3,7 @@ RAND_FILE=src/mainRand.o src/randWord.o src/recherche.o
FICHIER_FILE=src/mainFichier.o src/randWord.o src/recherche.o
CFLAGS=-g -Wall -ansi -pedantic -O3
all:naif naifRand naifFichier naifint naifRandint naifFichierint mp mpRand mpFichier kmp kmpRand kmpFichier bord KMPbord randWord bm bmRand
all:naif naifRand naifFichier naifint naifRandint naifFichierint mp mpRand mpFichier kmp kmpRand kmpFichier bord KMPbord randWord bm bmRand bonDec
naif: src/naif.o $(CLASSIC_FILE)
......@@ -58,6 +58,10 @@ kmpFichier:src/kmp.o $(FICHIER_FILE)
bm:src/bm.o $(CLASSIC_FILE)
gcc $(CFLAGS) src/bm.o $(CLASSIC_FILE) -o bm
bonDec:src/bondec.o
gcc $(CFLAGS) src/bondec.o -o bonDec
bmRand:src/bm.o $(RAND_FILE)
gcc $(CFLAGS) src/bm.o $(RAND_FILE) -o bmRand
......
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include "recherche.h"
#define MAX(x,y) (x<y)?y:x
int * derniere_occ(char * mot,int m,int k){
int * last_occ=(int *)malloc(sizeof(int)*k);
int i;
for(i=0;i<k;i++)
last_occ[i]=m;
for(i=0;i<(m-1);i++)
last_occ[(int)(mot[i]-'a')]=m-i-1;
for(i=0;i<k;i++)
printf("%d ",last_occ[i]);
printf("\n");
return last_occ;
}
void echanger(char * x,char * y){
char tmp=*x;
*x=*y;
*y=tmp;
}
void inverser_mot(char * motif,int m){
int i,j;
for(i=0,j=m-1;i<j;++i,--j)
echanger(&motif[i],&motif[j]);
}
int * kmp_bord(char * mot,int m){
int i,j;
int * table_bord=(int *)malloc(sizeof(int)*(m+1));
table_bord[0]=-1;
table_bord[1]=0;
j=0;
for(i=2;i<=m;i++){
while(j>=0&&mot[j]!=mot[i-1])
j=table_bord[j];
j++;
if(mot[i]!=mot[j])
table_bord[i]=j;
else
table_bord[i]=MAX(0,table_bord[j]);
}
return table_bord;
}
int * bm_bon_decalage(char * mot,int m){
int * table_bord;
int * bon_suff=(int *)malloc(sizeof(int)*(m+1));
int i,l;
inverser_mot(mot,m);
table_bord=kmp_bord(mot,m);
inverser_mot(mot,m);
for(l=0;l<=m;l++){
bon_suff[l]=m-(table_bord[m]);
}
for(i=1;i<=m;i++){
l=m-(table_bord[i]);
if(bon_suff[l]>(i-(table_bord[i])) )
bon_suff[l]=i-(table_bord[i]);
}
for(i=0;i<=m;i++)
printf("%d ",bon_suff[i]);
printf("\n");
return bon_suff;
}
int boyer_moore(char * texte, char * motif,int n,int m,int k){
int * last_occ=derniere_occ(motif,m,k);
int * bon_suff=bm_bon_decalage(motif,m);
int i,j;
int l,l2;
int occ=0;
i=0;
printf("%s\n",texte);
while(i<(n-m+1)){
j=m-1;
while(j>=0&&comparer_lettre(texte[i+j],motif[j]))
j--;
for(l=0;l<(i+j);l++)printf(" ");
for(l2=j;l2<m;l2++)printf("%c",motif[l2]);
printf("\n");
if(j<0){
occ++;
i+=bon_suff[0];
}
else
i+=MAX(bon_suff[j+1],last_occ[(int)(texte[i + j]-'a')] - m + 1 + j);
}
return occ;
}
int recherche(char * texte,char * motif,int n,int m,int k){
return boyer_moore(texte,motif,n,m,k);
}
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX(x,y) (x<y)?y:x
void echanger(char * x,char * y){
char tmp=*x;
*x=*y;
*y=tmp;
}
void inverser_mot(char * motif,int m){
int i,j;
for(i=0,j=m-1;i<j;++i,--j)
echanger(&motif[i],&motif[j]);
}
int * kmp_bord(char * mot,int m){
int i,j;
int * table_bord=(int *)malloc(sizeof(int)*(m+1));
table_bord[0]=-1;
table_bord[1]=0;
j=0;
for(i=2;i<=m;i++){
while(j>=0&&mot[j]!=mot[i-1])
j=table_bord[j];
j++;
if(mot[i]!=mot[j])
table_bord[i]=j;
else
table_bord[i]=MAX(0,table_bord[j]);
}
return table_bord;
}
int * bm_bon_decalage(char * mot,int m){
int * table_bord;
int * bon_suff=(int *)malloc(sizeof(int)*(m+1));
int i,l;
inverser_mot(mot,m);
table_bord=kmp_bord(mot,m);
inverser_mot(mot,m);
for(l=0;l<=m;l++){
bon_suff[l]=m-(table_bord[m]);
}
for(i=1;i<=m;i++){
l=m-(table_bord[i]);
if(bon_suff[l]>(i-(table_bord[i])) )
bon_suff[l]=i-(table_bord[i]);
}
return bon_suff;
}
int main(int argc,char ** argv){
int m,i;
int * bondec;
if(argc==2){
m = strlen(argv[1]);
bondec = bm_bon_decalage(argv[1], m);
for(i = 0; i <= m; i++)
printf("%d ", bondec[i]);
printf("\n");
free(bondec);
}
return EXIT_SUCCESS;
}
......@@ -9,10 +9,11 @@ int * bord(char * mot,int m){
int * table_bord=(int *)malloc(sizeof(int)*(m+1));
table_bord[0]=-1;
table_bord[1]=0;
j=0;
for(i=2;i<=m;i++){
while(j>=0&&mot[j]!=mot[i-1])
j=table_bord[j];
for(i=2; i<=m; i++){
while( j>=0 && mot[j] != mot[i-1])
j = table_bord[j];
j++;
table_bord[i]=j;
}
......
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "naif.h"
#include"recherche.h"
int algorithme_naif(char * texte,char * motif,int n,int m){
int i,j;
int psize=m>>2;
unsigned int * p=(unsigned int *)motif;
unsigned int * t;
int res;
for(i=0,res=0;i<=(n-m);i++){
j=0;
t=(unsigned int *)&texte[i];
while(j<psize&&comparer_lettres(t[j],p[j])){
j++;
}
if(j==psize){
j=m-m%4;
while(j<m&&comparer_lettres(texte[i+j],motif[j]))
j++;
if(j==m)
res++;
}
}
return res;
}
int recherche(char * texte,char * motif,int n,int m,int k){
return algorithme_naif(texte,motif,n,m);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment