AcWing841 字符串哈希 代码: #include<bits/stdc++.h> #define pii pair<int,int> #define ll long long #define ull unsigned long long #define cl(x) memset(x,0,sizeof(x)) const int N=1e5+3; const int P=131; const int mod=1e7+9; const int maxn=0x3f3f3f3f; const int minn=0xc0c0c0c0; const int inf=99999999; using namespace std; int n,m; char s[N]; ull h[N],p[N]; ull find(int l,int r) { return h[r]-h[l-1]*p[r-l+1]; } int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n>>m; cin>>s+1; int i; p[0]=1; for(i=1;i<=n;i++) { h[i]=h[i-1]*P+s[i]; p[i]=p[i-1]*P; } while(m--) { int l1,r1,l2,r2; cin>>l1>>r1>>l2>>r2; if(find(l1,r1)==find(l2,r2)) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }