Created
May 18, 2015 16:33
-
-
Save kimitoboku/3b0c6e54fd3dcbd4755d 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<time.h> | |
int count_string(char *str,char *s); | |
int string_length(char *str); | |
void makeSkipDistArray(int array[],char str[]); | |
int countStrinInStrings(int sda[],char str[],char word[]); | |
int main(int argc, char *argv[]){ | |
char buf[256]; | |
int sba[256] = {-1}; | |
char *str; | |
FILE *fp; | |
if((fp = fopen(argv[1],"r"))==NULL){ | |
printf("Can't open file. : {%s}",argv[1]); | |
return 1; | |
} | |
makeSkipDistArray(sba,argv[2]); | |
long sz; | |
fseek(fp,0,SEEK_END); | |
sz = ftell(fp); | |
rewind(fp); | |
str = (char *)calloc(sz+1,sizeof(char)); | |
int i; | |
for(i=0;i<sz;i++){ | |
str[i] = fgetc(fp); | |
} | |
str[i+1] = '\0'; | |
int count = 0; | |
clock_t start, end; | |
start = clock(); | |
count = countStrinInStrings(sba,str,argv[2]); | |
end = clock(); | |
printf("MB:[%d]\nTime:%lf[s]\n",count,(end-start)/(double)CLOCKS_PER_SEC); | |
puts(""); | |
start = clock(); | |
count = count_string(str,argv[2]); | |
end = clock(); | |
printf("Nomal:[%d]\nTime:%lf[s]\n",count,(end-start)/(double)CLOCKS_PER_SEC); | |
fclose(fp); | |
return 0; | |
} | |
int count_string(char str[],char s[]){ | |
int str_size = string_length(str); | |
int s_size = string_length(s); | |
int i,j; | |
int cout=0; | |
for(i=0;i<str_size;i++){ | |
if(strncmp(s,str+i,s_size)==0){ | |
cout++; | |
} | |
} | |
return cout; | |
} | |
int string_length(char *str){ | |
int i; | |
for(i=0;str[i];++i); | |
return i; | |
} | |
void makeSkipDistArray(int array[],char str[]){ | |
int size = strlen(str); | |
int i; | |
for(i=1;i<size;i++){ | |
array[str[i]] = size - i -1; | |
} | |
} | |
int countStrinInStrings(int sda[],char str[],char word[]){ | |
int str_size = strlen(str); | |
int word_size = strlen(word); | |
int count = 0; | |
int i,j,t; | |
for(i=word_size-1;i<str_size;){ | |
int buf = i; | |
for(j=word_size-1;j>=0;--j){ | |
if(word[j] != str[i-word_size+j+1]){ | |
if(sda[str[i]] != 0){ | |
i += sda[str[i]]; | |
}else{ | |
i += word_size-1; | |
} | |
break; | |
} | |
} | |
if(buf == i){ | |
count++; | |
if(sda[str[i]] != 0){ | |
i += sda[str[i]]; | |
}else{ | |
i += word_size-1; | |
} | |
} | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment