CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) B. Maximum Substring 解题报告
创始人
2024-01-21 12:32:06
0

原题链接:

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:

  • x⋅yx⋅y, if x>0x>0 and y>0y>0;
  • x2x2, if x>0x>0 and y=0y=0;
  • y2y2, if x=0x=0 and y>0y>0.

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。

代码(CPP):

#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++;elsecnt = 1;Max = max(Max, cnt * cnt);}cout << Max << endl;}return 0;
}

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...