• 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):

    1. #include
    2. using namespace std;
    3. #define endl '\n'
    4. #define int long long
    5. typedef long long ll;
    6. typedef unsigned long long ull;
    7. const int maxn = 2e5 + 10;
    8. const int INF = 0x3fffffff;
    9. int n;
    10. string s;
    11. signed main()
    12. {
    13. freopen("in.txt", "r", stdin);
    14. ios::sync_with_stdio(false);
    15. cin.tie(0);
    16. cout.tie(0);
    17. cout << fixed;
    18. cout.precision(18);
    19. int t;
    20. cin >> t;
    21. while(t--)
    22. {
    23. cin >> n;
    24. cin >> s;
    25. int cnt1 = count(s.begin(), s.end(), '0');
    26. int cnt = 1, Max = max(cnt1 * (n - cnt1), 1LL);
    27. for (int i = 1; i < n; i++)
    28. {
    29. if(s[i] == s[i - 1])
    30. cnt++;
    31. else
    32. cnt = 1;
    33. Max = max(Max, cnt * cnt);
    34. }
    35. cout << Max << endl;
    36. }
    37. return 0;
    38. }

  • 相关阅读:
    工具及方法 - 多邻国: Duolingo
    TensorFlow机器学习实战指南 PDF书籍推荐
    k8s dashboard安装部署实战详细手册
    R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用回归系数及其标准误计算每个系数对应的Z统计量的值
    MongoDB基础入门
    图解垃圾回收器运作机制
    Vue前后端项目开发指南(一)【前端项目的创建】
    Hadoop源码解析之Mapper数量计算公式
    基于安卓android微信小程序的食谱大全系统
    P31 JList列表框
  • 原文地址:https://blog.csdn.net/qq_45554473/article/details/127871672