|
/*
* =====================================================================================
*
* Filename: kmp1.c
*
* Description: another kmp sample
*
* Version: 1.0
* Created: 2009年01月08日 11时40分51秒
* Revision: none
* Compiler: gcc
*
* Author: Dr. Fritz Mehner (mn), mehner@fh-swf.de
* Company: FH Südwestfalen, Iserlohn
*
* =====================================================================================
*/
#include <assert.h>
#include <stdio.h>
#include <time.h>
#include <stdio.h>
#define SEARCH_TIME 10000
/* The file contain words to search. */
#ifdef WINDOWS
#define FILE_PATH "C:\\search.txt"
#else
#define FILE_PATH "/home/xxx/search.txt"
/* The file contain words to search. */
#endif
char *
read_text_file ()
{
FILE *f;
char *text ,*text1;
text = (char *)malloc(100);
text1 = (char *)malloc(4096);
f = fopen(FILE_PATH, "rt");
while (fgets(text,100,f))
{
text1 = strcat(text1,text);
}
printf("%s\n",text1);
fclose(f);
return text ;
}
void kmp_init(const char *patn, int len, char *next)
{
int i, j;
assert(patn != NULL && len > 0 && next != NULL);
next[0] = 0;
for (i = 1, j = 0; i < len; i ++) {
while (j > 0 && patn[j] != patn)
j = next[j - 1];
if (patn[j] == patn)
j += 1;
next = j;
}
}
int kmp_find(const char *text, int text_len, const char *patn,
int patn_len, char *next)
{
int i, j;
assert(text != NULL && text_len > 0 && patn != NULL && patn_len > 0
&& next != NULL);
for (i = 0, j = 0; i < text_len; i ++ ) {
while (j > 0 && text != patn[j])
j = next[j - 1];
if (text == patn[j])
j += 1;
if (j == patn_len)
return i + 1 - patn_len;
}
return -1;
}
int main(int argc, char *argv[])
{
char *next;
int i, pos, len = strlen(argv[1]);
time_t timep;
int seconds, seconds1;
char *text;
text = read_text_file();
if (argc < 2) {
printf("Usage: %s pattern\n", argv[0]);
return 1;
}
seconds = time((time_t*)NULL);
for(int x = 0; x < SEARCH_TIME ; x++){
next = calloc(strlen(argv[1]), sizeof(int));
kmp_init(argv[1], strlen(argv[1]), next);
printf("next array:\n");
for (i = 0; i < len; i ++)
printf("\t%c", argv[1]);
printf("\n");
for (i = 0; i < len; i ++)
printf("\t%d", next);
printf("\n");
pos = kmp_find(text, strlen(text), argv[1], strlen(argv[1]), next);
printf("find result:\n");
if (pos < 0) {
printf("None found!\n");
} else {
printf("%s\n", argv[1]);
for (i = 0; i < pos; i ++)
printf(" ");
printf("^\n");
}
}/* for */
seconds1 = time((time_t*)NULL);
printf("Here are the result of ..\n");
printf("time when start search : %d\n" , seconds);
printf("time when serch end : %d\n", seconds1);
printf("seconds elapsed : %d\n", seconds1-seconds);
return 0;
}
上面是挤出来的,很大部分抄了网上的。现在还不能解决汉字检索问题。代码写的很烂。我是C菜鸟,现在是想用C实现这个汉子检索的算法。帮下忙。有没有高手知道比这个更快的,推荐下,谢谢! |
|