Skip to content

Instantly share code, notes, and snippets.

@LTe
Created March 16, 2010 16:57
Show Gist options
  • Save LTe/334213 to your computer and use it in GitHub Desktop.
Save LTe/334213 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int dl1, dl2;
int p, q;
int wynik[256];
int licznik;
int j,k,i;
char a;
char **delta;
char *tablica1, *tablica2;
void Delta(int m , char Wzorzec[])
{
delta= (char **) malloc(m+3 * sizeof (char *)); // przydzielenie miejsca na tablice wskaŸnikow
for (q=0; q<=m; q++) // dla kazdego stanu tworzymy tablice
{
delta[q] = (char *) malloc(256 * sizeof(char)); // alfabet ASCII
}
tablica1 = (char *) malloc(m+2 * sizeof(char));
tablica2 = (char *) malloc(m+2 * sizeof(char));
for (q=0; q<=m; q++)
{
for (a=1; a!=0 ; a++)
{
k=fmin(m+1,q+2);
do
{
k--;
strncpy(tablica1, Wzorzec, k);
tablica1[k] = 0;
strncpy(tablica2, Wzorzec, q); //skopiuj z P do tablicy 2 q bitów
tablica2[q] = a;
tablica2[q+1] = 0;
} while (!(SprawdzSufiks(tablica1 ,tablica2)));
delta[q][a]=k;
}
}
}
void Wyszukaj(char Tekst[], char Wzorzec[], int n , int m ,int floresy[],int *esy)
{
int q=0;
for (i=1;i<=n;i++)
{
q=delta[q][Tekst[i]];
if (q==m)
{
floresy[*esy]=i-q+1;
(*esy)++;
}
}
}
int SprawdzSufiks(char wyrazenie1[], char wyrazenie2[])
{
dl1 = strlen(wyrazenie1);
dl2 = strlen(wyrazenie2);
for (p=dl1-1,q=dl2-1; p>=0; p--,q--)
{
if (wyrazenie1[p] != wyrazenie2[q])
return 0;
}
return 1;
}
int main(int argc, char *argv[])
{
Wyszukaj(argv[1], argv[2], strlen(argv[1]), strlen(argv[2]), wynik,&licznik);
for(j=0; j<licznik; j++)
{
printf("BRAWO! WZORZEC WYSTEPUJE! Z PRZESUNIECIEM = %d\n", wynik[j]);
}
getchar();
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment