• Codeforces 1750B. Maximum Substring


    B. Maximum Substring

    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† t of string s containing x characters 0 and y characters 1, define its cost as:

    x⋅y, if x>0 and y>0;
    x2, if x>0 and y=0;
    y2, if x=0 and y>0.
    Given a binary string s of length n, find the maximum cost across all its non-empty substrings.

    † 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.

    Input
    Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤105) — the number of test cases. The description of test cases follows.

    The first line of each test case contains a single integer n (1≤n≤2⋅105) — 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⋅105.

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

    Example
    inputCopy
    6
    5
    11100
    7
    1100110
    6
    011110
    7
    1001010
    4
    1000
    1
    0
    outputCopy
    9
    12
    16
    12
    9
    1
    Note
    In the first test case, we can take a substring 111. It contains 3 characters 1 and 0 characters 0. So a=3, b=0 and its cost is 32=9.

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

    In the third test case, we can can take a substring 1111 and its cost is 42=16.

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

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

    In the sixth test case, we can only take the substring 0 and its cost is 1⋅1=1.

    #include
    using namespace std;
    const int N = 12;
    typedef long long ll;
    void solve() {
    	ll n;
    	string s;
    	cin >> n >> s;
    	ll len1 = 0, len0 = 0;
    	ll maxv0=0, maxv1 = 0;
    	if (s[n - 1] == '0')s[n] = '1';
    	else s[n] = '0';
     
    	ll len00=0, len11=0;
    	for (int i = 0; i < n; i++) {
    		if (s[i] == '0') {
    			len0++;
    			len00++;
    		}
    		if (s[i] == '0' && s[i + 1] == '1') {
    			maxv0 = max(maxv0, len0);
    			len0 = 0;
    		}
    		if (s[i] == '1') {
    			len1++;
    			len11++;
    		}
    		if (s[i] == '1' && s[i + 1] == '0') {
    			maxv1 = max(maxv1, len1);
    			len1 = 0;
    		}
    	}
    	ll p = max(pow(maxv0, 2), pow(maxv1, 2));
    	ll maxv = max(p, len00 * len11);
    	cout << maxv << endl;
    }
    int main(void) {
    	int t; 
    	cin >> t;
    	while (t--) {
    		solve();
    	}
    }
    //code by yxisme;
    //code by 01100_10111;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
  • 相关阅读:
    在Linux上安装Oracle相关产品,常用文件的默认路径
    redis6.0引入多线程
    使用NFS作为Glance存储后端
    Python+selenium自动化环境搭建(安装selenium API库、下载(webdriver)驱动)
    你不知道的JavaScript性能测试 之 两个变量值交换
    Yarn安装报错和使用
    100天精通Python(基础篇)——第8天:字符串的三种定义
    小程序云开发笔记二
    TMS320C6678 DSP + Xilinx Kintex-7 FPGA核心板参数资料规格书手册
    一图看懂,阿里云飞天企业版如何支持政企数智创新
  • 原文地址:https://blog.csdn.net/qq_60775360/article/details/127731460