我写了脚本测试了一下
果然效果很好。
果然效果很好。
-bash-3.00$ ./test < in.dat
Run Time 0.036625
Run Time 0.036625
-bash-3.00$ ./outcome < in.dat
Run Time 0.923035
Run Time 0.923035

生命不熄,战斗不止.
程序代码:#include <stdio.h>
#include <string.h>
#define HT_SIZE 16381
int total = 1;
typedef unsigned int hash_T;
hash_T ht_hash[HT_SIZE] = {0};
int ht_index[HT_SIZE];
char text[10001][201];
int count[10001];
hash_T hash(char *str)
{
hash_T h = *str;
if (h != 0)
{
for (str += 1; *str != '\0'; str++)
h = (h << 5) - h + *str;
}
return h;
}
int lookup_and_insert(char *str)
{
size_t step = 0, index;
hash_T hash_value = hash(str);
if (hash_value < 1)
hash_value = 1;
index = hash_value % HT_SIZE;
while (ht_hash[index] != 0)
{
if (ht_hash[index] == hash_value
&& !strcmp(text[ht_index[index]], str))
return ht_index[index];
step++;
index += step;
index %= HT_SIZE;
}
strcpy(text[total], str);
ht_hash[index] = hash_value;
ht_index[index] = total;
count[total] = 1;
++total;
return 0;
}
int main(void)
{
char txt[201];
int i, n, res;
res = scanf("%d", &n);
for (i = 0; i < n; ++i)
{
if (scanf("%s", txt) == 1
&& (res = lookup_and_insert(txt)))
++count[res];
}
for (i = 1; i < total; ++i)
printf("%s %d\n", text[i], count[i]);
return 0;
}