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;
}
};

【TP SRM 703 div2 250】AlternatingString的更多相关文章

  1. 【TP SRM 703 div2 500】 GCDGraph

    Problem Statement You are given four ints: n, k, x, and y. The ints n and k describe a simple undire ...

  2. 【SRM 717 DIV2 C】DerangementsDiv2

    Problem Statement You are given two ints: n and m. Let D be the number of permutations of the set {1 ...

  3. 【SRM 717 div2 B】LexmaxReplace

    Problem Statement Alice has a string s of lowercase letters. The string is written on a wall. Alice ...

  4. 【SRM 717 div2 A】 NiceTable

    Problem Statement You are given a vector t that describes a rectangular table of zeroes and ones. Ea ...

  5. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  6. topcoder srm 628 div2 250 500

    做了一道题,对了,但是还是掉分了. 第二道题也做了,但是没有交上,不知道对错. 后来交上以后发现少判断了一个条件,改过之后就对了. 第一道题爆搜的,有点麻烦了,其实几行代码就行. 250贴代码: #i ...

  7. topcoder srm 610 div2 250

    第一次做tc 的比赛,一点也不懂,虽然题目做出来了, 但是,也没有在比赛的时候提交成功.. 还有,感谢一宁对tc使用的讲解.. 贴一下代码..... #include <cstring> ...

  8. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  9. 【十大经典数据挖掘算法】PageRank

    [十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 我特地把PageRank作为[十大经 ...

随机推荐

  1. linux常用命令之lsof 、netstat、ipcs、ldd

    一.lsof lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.每行 ...

  2. open阶段的一致性检验(二)

    SQL> select * from v$version where rownum=1; BANNER --------------------------------------------- ...

  3. apache2 虚拟机多用户多站点设置 mpm-itk

    MPM设置 https://bbs.csdn.net/topics/390479795/ vim /opt/lampp/etc/extra/httpd-ssl.conf vim /opt/lampp/ ...

  4. UDP 打洞示例 包含 服务器 客户端

    客户端示例: #include "Net.h" #include "../p2pInfo.h" int main() { CUdp  udp; if (0!=u ...

  5. NET Framework 4.5 有更加简便的方法 Task.Run()

    NET Framework 4.5 有更加简便的方法 Task.Run()

  6. 使IIS服务器支持下载 apk/ipa 安装包

    默认情况下,使用IIS作为Web服务器的无法下载此文件,访问会触发404错误,服务器找不到对应资源. IIS服务器不能下载.apk文件的原因:iis的默认MIME类型中没有.apk文件,所以无法下载. ...

  7. Node.js安装+环境配置【Windows版】

    Node.js安装及环境配置之Windows篇  一.安装环境 1.本机系统:Windows 10 Pro(64位)2.Node.js:v6.9.2LTS(64位) 二.安装Node.js步骤 1.下 ...

  8. [置顶] MVC三层架构在各框架中的特征

    1.从结构上分析jsp+servlet图解原理: 在基于mvc设计模式下的最原始的jsp+Servlet框架,在某种程度上是不能够达到mvc最直观的体现.当客户端发送请求到服务器时,服务器会将从客户端 ...

  9. canvas画板基础应用的学习

    canvas是html5中的绘图容器,我们可以通过javascript的控制来进行图形的绘制,绘制对象可以是路径.盒.圆.字符等,同时,你也可以通过js给画布添加图像,下面来介绍canvas的各种基本 ...

  10. spring与cxf整合配置webservice接口(以jaxws:server的方式配置)

    ps:最近项目需要跟其他系统做同步,需要使用webservice来提供接口给其他系统调用:临时抱佛脚赶紧去网上找了下资料,发现用Endpoint的方式发布接口好容易哦:赶紧写了个例子做验证,发布成功. ...