CF1750B.Maximum Substring

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A binary string is a string consisting only of the characters 0 and 1. You are given a binary string ss.

For some non-empty substring†^\dagger tt of string ss containing xx characters 0 and yy characters 1, define its cost as:

  • x⋅yx \cdot y, if x>0x \gt 0 and y>0y \gt 0;
  • x2x^2, if x>0x \gt 0 and y=0y = 0;
  • y2y^2, if x=0x = 0 and y>0y \gt 0.

Given a binary string ss of length nn, find the maximum cost across all its non-empty substrings.

†^\dagger A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

二进制字符串是指仅由字符 0 和 1 组成的字符串。现给定一个二进制字符串 ss。

对于字符串 ss 的某个非空子串†^\dagger tt,设其包含 xx 个字符 0 和 yy 个字符 1,则定义其代价为:

  • 若 x>0x > 0 且 y>0y > 0,代价为 x⋅yx \cdot y;
  • 若 x>0x > 0 且 y=0y = 0,代价为 x2x^2;
  • 若 x=0x = 0 且 y>0y > 0,代价为 y2y^2。

给定长度为 nn 的二进制字符串 ss,求其所有非空子串中的最大代价。

†^\dagger 字符串 aa 是字符串 bb 的子串,当且仅当 aa 可通过从 bb 的开头删除若干(可能为零或全部)字符、并从结尾删除若干(可能为零或全部)字符而得到。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051 \leq t \leq 10^5) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of the string ss.

The second line of each test case contains a binary string ss of length nn.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的二进制字符串 ss。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print a single integer — the maximum cost across all substrings.

对于每个测试用例,输出一个整数——所有子串中的最大代价。

输入输出样例

  • 输入#1

    6
    5
    11100
    7
    1100110
    6
    011110
    7
    1001010
    4
    1000
    1
    0

    输出#1

    9
    12
    16
    12
    9
    1

说明/提示

In the first test case, we can take a substring 111111. It contains 33 characters 1 and 00 characters 0. So a=3a = 3, b=0b = 0 and its cost is 32=93^2 = 9.

In the second test case, we can take the whole string. It contains 44 characters 1 and 33 characters 0. So a=4a = 4, b=3b = 3 and its cost is 4⋅3=124 \cdot 3 = 12.

In the third test case, we can can take a substring 11111111 and its cost is 42=164^2 = 16.

In the fourth test case, we can take the whole string and cost is 4⋅3=124 \cdot 3 = 12.

In the fifth test case, we can take a substring 000000 and its cost is 3⋅3=93 \cdot 3 = 9.

In the sixth test case, we can only take the substring 00 and its cost is 1⋅1=11 \cdot 1 = 1.

在第一个测试用例中,我们可以选取子串 111111。它包含 33 个字符 1 和 00 个字符 0。因此 a=3a = 3,b=0b = 0,其代价为 32=93^2 = 9。

在第二个测试用例中,我们可以选取整个字符串。它包含 44 个字符 1 和 33 个字符 0。因此 a=4a = 4,b=3b = 3,其代价为 4⋅3=124 \cdot 3 = 12。

在第三个测试用例中,我们可以选取子串 11111111,其代价为 42=164^2 = 16。

在第四个测试用例中,我们可以选取整个字符串,其代价为 4⋅3=124 \cdot 3 = 12。

在第五个测试用例中,我们可以选取子串 000000,其代价为 3⋅3=93 \cdot 3 = 9。

在第六个测试用例中,我们只能选取子串 00,其代价为 1⋅1=11 \cdot 1 = 1。

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

首页