• 【TP SRM 703 div2 250】AlternatingString


    Problem Statement

    A string of zeros and ones is called an alternating string if no two adjacent characters are the same. Examples of alternating strings: “1”, “10101”, “0101010101”. You are given a string s. Each character of s is a ‘0’ or a ‘1’. Please find the longest contiguous substring of s that is an alternating string. Return the length of that substring.
    Definition

    Class:
    AlternatingString
    Method:
    maxLength
    Parameters:
    string
    Returns:
    int
    Method signature:
    int maxLength(string s)
    (be sure your method is public)
    Limits

    Time limit (s):
    2.000
    Memory limit (MB):
    256
    Stack limit (MB):
    256

    Constraints

    s will contain between 1 and 50 characters, inclusive.

    Each character in s will be ‘0’ or ‘1’.
    Examples
    0)

    “111101111”
    Returns: 3
    Among all substrings, there are 5 different alternating strings: “1”, “0”, “10”, “01”, “101”. The one with maximal length is “101” and the length is 3.
    1)

    “1010101”
    Returns: 7
    The string s itself is an alternating string.
    2)

    “000011110000”
    Returns: 2
    Note that a substring must be contiguous. The longest alternating substrings of this s are “01” and “10”. The string “010” is not a substring of this s.
    3)

    “1011011110101010010101”
    Returns: 8

    4)

    “0”
    Returns: 1

    【题目链接】:

    【题解】

    找连续的01串。找长度最长的那个长度为多少.
    枚举下就可以了;
    应该也有O(n)的方法.
    就是直接顺序往前找就可以了.
    如果遇到了停顿的地方就尝试更新一下最大值并且让len=0;
    因为看到长度最大为50就写了个暴力

    【完整代码】

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <set>
    #include <map>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <string>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    //const int MAXN = x;
    const int dx[5] = {0,1,-1,0,0};
    const int dy[5] = {0,0,0,-1,1};
    const double pi = acos(-1.0);
    
    class AlternatingString
    {
        public:
        int maxLength(string s)
        {
            int len = s.size();
            int ans = 0;
            rep1(i,0,len-1)
                {
                    int j = i+1;
                    while (j<=len-1 && s[j]!=s[j-1])
                        j++;
                    ans = max(ans,j-i);
                }
            return ans;
        }
    };
  • 相关阅读:
    js检验文件格式
    java判空工具类
    $(document).ready() 是个什么函数?为什么要用它?
    Maven 手动添加jar
    java深克隆
    cors跨域详解
    常见异常类总结
    Spring事务回滚机制
    Java获取13位毫秒级时间戳
    JSON 字符串转换为 Map
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626821.html
Copyright © 2020-2023  润新知