strstr的这个实现使用散列,下面的散列函数选择显然不完美,这就是我想改进的原因;完整的计划如下:
#include <stdlib.h>
#include <stdio.h>
const char *
strstr(const char *str1, const char *str2)
{
unsigned hash1 = 0, hash2 = 0;
unsigned i, j, str2_len;
for (i = 0; str2[i] != '\0'; i++)
{
hash2 += (unsigned) str2[i];
if (str1[i] == '\0')
return NULL;
hash1 += (unsigned) str1[i];
}
str2_len = i;
for (i=0; str1[i] != '\0'; i++)
{
if (hash1 == hash2)
{
for (j=0; j < str2_len; j++)
if (str1[i+j] != str2[j])
break;
if (j == str2_len)
return str1+i;
}
if (str1[i+str2_len] == '\0')
return NULL;
hash1 = (hash1 - ((unsigned)str1[i])) + ((unsigned)str1[i+str2_len]);
}
}
int
main(int argc, char **argv)
{
const char *match;
char buffer[] = "abc abcdef";
char sub[] = "abcdef";
match = strstr(buffer, sub);
if (match)
printf("%s\n", match);
else
printf("NOT FOUND\n");
return 0;
}
更好的哈希函数可以改善此算法的运行时间(减少str2
与str1
的子字符串进行比较的次数。)
任何有助于改进算法的建议,都会很受欢迎
感谢。