图书管理

题目链接:图书管理

我真的太弱了,这种XX题做了五六遍

对于这道题,建一个Hash就行

直接上代码:

 1 #include<cstdio>
 2 #include<string>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 const int N = 1e8+7, Prime =  17;//Prime最好取一个素数,N不要太大,也不能太小,1e8正好,memset速度快而且减小了冲突
 8 int T;
 9 bool vis[N];
10 char s[205];
11 int main()
12 {
13     scanf("%d",&T);
14     while(T--)
15     {
16         scanf("%s",s);
17         if(strcmp(s,"add")==0)
18         {
19             int hs=0;
20             gets(s);
21             int len=strlen(s);
22             for(int i=0;i<len;++i)
23                 hs=(hs*Prime+s[i])%N;
24             vis[hs]=true;
25         }else{
26             int hs=0;
27             gets(s);
28             int len=strlen(s);
29             for(int i=0;i<len;++i)
30                 hs=(hs*Prime+s[i])%N;
31             if(vis[hs]) printf("yes\n");
32             else  printf("no\n");
33         }
34     }
35     return 0;
36 }
View Code

一个1e8害的我搞了半个晚上emmm……

猜你喜欢

转载自www.cnblogs.com/cptbtptpbcptbtptp/p/11397310.html