该系统需要支持 2 种操作:
- add(s) 表示新加入一本书名为 s 的图书。
- find(s) 表示查询是否存在一本书名为 s 的图书。
【题解】:
利用链式前向星来模拟hash表的链,同时使用了双hash结构,代码是抄袭书本的。。。。
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 3e5+10; 4 const int Mod1 = 1e6+3; 5 const int Mod2 = 1e6+9; 6 const int p1 = 47 ; 7 const int p2 = 79 ; 8 int cnt = 0 , Next[N+5],Head[Mod2+10],W[N]; 9 void Insert(int x,int y ){ 10 Next[++cnt] = Head[x] ; 11 Head[x] = cnt ; 12 W[cnt] = y ; 13 } 14 bool Query(int x,int y){ 15 for(int i=Head[x];i;i=Next[i]){ 16 if( W[i] == y ) return true; 17 } 18 return false ; 19 } 20 int main() 21 { 22 ios_base :: sync_with_stdio(NULL); 23 cin.tie(0),cout.tie(0); 24 int n; 25 char op[10],s[5006]; 26 cin >> n ; 27 while (n--) { 28 cin >> op ; 29 cin.getline(s,sizeof(s)); 30 int len = strlen(s); 31 long long sum1 = 0 ,sum2 = 0; 32 for(int i=0;i<len;i++){ 33 sum1 = ( sum1 * p1 + s[i] ) % Mod1; 34 sum2 = ( sum2 * p2 + s[i] ) % Mod2; 35 } 36 if ( op[0] == ‘a‘ ){ 37 Insert(sum1,sum2); 38 }else{ 39 if( Query(sum1,sum2) ){ 40 printf("yes\n"); 41 }else{ 42 printf("no\n"); 43 } 44 } 45 } 46 return 0; 47 }
原文:https://www.cnblogs.com/Osea/p/11325337.html