题库练习 Maximum Substring
← 上一题 下一题 →

A15544 | Maximum Substring

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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

For some non-empty substring $^\dagger$ $t$ of string $s$ containing $x$ characters 0 and $y$ characters 1, define its cost as:

- $x \cdot y$ , if $x > 0$ and $y > 0$ ;
- $x^2$ , if $x > 0$ and $y = 0$ ;
- $y^2$ , if $x = 0$ and $y > 0$ .

Given a binary string $s$ of length $n$ , find the maximum cost across all its non-empty substrings.

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

输入格式

Each test consists of multiple test cases. The first line contains a single integer $t$ ( $1 \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 $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the length of the string $s$ .

The second line of each test case contains a binary string $s$ of length $n$ .

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \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
C++ 编辑器
输入
输出