『一本通』哈希和哈希表

Oulipo

 1 #include<bits/stdc++.h>
 2 #define N 1000000+5
 3 using namespace std;
 4 typedef unsigned long long ULL;
 5 const int b=55;
 6 ULL n,m,s,ans,p[N],sum[N];
 7 char s1[N],s2[N];
 8 
 9 int main() {
10     scanf("%s",s1+1),scanf("%s",s2+1);
11     n=strlen(s1+1),m=strlen(s2+1);
12     p[0]=1;
13     for(int i=1;i<=n;i++) 
14      sum[i]=sum[i-1]*b+s1[i]-'A',p[i]=p[i-1]*b;
15     for(int i=1;i<=m;i++) s=s*b+s2[i]-'A';
16     for(int i=0;i<=n-m;i++)
17      if(s==sum[i+m]-sum[i]*p[m]) ans++;
18     printf("%lld",ans); 
19 }

图书管理

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned long long ULL;
 4 const int b=233;
 5 long long n,len;
 6 ULL h;
 7 char op[5],s[205];
 8 map<ULL,bool>mp;
 9 
10 int main() {
11     scanf("%d",&n); 
12     while(n--) {
13         scanf("%s",op);
14         scanf("%s",s);
15         h=0,len=strlen(s);
16         for(int i=0;i<len;i++) h=h*b+s[i];
17         if(op[0]=='a') mp[h]=1;
18         else puts(mp[h]==1?"yes":"no");
19     }
20 }

Seekthe Name, Seek the Fame

 1 #include<bits/stdc++.h>
 2 #define ULL unsigned long long
 3 using namespace std;
 4 const int b=55,N=4*1e5+5;
 5 char s[N];
 6 ULL sum[N],pw[N];
 7 
 8 int main() {
 9     while(~scanf("%s",s+1)) {
10         int len=strlen(s+1); pw[0]=1;
11         for(int i=1;i<=len;i++) {
12             sum[i]=sum[i-1]*b+s[i];
13             pw[i]=pw[i-1]*b;
14         }
15         for(int i=1;i<=len;i++) {
16             ULL hou=sum[len]-sum[len-i]*pw[i];
17             if(sum[i]==hou) printf("%d ",i);
18         }
19         printf("\n");
20     }
21 }

收集雪花

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=1e6+5,t=7001;
 4 int n,l,ans,a[N],lst[N],h[t+5];
 5 
 6 int main() {
 7     n=read();
 8     for(int i=1,j;i<=n;i++) {
 9         a[i]=read();
10         j=lst[i]=h[a[i]%t];
11         while(j&&a[i]!=a[j]) j=lst[j];
12         l=max(l,j),ans=max(ans,i-l);
13         h[a[i]%t]=i;
14     }
15     printf("%d",ans);
16 }

猜你喜欢

转载自www.cnblogs.com/qq8260573/p/10353877.html