#include<iostream>
#include<string>
using namespace std;
const int N = 10010;
//A 65 --- 0
int a[N];
string v[510];
int Hash(const int* Key, int TableSize)
{
unsigned long int h = 0;
for (int i = 0; i < 3; i++)
{
h = (h << 5) + Key[i];
}
return h % TableSize;
}
int index = 0;
int main() {
int n, p;
cin >> n >> p;
bool flag = true;
while (n--)
{
string s1, s2;
cin >> s1;
bool m = true;
for (int i = 0; i < index; i++)
{
if (v[i] == s1) m = false;
break;
}
s2 = s1.substr(s1.size() - 3);
int* str;
str = (int*)malloc(sizeof(int) * 3);
for (int i = 0; i < 3; i++)
{
str[i] = s2[i] - 65;
}
unsigned long int ret = 0;
ret = Hash(str, p);
if (m==true)
{
v[index++] = s1;
//进行平方探测
int i = 0;
unsigned long int temp = ret;
while (a[ret] == 1) {
i++;
ret = (temp + i * i) % p; // 平方探测公式
if (a[ret] == 0)
{
break;
}
ret = (temp - i * i + p) % p; // 确保索引为正
if (a[ret] == 0)
{
break;
}
if (i >= p) {
break; // 防止无限循环
}
ret = temp;
}
a[ret] = 1;
}
a[ret] = 1;
if (flag)
{
cout << ret;
flag = false;
}
else
{
cout << ' ' << ret;
}
}
}
标签:temp,int,s1,ret,列表,break,++,字符串,暂存
From: https://www.cnblogs.com/szz123/p/18516676