【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;
}
};
【TP SRM 703 div2 250】AlternatingString的更多相关文章
- 【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 ...
- 【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 ...
- 【SRM 717 div2 B】LexmaxReplace
Problem Statement Alice has a string s of lowercase letters. The string is written on a wall. Alice ...
- 【SRM 717 div2 A】 NiceTable
Problem Statement You are given a vector t that describes a rectangular table of zeroes and ones. Ea ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- topcoder srm 628 div2 250 500
做了一道题,对了,但是还是掉分了. 第二道题也做了,但是没有交上,不知道对错. 后来交上以后发现少判断了一个条件,改过之后就对了. 第一道题爆搜的,有点麻烦了,其实几行代码就行. 250贴代码: #i ...
- topcoder srm 610 div2 250
第一次做tc 的比赛,一点也不懂,虽然题目做出来了, 但是,也没有在比赛的时候提交成功.. 还有,感谢一宁对tc使用的讲解.. 贴一下代码..... #include <cstring> ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- 【十大经典数据挖掘算法】PageRank
[十大经典数据挖掘算法]系列 C4.5 K-Means SVM Apriori EM PageRank AdaBoost kNN Naïve Bayes CART 我特地把PageRank作为[十大经 ...
随机推荐
- Tomcat之——配置项目有虚拟路径
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47024863 非常easy,在Tomcat的Server.xml文件里的Host节 ...
- 【Android 面试基础知识点整理】
针对Android面试中常见的一些知识点整理,Max 仅仅是个搬运工.感谢本文中引用文章的各位作者,给大家分享了这么多优秀文章.对于当中的解析,是原作者个人见解,有错误和不准确的地方,也请大家积极指正 ...
- ZOJ 2301 Color the Ball 线段树(区间更新+离散化)
Color the Ball Time Limit: 2 Seconds Memory Limit: 65536 KB There are infinite balls in a line ...
- js面向对象3-继承
一.了解继承 首先我们一起了解下js中继承,其实继承就是后辈继承前辈的属性和方法. 二.继承的方法 从父类继承属性和方法 这是对象冒充的方法,模仿java的继承方法.实现的原理是,通过改变父类的执行 ...
- CSS min-height不能解决垂直外边距合并问题
垂直外边距合并有一种情况是嵌套元素的垂直外边距合并,当父级元素没有设定外边距时,在顶部或者底部边缘的子元素的垂直外边距就会和父级的合并,导致父级也有了“隐形”的垂直外边距. 当父级元素的min-hei ...
- BZOJ2754: [SCOI2012]喵星球上的点名(AC自动机/后缀自动机)
Description a180285幸运地被选做了地球到喵星球的留学生.他发现喵星人在上课前的点名现象非常有趣. 假设课堂上有N个喵星人,每个喵星人的名字由姓和名构成.喵星球上的老师会选择M个串 ...
- CISP/CISA 每日一题 四
CISA 每日一题(答) 连续在线审计技术: 1.系统控制审计检查文件和内嵌审计模型(SCARF/EAM):非常复杂,适用于正常处理不能被中断:通过在组织的主机应用系统中内嵌经特别编写的审计软件,使审 ...
- 洛谷 P1626 象棋比赛
P1626 象棋比赛 题目描述 有N个人要参加国际象棋比赛,该比赛要进行K场对弈.每个人最多参加两场对弈,最少参加零场对弈.每个人都有一个与其他人不相同的等级(用一个正整数来表示). 在对弈中,等级高 ...
- Vue 学习记录<1>
1.环境搭建:(前提node.js搭建) # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue i ...
- 推荐一款优雅高效的免费在线APP原型工具
有段时间没有推荐干货给大伙了,今天是时候把压箱底的东西拿出来分享给大家了! 想要学习原型图绘制的小伙伴可以看过来,适合零基础的小白,五分钟就可以上手,绘制自己想要的产品原型图. 官方介绍:用户只需输入 ...