Problem - B - Codeforces (Unofficial mirror by Menci)
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†† tt of string ss containing xx characters 0 and yy characters 1, define its cost as:
Given a binary string ss of length nn, find the maximum cost across all its non-empty substrings.
†† 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.
Input
Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051≤t≤105) — 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≤n≤2⋅105) — 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⋅105.
Output
For each test case, print a single integer — the maximum cost across all substrings.
Example
给定一个01字符串s,我们定义一个子串的价值为:
1、如果子串是纯1或者纯0,则为字符串长度的平方。
2、如果子串是01混合串,则为子串中0和1各自个数的乘积。
找出最大的子串价值。
要么操作整个数组(因为混合串能得到的最大的必然是整个串),要么找到最大的连续1或相同0。
- #include
- using namespace std;
- #define endl '\n'
- #define int long long
- typedef long long ll;
- typedef unsigned long long ull;
- const int maxn = 2e5 + 10;
- const int INF = 0x3fffffff;
- int n;
- string s;
-
- signed main()
- {
- freopen("in.txt", "r", stdin);
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- cout << fixed;
- cout.precision(18);
-
- int t;
- cin >> t;
- while(t--)
- {
- cin >> n;
- cin >> s;
- int cnt1 = count(s.begin(), s.end(), '0');
- int cnt = 1, Max = max(cnt1 * (n - cnt1), 1LL);
- for (int i = 1; i < n; i++)
- {
- if(s[i] == s[i - 1])
- cnt++;
- else
- cnt = 1;
- Max = max(Max, cnt * cnt);
- }
- cout << Max << endl;
- }
- return 0;
- }