图书管理
题目链接:图书管理
我真的太弱了,这种XX题做了五六遍
对于这道题,建一个Hash就行
直接上代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
一个1e8害的我搞了半个晚上emmm……
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步