Created
March 16, 2010 16:06
-
-
Save LTe/334151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <math.h> | |
#define MAX 10000 | |
char tekst[MAX]; | |
char *wzorzec; | |
char **przejscia; | |
char *strrev(char *str) | |
{ | |
char *p1, *p2; | |
if (! str || ! *str) | |
return str; | |
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) | |
{ | |
*p1 ^= *p2; | |
*p2 ^= *p1; | |
*p1 ^= *p2; | |
} | |
return str; | |
} | |
void wczytaj_tekst() | |
{ | |
char znak; | |
int i = 0; | |
while(1) | |
{ | |
znak = fgetc(stdin); | |
if(znak == EOF) | |
break; | |
else | |
{ | |
tekst[i] = znak; | |
i++; | |
} | |
} | |
} | |
void wczytaj_wzorzec(char *argument) | |
{ | |
int dlugosc = 0; | |
int i = 0; | |
dlugosc = strlen(argument); | |
wzorzec = (char *) malloc(dlugosc * sizeof(char)); | |
for(i=0; i<dlugosc; i++) | |
{ | |
wzorzec[i] = argument[i]; | |
} | |
} | |
int sufiks(char *P1, char *P2) | |
{ | |
int i = 0; | |
int dlP1 = 0; | |
int dlP2 = 0; | |
dlP1 = strlen(P1); | |
dlP2 = strlen(P2); | |
//char P1rev[dlP1]; | |
//char P2rev[dlP2]; | |
P1 = strrev(P1); | |
P2 = strrev(P2); | |
for(i=0; i<dlP1; i++) | |
{ | |
if(P1[i] != P2[i]) | |
return 0; | |
} | |
return 1; | |
} | |
void funkcja_przejscia() | |
{ | |
int dl = 0; | |
unsigned int i = 0;//stany | |
int j = 0; | |
int k=0; | |
dl = strlen(wzorzec); | |
char P1[dl+4]; | |
char P2[dl+4]; | |
przejscia = (char **) malloc(dl+3 * sizeof(char *)); | |
for(i=0; i<=dl; i++) | |
{ | |
przejscia[i] = (char *) malloc(256); | |
} | |
for(i=0; i<=dl; i++) | |
{ | |
for(j=0; j<255; j++) | |
{ | |
k = fmin(dl+1, i+2); | |
do | |
{ | |
k--; | |
strncpy(P1, wzorzec, k); | |
P1[k] = 0; | |
strncpy(P2, wzorzec, i); | |
P2[i] = j; | |
P2[i+1] = 0; | |
} | |
while(!sufiks(P1, P2)); | |
{ | |
przejscia[i][j] = k; | |
} | |
} | |
} | |
} | |
void szukaj() | |
{ | |
int dl = 0; | |
int q = 0; | |
int i = 0; | |
int dlwzorca = 0; | |
int s = 0; | |
dl = strlen(tekst); | |
dlwzorca = strlen(wzorzec); | |
for(i=0; i<dl; i++) | |
{ | |
q = przejscia[q][(unsigned int)tekst[i]]; //char na int | |
if(q == dlwzorca) | |
{ | |
s = i - dlwzorca+1; | |
printf("%d \n", s); | |
} | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
wczytaj_tekst(); | |
//int dl = strlen(tekst); | |
//int i=0; | |
//for(i=0; i<dl; i++) | |
//printf("%c\n", tekst[i]); | |
wczytaj_wzorzec(argv[1]); | |
funkcja_przejscia(); | |
szukaj(); | |
//int length = strlen(wzorzec); | |
//for(i=0; i<length; i++) | |
//{ | |
//printf("%c\n", wzorzec[i]); | |
//} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment