萌新刚学A+B,尝试手搓哈希表
2025-01-16 22:40:08
发布于:广东
38阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
using namespace std;
string s[100005];
struct node{
string key;
int value;
node *next = nullptr;
};
struct Hash{
node *head[99996];
Hash(){
for(int i = 0; i < 99991; i++){
head[i] = new node;
}
}
void insert(string tmp){
unsigned long long __key = 0;
for(char c:tmp) __key = __key * 131 + c;
__key %= 99991;
node *p = head[__key];
while(p -> next && p -> next -> key != tmp) p = p -> next;
if(!(p -> next)){
node *__new = new node;
__new -> key = tmp;
__new -> value = 1;
p -> next = __new;
}else{
p -> next -> value++;
}
}
int get_max(){
int max_val = 0;
for(int i = 0; i < 99991; i++){
node *p = head[i];
while(p -> next) max_val = max(max_val, (p = p -> next) -> value);
}
return max_val;
}
}mp;
int main(){
cin.tie(nullptr) -> sync_with_stdio(0);
cout.tie(nullptr) -> sync_with_stdio(0);
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> s[i];
mp.insert(s[i]);
}
cout << mp.get_max();
return 0;
}
全部评论 1
《萌新》
2025-01-21 来自 广东
0
有帮助,赞一个