CF1673B.A Perfectly Balanced String?

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Let's call a string ss perfectly balanced if for all possible triplets (t,u,v)(t,u,v) such that tt is a non-empty substring of ss and uu and vv are characters present in ss, the difference between the frequencies of uu and vv in tt is not more than 11.

For example, the strings "aba" and "abc" are perfectly balanced but "abb" is not because for the triplet ("bb",'a','b'), the condition is not satisfied.

You are given a string ss consisting of lowercase English letters only. Your task is to determine whether ss is perfectly balanced or not.

A string bb is called a substring of another string aa if bb can be obtained by deleting some characters (possibly 00) from the start and some characters (possibly 00) from the end of aa.

我们称一个字符串 ss 是完美平衡的,如果对所有可能的三元组 (t,u,v)(t,u,v)(其中 tt 是 ss 的一个非空子串,且 uu 和 vv 均为 ss 中出现过的字符),tt 中 uu 与 vv 的出现频数之差的绝对值不超过 11。

例如,字符串 "aba" 和 "abc" 是完美平衡的,但 "abb" 不是,因为对三元组 ("bb", 'a', 'b'),该条件不成立。

你将得到一个仅由小写英文字母组成的字符串 ss。你的任务是判断 ss 是否为完美平衡的。

若字符串 bb 可通过从字符串 aa 的开头删除若干字符(可能为 00 个)且从结尾删除若干字符(可能为 00 个)而得到,则称 bb 是 aa 的一个子串。

输入格式

The first line of input contains a single integer tt (1≤t≤2⋅1041\leq t\leq 2\cdot 10^4) denoting the number of testcases.

Each of the next tt lines contain a single string ss (1≤∣s∣≤2⋅1051\leq |s|\leq 2\cdot 10^5), consisting of lowercase English letters.

It is guaranteed that the sum of ∣s∣|s| over all testcases does not exceed 2⋅1052\cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤2⋅1041\leq t\leq 2\cdot 10^4),表示测试用例的数量。

接下来的 tt 行,每行包含一个字符串 ss(1≤∣s∣≤2⋅1051\leq |s|\leq 2\cdot 10^5),由小写英文字母组成。

保证所有测试用例中 ∣s∣|s| 的总和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, print "YES" if ss is a perfectly balanced string, and "NO" otherwise.

You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).

对于每个测试用例,如果 ss 是一个完全平衡的字符串,则输出 "YES";否则输出 "NO"。

你可以以任意大小写形式输出每个字母(例如,"YES"、"Yes"、"yes"、"yEs" 均会被识别为肯定回答)。

输入输出样例

  • 输入#1

    5
    aba
    abb
    abc
    aaaaa
    abcba

    输出#1

    YES
    NO
    YES
    YES
    NO

说明/提示

Let ft(c)f_t(c) represent the frequency of character cc in string tt.

For the first testcase we have

tt

ft(a)f_t(a)

ft(b)f_t(b)

aa

11

00

abab

11

11

abaaba

22

11

bb

00

11

baba

11

11

It can be seen that for any substring tt of ss, the difference between ft(a)f_t(a) and ft(b)f_t(b) is not more than 11. Hence the string ss is perfectly balanced.

For the second testcase we have

tt

ft(a)f_t(a)

ft(b)f_t(b)

aa

11

00

abab

11

11

abbabb

11

22

bb

00

11

bbbb

00

22

It can be seen that for the substring t=bbt=bb, the difference between ft(a)f_t(a) and ft(b)f_t(b) is 22 which is greater than 11. Hence the string ss is not perfectly balanced.

For the third testcase we have

tt

ft(a)f_t(a)

ft(b)f_t(b)

ft(c)f_t(c)

aa

11

00

00

abab

11

11

00

abcabc

11

11

11

bb

00

11

00

bcbc

00

11

11

cc

00

00

11

It can be seen that for any substring tt of ss and any two characters u,v∈a,b,cu,v\in{a,b,c}, the difference between ft(u)f_t(u) and ft(v)f_t(v) is not more than 11. Hence the string ss is perfectly balanced.

令 ft(c)f_t(c) 表示字符 cc 在字符串 tt 中的出现频数。

对于第一个测试用例,我们有

tt

ft(a)f_t(a)

ft(b)f_t(b)

aa

11

00

abab

11

11

abaaba

22

11

bb

00

11

baba

11

11

可以看出,对于 ss 的任意子串 tt,ft(a)f_t(a) 与 ft(b)f_t(b) 的差值均不超过 11。因此,字符串 ss 是完美平衡的。

对于第二个测试用例,我们有

tt

ft(a)f_t(a)

ft(b)f_t(b)

aa

11

00

abab

11

11

abbabb

11

22

bb

00

11

bbbb

00

22

可以看出,对于子串 t=bbt=bb,ft(a)f_t(a) 与 ft(b)f_t(b) 的差值为 22,大于 11。因此,字符串 ss 不是完美平衡的。

对于第三个测试用例,我们有

tt

ft(a)f_t(a)

ft(b)f_t(b)

ft(c)f_t(c)

aa

11

00

00

abab

11

11

00

abcabc

11

11

11

bb

00

11

00

bcbc

00

11

11

cc

00

00

11

可以看出,对于 ss 的任意子串 tt 以及任意两个字符 u,v∈{a,b,c}u,v\in\{a,b,c\},ft(u)f_t(u) 与 ft(v)f_t(v) 的差值均不超过 11。因此,字符串 ss 是完美平衡的。

输入解题思路,AI测评打分。不知道怎么写?

首页