codeforces#505--C Plasticine Zebra
C. Plasticine zebra
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Is there anything better than going to the zoo after a tiresome week at work? No wonder Grisha feels the same while spending the entire weekend accompanied by pretty striped zebras.
Inspired by this adventure and an accidentally found plasticine pack (represented as a sequence of black and white stripes), Grisha now wants to select several consequent (contiguous) pieces of alternating colors to create a zebra. Let's call the number of selected pieces the length of the zebra.
Before assembling the zebra Grisha can make the following operation 00 or more times. He splits the sequence in some place into two parts, then reverses each of them and sticks them together again. For example, if Grisha has pieces in the order "bwbbw" (here 'b' denotes a black strip, and 'w' denotes a white strip), then he can split the sequence as bw|bbw (here the vertical bar represents the cut), reverse both parts and obtain "wbwbb".
Determine the maximum possible length of the zebra that Grisha can produce.
Input
The only line contains a string ss (1≤|s|≤1051≤|s|≤105, where |s||s| denotes the length of the string ss) comprised of lowercase English letters 'b' and 'w' only, where 'w' denotes a white piece and 'b' denotes a black piece.
Output
Print a single integer — the maximum possible zebra length.
Examples
input
Copy
bwwwbwwbw
output
Copy
5
input
Copy
bwwbwwb
output
Copy
3
Note
In the first example one of the possible sequence of operations is bwwwbww|bw →→ w|wbwwwbwb →→ wbwbwwwbw, that gives the answer equal to 55.
In the second example no operation can increase the answer.
同时翻转其实相当于把字符串看成是首尾连接的环
所以先把字符串后面接一个与自己一样的字符串
从头到尾【原字符串】枚举最长的长度 当然不可以超过原字符串的长度
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 5;
string s;
int main()
{
while(cin>>s){
s += s;
int maxlen = 0;
for(int i = 0; i < s.length() / 2; ){
char now = s[i];
for(int j = i + 1; j - i <= s.length() / 2; j++){
if(s[j] != now){
now = s[j];
if(j - i == s.length() / 2){
maxlen = max(maxlen, j - i);
}
}
else{
maxlen = max(maxlen, j - i);
i = j;
break;
}
}
if(maxlen == s.length() / 2){
break;
}
}
printf("%d\n", maxlen);
}
return 0;
}
codeforces#505--C Plasticine Zebra的更多相关文章
- CF1025C Plasticine zebra 思维 字符串
Plasticine zebra time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C. Plasticine zebra
问了学长,感觉还是很迷啊,不过懂了个大概,这个翻转操作,实质不就是在序列后面加上前面部分比如 bw | wwbwwbw 操作过后 wbwbwwbww 而 bw | wwbwwbwbw 这样我们就知道 ...
- Codeforces #505(div1+div2) C Plasticine zebra
题意:给你一段字符串,可以选择任意多的位置,每个位置会反转两边的字符串,问交错的字符串最长是多长? 思路:找规律,仔细分析样例1.假设位置为 1 2 3 4 5 6 7 8 9,反转之后会发现答案是7 ...
- CF 1025C Plasticine zebra
昨晚忘记判只有一个字符的情况fst了呜呜呜 挺有趣的题,昨晚连刚带猜弄出结论 考虑答案的取值,最优答案可能是一个后缀,或者是一个前缀,或者是一个后缀加上前缀 那么翻转之后最优答案的可选值就有了1的前缀 ...
- Codeforces 505 A Mr. Kitayuta's Gift【暴力】
题意:给出一个字符串,可以向该字符串的任意位置插入一个字母使其变成回文串 因为字符串的长度小于10,枚举插入的字母,和要插入的位置,再判断是否已经满足回文串 #include<iostream& ...
- CF1025C Plasticine zebra【环状字符串/思维】
给你一个长度为 \(\left|s\right|\) 的01串 \(s\) ,每次操作你可以任选一个 \(k\) ,使01串的 \([1,k]\) 和 \((k,\left|s\right|]\) 分 ...
- Codeforces #505(div1+div2) D Recovering BST
题意:给你一个升序的数组,元素之间如果gcd不为1可以建边,让你判断是否可以建成一颗二叉搜索树. 解法:dp,首先建图,然后进行状态转移.因为如果点k左端与i相连,右端与k相连,则i和k可以相连,同时 ...
- Codeforces #505(div1+div2) B Weakened Common Divisor
题意:给你若干个数对,每个数对中可以选择一个个元素,问是否存在一种选择,使得这些数的GCD大于1? 思路:可以把每个数对的元素乘起来,然后求gcd,这样可以直接把所有元素中可能的GCD求出来,从小到大 ...
- CF C. Plasticine zebra (思维)
题意: 是输入一个只有'w','b'的字符串,可以对他的任意位置切割成两个子串,切割后的右边的子串翻转后再和左边的子串拼起来会得到一个新的字符串,操作次数不限,问能得到的字符串中wb交替出现的最大的长 ...
随机推荐
- 科技发烧友之单反佳能700d中高端
http://detail.zol.com.cn/series/15/15795_1.html 前三 佳能 尼康 索尼 佳能5d 1.6w 佳能70d 5k 佳能6d 9k 佳能d7100 5k 尼康 ...
- 记js的一个奇葩问题!!!!!!!!!!!!
<a id='gc-yl' onclick='Preview('" + imgPath + "')' style='margin-left:5px;' >预览</ ...
- Kubernetes(一)初探
Kubernetes是Google开源的容器集群管理系统.它构建于docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等整一套功能,本质上可看作是基于容器技术的mini-Pa ...
- ThinkPHP Mongo驱动update方法支持upsert参数
Mongo数据库update操作有一个相对于Mysql的关键特性,它可以使用upsert模式,当更新的数据不存在时,直接插入,但是ThinkPHP的Mongo驱动居然不支持这一特性,没办法,自力更生了 ...
- Spring------SpringBoot参考书籍
转载: http://download.csdn.net/download/plus_dy/8972653
- 超全面的JavaWeb笔记day07<Java基础加强>
1.myeclipse安装和使用(**) 2.debug调试模式(**) - F6: 单步执行 - F8:结束断点,后面有断点到下一个断点 3.myeclipse快捷键(**) 4.junit单元测试 ...
- Leetcode: Best Time to Buy and Sell Stock I, II
思路: 1. 算法导论讲 divide and conquer 时, 讲到过这个例子. 书中的做法是先让 price 数组减去一个值, 然后求解最大连续子数组的和. 分治算法的复杂度为 o(nlogn ...
- 监控之_nrpe
监控机上安装nagios插件和nrpe(nrpe添加为xinetd服务) 1.添加nagios用户 /usr/sbin/useradd nagios passwd nagios 2.安装nagio ...
- nohub和重定向文件
1.如果使用远程连接的Linux的方式并想后台运行执行如下命令: 格式:nohup <程序名> & 比如:nohup /usr/local/collection/bin/start ...
- 切换sprite
using UnityEngine; using System.Collections; public class BTN : MonoBehaviour { void Awake () { //s ...