CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) B. Maximum Substring 解题报告

原题链接:

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 <bits/stdc++.h>
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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值