欢迎光临散文网 会员登陆 & 注册

CF 1569A - Balanced Substring

2023-06-26 15:13 作者:您是打尖儿还是住店呢  | 我要投稿

You are given a string s, consisting of n letters, each letter is either 'a' or 'b'. The letters in the string are numbered from 1 to n.

s[l;r] is a continuous substring of letters from index l to r of the string inclusive.

A string is called balanced if the number of letters 'a' in it is equal to the number of letters 'b'. For example, strings "baba" and "aabbab" are balanced and strings "aaab" and "b" are not.

Find any non-empty balanced substring s[l;r] of string s. Print its l and r (1≤l≤r≤n). If there is no such substring, then print −1 −1.

----------------------------------------

给定一个字符串 s,由 n 个字母组成,每个字母要么是“a”,要么是“b”。 字符串中的字母从 1 到 n 编号。

s[l;r] 是字符串中从索引 l 到 r 的连续字母子串。

如果字符串中字母“a”的数量等于字母“b”的数量,则该字符串被称为平衡字符串。 例如,字符串“baba”和“aabbab”是平衡的,而字符串“aaab”和“b”则不是。

查找字符串 s 的任意非空平衡子串 s[l;r]。 打印它的 l 和 r (1≤l≤r≤n)。 如果不存在这样的子串,则打印−1 −1。

-----------------------------------------------

用presum来求和,然后依次去判断即可;

下面是代码:


CF 1569A - Balanced Substring的评论 (共 条)

分享到微博请遵守国家法律