CodeForces - 985F Isomorphic Strings

假如两个区间的26的字母出现的位置集合分别是 A1,B1,A2,B2,....., 我们再能找到一个排列p[] 使得 A[i] = B[p[i]] ,那么就可以成功映射了。

显然集合可以直接hash,排序一下hash值就可以判断是否匹配了。。。。

虽然不知道为什么要卡19260817,但反正我是坚决不写双hash的,最后随便换了一个大模数(甚至都不是质数2333)然后就过了。。。

Discription

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 Tfor 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·105, 1?≤?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?≤?n, 1?≤?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] ands[yi... yi?+?leni?-?1] are isomorphic and "NO" otherwise.

Example

Input

7 4abacaba1 1 11 4 22 1 32 4 3

Output

YESYESNOYES

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.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=200005,ha=998244357,b=233;
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
int H[maxn][26],ci[maxn],A[26],B[26];
int n,Q,X,Y,L;
char ch;

inline void Get(int *x,int y){
	for(int i=0;i<26;i++) x[i]=add(H[y+L-1][i],ha-H[y-1][i]*(ll)ci[L]%ha);
}

inline void solve(){
	int i;
	while(Q--){
		scanf("%d%d%d",&X,&Y,&L);
		Get(A,X),Get(B,Y);

		sort(A,A+26),sort(B,B+26);

		for(i=0;i<26;i++) if(A[i]!=B[i]) break;
		puts(i==26?"YES":"NO");
	}
}

int main(){
	scanf("%d%d",&n,&Q);

	ci[0]=1;
	for(int i=1;i<=n;i++) ci[i]=(ci[i-1]*(ll)b)%ha;

	for(int i=1;i<=n;i++){
		ch=getchar();
		while(ch<‘a‘||ch>‘z‘) ch=getchar();

		for(int j=0;j<26;j++) H[i][j]=(H[i-1][j]*(ll)b)%ha;
		H[i][ch-‘a‘]++;
	}

	solve();

	return 0;
}

  

原文地址:https://www.cnblogs.com/JYYHH/p/9071465.html

时间: 2024-10-27 04:45:28

CodeForces - 985F Isomorphic Strings的相关文章

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurre

leetcode_205题——Isomorphic Strings(用的map)

Isomorphic Strings Total Accepted: 5891 Total Submissions: 24698My Submissions Question Solution Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences

[2016-04-26][codeforces][665C - Simple Strings]

时间:2016-04-26 10:11:21 星期二 题目编号:[2016-04-26][codeforces][665C - Simple Strings] 题目大意:给定一个字符串,问最少需要更改多少字符,使得相邻的字母都不一样,输出更改后的字符串 分析: 贪心,从左往右扫一遍数组,更改相同的元素即可 遇到的问题: 注意,相同元素必须更改后面那个元素才是最优的答案. 比如 aaa 这种情况只能更改中间那个 #include<iostream> #include<string>

leetcode 204/187/205 Count Primes/Repeated DNA Sequences/Isomorphic Strings

一:leetcode 204 Count Primes 题目: Description: Count the number of prime numbers less than a non-negative number, n 分析:此题的算法源码可以参看这里,http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 代码: class Solution { public: int countPrimes(int n) { // 求小于一个数n的素数个

205. Isomorphic Strings - LeetCode

Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中相同位置字符的差是否相同 Java实现: public boolean isIsomorphic(String s, String t) { Map<String, Integer> map = new HashMap<>(); for (int i=0; i<s.length(

codeforces 985 F. Isomorphic Strings

题目链接 https://codeforces.com/contest/985/problem/F 题意 首先定义两个长度相等字符串a,b同构,当且仅当a中的每个字母,b中只存在一个字母与之对应,并且两个字母出现位置要完全一致,a,b反过来也要满足. 给定一个长度为\(2 \times 10^5\)的字符串s,有很多次询问.每次询问s从x位置开始的长度为l的字串与从y开始的长度为l的字串是否同构. 分析 对每个字母出现位置的集合进行哈希. 比如对于字母\('k'\),我们把原串中出现\('k'\

【Codeforces 985 F】Isomorphic Strings

题意:给一个字符串\(s\),有\(q\)个询问 x y l 表示问从\(x,y\)开始的长度为\(l\)的子串是否等价. 等价的定义是是否可以形成一个映射\(f\),使得把所有的第一个字符串的字符经过映射后得到恰恰是第二个字符串. 思路:首先我们看如果一个字符串等价于另一个字符串,那么它们所有字符出现位置的哈希值可以一一对应. 那么我们可以处理前缀每个字符出现的哈希值. 那么我们就可以\(O(26)\)求出每次查询了. 一一对应很简单,就是排序之后相同就行了. 原文地址:https://www

leetcode笔记:Isomorphic Strings

一.题目描述 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of chara

【LeetCode】205 - Isomorphic Strings

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.