# 模拟哈希表&字符串哈希

#### 模拟哈希表

##### 处理冲突

###### 拉链法

using namespace std;
const int mod = 100003;
int h[100005],cnt = 1,n;
struct node{
int nex,number;
}edge[100005];//链表
void insert(int x){
int hashCode = (x % mod + mod) % mod;//把负数也给映射过来
edge[cnt].number = x;
edge[cnt].nex = h[hashCode];
h[hashCode] = cnt++;
}
bool find(int x){
int hashCode = (x % mod + mod) % mod;
for(int i = h[hashCode]; i != 0;i = edge[i].nex){//遍历该哈希值下的所有数
if(edge[i].number == x)return true;
}
return false;
}


#### 字符串哈希

s u m [ 1 ] = P ∗ ‘ a ’ sum[1] = P * ‘a’
s u m [ 2 ] = P 2 ∗ ‘ c ’ + s u m [ 1 ] sum[2] = P^2 * ‘c’ + sum[1]
s u m [ 3 ] = P 3 ∗ ‘ f ’ + s u m [ 2 ] sum[3] = P^3 * ‘f’ + sum[2]

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
#define eps 1e-8
using namespace std;
const ll maxn = 1e5 + 5;
typedef unsigned long long ull;
int n,m,L1,R1,L2,R2;
ull p[maxn],sum[maxn];
char str[100005];
ull finds(int l,int r){
ull res = sum[r] - sum[l - 1] * p[r - l + 1];
return res;
}
int main(){
scanf("%d %d",&n,&m);
scanf("%s",str + 1);
p[0] = 1;
for(int i = 1; i <= n; i++){
p[i] = p[i - 1] * 131;
sum[i] = sum[i - 1] * 131 + str[i];
}
while(m--){
scanf("%d %d %d %d",&L1,&R1,&L2,&R2);
if(finds(L1,R1) == finds(L2,R2))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}

12-21

02-18 456
10-31 795
06-03 19万+
04-02 42
01-11 42
05-07 648
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客