开放寻址法
#include<iostream> #include<algorithm> #include<cstring> #include<string> using namespace std; const int N =200003,null=0x3f3f3f3f; int a[N]; int find(int x) { int k = (x % N + N) % N;//重点哈希公式 while (a[k] != null && a[k] != x) { k++; if (k == N) k = 0; } return k; } int main() { memset(a, null, sizeof(a)); int n; cin >> n; int x; while (n--) { string s; cin >> s; scanf("%d", &x); int k = find(x); if (s == "I") a[k] = x; else { if(a[k]==x) printf("Yes\n"); else printf("No\n"); } } return 0; }
字符串哈希
注意点!!!!
#include<iostream> #include<string> using namespace std; typedef unsigned long long ULL; const int N=100001;char str[N]; ULL a[N],p[N];int P=131; ULL get(int l,int r){ return a[r]-a[l-1]*p[r-l+1]; } int main(){ int n,m;cin>>n>>m; scanf("%s",str+1); p[0]=1; for(int i=1;i<=n;i++){ a[i]=a[i-1]*P+str[i];//存放字符串哈希 p[i]=p[i-1]*P;//存放p进制数 } while(m--){ int l1,r1,l2,r2; scanf("%d%d%d%d",&l1,&r1,&l2,&r2); if(get(l1,r1)==get(l2,r2)) puts("Yes"); else puts("No"); } return 0; }
标签:null,15,哈希,int,ULL,字符串,return,include From: https://www.cnblogs.com/daimazhishen/p/17706371.html