#include <iostream>
#include <vector>
const int MOD = 233333;
std::vector<std::pair<int, int>> linker[MOD + 5];
int gethash(std::string a, std::string b) // 算哈希值
{
return (a[0] - 'A') + (a[1] - 'A') * 26 + (b[0] - 'A') * 26 * 26 + (b[1] - 'A') * 26 * 26 * 26;
}
void insert(int x) // 将这个哈希值加入到哈希表
{
for (auto i : linker[x % MOD])
if (i.first == x)
{
std::cout << x << " " << i.second << " "; // 调试
i.second++;
std::cout << i.second << std::endl; // 调试, i.second 确实加了 1, 但是貌似退出函数后加的 1 就没了
return;
}
linker[x % MOD].push_back(std::pair<int, int>(x, 1));
}
int find(int x) // 求这个哈希值出现了几次
{
for (auto i : linker[x % MOD])
if (i.first == x)
return i.second;
return 0;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int n;
long long ans = 0;
std::cin >> n;
for (int i = 1; i <= n; i++)
{
std::string a, b;
std::cin >> a >> b;
std::cout << gethash(a, b) << std::endl; // 调试
// if (a[0] != b[0] || a[1] != b[1])
// ans += find(gethash(b, a));
insert(gethash(a, b));
}
// std::cout << ans << "\n";
// 调试
while (true)
{
std::string a, b;
std::cin >> a >> b;
std::cout << gethash(a, b) << " " << find(gethash(a, b)) << std::endl;
}
return 0;
}
现在的问题是 insert() 有问题。
输入以下数据,发现第 18、19 行输出来了,i.second 也加了 1,但函数退出来之后加 1 就没了。
3
ABC DE
ABD DE
ABD DE