斗吟锋 提问:题目链接:7.字符串统计 - 蓝桥云课 (lanqiao.cn)
问题描述:通过率只有50%
代码:
#include <stdio.h>
#include <string.h>
#define BASE 256
#define MOD 2010201401
#define MAXN 10001
int hash[MAXN];
int string_hash(char* str)
{
long long sum = 0;
for (int i = 0; str[i]; i++)
{
sum += (sum * BASE + str[i]) % MOD;
}
return sum;
}
void hash_insert(int x)
{
int y = x % MAXN;
while (1)
{
if (hash[y] == -1)
{
hash[y] = x;
return;
}
else if (hash[y] == x)
{
return;
}
y += 1;
if (y >= MAXN)
{
y = 0;
}
}
}
char str[2000010];
int main(int argc, char* argv[])
{
int n;
scanf("%d", &n);
memset(hash, -1, sizeof(hash));
for (int i = 0; i < n; i++)
{
scanf("%s", str);
int x = string_hash(str);
hash_insert(x);
}
int cnt = 0;
for (int i = 0; i < MAXN; i++)
{
if (hash[i] != -1)
{
cnt++;
}
}
printf("%d", cnt);
return 0;
}
string_hash 最后加上这么一句话:return s...