题解
2025-12-31 19:01:47
发布于:浙江
7阅读
0回复
0点赞
很板的字符串哈希。就是默个板子。虽然本人也不会。可以看见我的字符串哈希函数是乱搞的
#include <bits/stdc++.h>
using namespace std;
const int N=1e7+19;
int f(string a){
int ans=0;
for(int i=0;i<=a.size()-1;i++)ans+=((((a[i]%N+N)*i)*(a[i]+i))+abs(a[i]-i))%N; //乱搞,反正过了
return ans;
}
struct Hash{
int h[N];//哈希表
int e[N],ne[N],idx=0; //链表的东西
void insert(int x){
int hsh=(x%N+N)%N;
e[idx]=x;
ne[idx]=h[hsh];
h[hsh]=idx++;
return;
}bool find(int x){
int hsh=(x%N+N)%N;
for(int i=h[hsh];i!=-1;i=ne[i])
if(e[i]==x)return true;
return false;
}void init(){
for(int i=1;i<=N;i++)h[i]=-1; //初始化,让h中的初始值都为-1
return;
}
};
int main(){
int t;
cin >> t;
Hash a;
a.init();
while(t--){
string op,name;
cin >> op;
getline(cin,name);
if(op=="add"){
a.insert(f(name));
}if(op=="find"){
bool p=a.find(f(name));
cout << (p?"yes":"no");
cout << endl;
}
}
return 0;
}
时间复杂度:大约
全部评论 2
dddddddddddddddd
2025-12-31 来自 浙江
0d
2025-12-31 来自 浙江
0











有帮助,赞一个