Codeforces 985F Isomorphic Strings 字符串哈希

F. Isomorphic Strings
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a string s of length n consisting of lowercase English letters.

For two given strings s and t, say S is the set of distinct characters of s and T is the set of distinct characters of t. The strings s and t are isomorphic if their lengths are equal and there is a one-to-one mapping (bijection) f between S and T for which f(si) = ti. Formally:

  1. f(si) = ti for any index i,
  2. for any character  there is exactly one character  that f(x) = y,
  3. for any character  there is exactly one character  that f(x) = y.

For example, the strings "aababc" and "bbcbcz" are isomorphic. Also the strings "aaaww" and "wwwaa" are isomorphic. The following pairs of strings are not isomorphic: "aab" and "bbb", "test" and "best".

You have to handle m queries characterized by three integers x, y, len (1 ≤ x, y ≤ n - len + 1). For each query check if two substrings s[x... x + len - 1] and s[y... y + len - 1] are isomorphic.

Input

The first line contains two space-separated integers n and m (1 ≤ n ≤ 2·1051 ≤ m ≤ 2·105) — the length of the string s and the number of queries.

The second line contains string s consisting of n lowercase English letters.

The following m lines contain a single query on each line: xiyi and leni (1 ≤ xi, yi ≤ n1 ≤ leni ≤ n - max(xi, yi) + 1) — the description of the pair of the substrings to check.

Output

For each query in a separate line print "YES" if substrings s[xi... xi + leni - 1] and s[yi... yi + leni - 1] are isomorphic and "NO" otherwise.

Example
input
Copy
7 4
abacaba
1 1 1
1 4 2
2 1 3
2 4 3
output
Copy
YES
YES
NO
YES
Note

The queries in the example are following:

  1. substrings "a" and "a" are isomorphic: f(a) = a;
  2. substrings "ab" and "ca" are isomorphic: f(a) = cf(b) = a;
  3. substrings "bac" and "aba" are not isomorphic since f(b) and f(c) must be equal to a at same time;
  4. substrings "bac" and "cab" are isomorphic: f(b) = cf(a) = af(c) = b.


给定一个字符串,2e5次询问,求两段区间的字符串是否同构。


对于ch='a'~'z',将字符串变为01串,某位为ch时为1,否则为0. 分别对26个字符串哈希。查询的时候,只要找到两段区间位置上对应的字符,看它们在这两段哈希值是否相等。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define pb push_back 
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=200005;
const ll inf=0x3f3f3f3f,mod=1e9+7;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
ll hash[maxn][26],fac[maxn];
char s[maxn];
vector<int> v[26];

int main() {
	int n,m,i,j,x,y,z;
	scanf("%d%d",&n,&m);
	scanf("%s",s+1);
	fac[0]=1;
	for (i=1;i<=n;i++) {
		fac[i]=(fac[i-1]*inf)%mod;
		for (j=0;j<26;j++) {
			hash[i][j]=(hash[i-1][j]*inf+(s[i]==(char)('a'+j)))%mod;
		}
		v[s[i]-'a'].pb(i);
	}
	for (j=0;j<26;j++) v[j].pb(n+1); 
	for (i=1;i<=m;i++) {
		scanf("%d%d%d",&x,&y,&z);
		int flag=1;
		for (j=0;j<26;j++) {
			int pos=lower_bound(v[j].begin(),v[j].end(),x)-v[j].begin();
			pos=v[j][pos];
			if (pos>=z+x) continue;
			int dual=s[pos-x+y]-'a';       //找第一个区间内'a'+j在第二个区间对应的字符
			ll h1=(((hash[z+x-1][j]-hash[x-1][j]*fac[z])%mod)+mod)%mod;
			ll h2=(((hash[z+y-1][dual]-hash[y-1][dual]*fac[z])%mod)+mod)%mod;
			if (h1!=h2) {
				flag=0;break;
			}
		}
		if (flag) printf("YES\n"); else printf("NO\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值