CF985F-Isomorphic Strings

目录

CF985F-Isomorphic Strings

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

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

  1. f(si)=ti f(s_{i})=t_{i} for any index i i ,
  2. for any character https://cdn.luogu.com.cn/upload/vjudge_pic/CF985F/f0f59850188390351c083ddc0339cc47c4315e9d.png there is exactly one character https://cdn.luogu.com.cn/upload/vjudge_pic/CF985F/cfd6520533d25a050303bbfc24cf098c4a7d5d3f.png that f(x)=y f(x)=y ,
  3. for any character https://cdn.luogu.com.cn/upload/vjudge_pic/CF985F/cfd6520533d25a050303bbfc24cf098c4a7d5d3f.png there is exactly one character https://cdn.luogu.com.cn/upload/vjudge_pic/CF985F/f0f59850188390351c083ddc0339cc47c4315e9d.png that f(x)=y 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 m queries characterized by three integers x,y,len x,y,len ( 1<=x,y<=nlen+1 1<=x,y<=n-len+1 ). For each query check if two substrings s[x x+len1] s[x…\ x+len-1] and s[y y+len1] s[y…\ y+len-1] are isomorphic.

The first line contains two space-separated integers n n and m m ( 1<=n<=2105 1<=n<=2·10^{5} , 1<=m<=2105 1<=m<=2·10^{5} ) — the length of the string s s and the number of queries.

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

The following m m lines contain a single query on each line: xi x_{i} , yi y_{i} and leni len_{i} ( 1<=xi,yi<=n 1<=x_{i},y_{i}<=n , 1<=leni<=nmax(xi,yi)+1 1<=len_{i}<=n-max(x_{i},y_{i})+1 ) — the description of the pair of the substrings to check.

For each query in a separate line print “YES” if substrings s[xi xi+leni1] s[x_{i}…\ x_{i}+len_{i}-1] and s[yi yi+leni1] s[y_{i}…\ y_{i}+len_{i}-1] are isomorphic and “NO” otherwise.

1
2
3
4
5
6
7
7 4
abacaba
1 1 1
1 4 2
2 1 3
2 4 3

1
2
3
4
5
YES
YES
NO
YES

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Problem: CF985F Isomorphic Strings
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF985F
// Memory Limit: 250 MB
// Time Limit: 3000 ms
// Author: Ybw051114
//
// Powered by CP Editor (https://cpeditor.org)

#include "ybwhead/ios.h"
const int MOD1 = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const int N = 5e5 + 10;
#define fi first
#define se second
typedef pair<int, int> pii;
typedef long long ll;
const int b1 = 103;
const int b2 = 107;
pii H[30][N];
int pw1[N], pw2[N];
int n, m;
string s;

pii Get(pii H[], int l, int r)
{
    pii ans = H[r];
    l--;
    if (l >= 0)
    {
        ans.fi = (ans.fi - ll(H[l].fi) * pw1[r - l] % MOD1 + MOD1) % MOD1;
        ans.se = (ans.se - ll(H[l].se) * pw2[r - l] % MOD2 + MOD2) % MOD2;
    }
    return ans;
}

signed main()
{
    pw1[0] = pw2[0] = 1;
    yin >> n >> m;
    yin >> s;
    for (int i = 1; i <= n; i++)
    {
        pw1[i] = 1ll * pw1[i - 1] * b1 % MOD1;
        pw2[i] = 1ll * pw2[i - 1] * b2 % MOD2;
    }
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < 26; j++)
        {
            H[j][i].fi = (ll(H[j][i - 1].fi) * b1 + ll(1 + ('a' + j == s[i - 1]))) % MOD1;
            H[j][i].se = (ll(H[j][i - 1].se) * b2 + ll(1 + ('a' + j == s[i - 1]))) % MOD2;
        }
    while (m--)
    {
        int x, y, len;
        yin >> x >> y >> len;
        multiset<pii> A, B;
        for (int i = 0; i < 26; i++)
        {
            A.insert(Get(H[i], x, x + len - 1));
            B.insert(Get(H[i], y, y + len - 1));
        }
        yout << (A == B ? "YES" : "NO") << endl;
    }
}

来发评论吧~
Powered By Valine
v1.4.14