文章目录
@[TOC]H: 交换
题目描述
给一个长度为n的01序列s[1],s[2],…,s[n],现在可以至多进行1次如下操作:
选择1≤x<n,将s序列变成s[x+1],s[x+2],……s[n],s[1],s[2],….s[x]
输出最长的全为1的子区间长度。
输入
一个01字符串,表示序列s。(1<=|s|<=100000)
输出
输出一个整数表示答案。
样例输入
1001
11111
10111010
样例输出
2
5
3
主要就是判断头尾的1
1和0间隔就不能构成连续
//【AC的代码】
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(cin >> s)
{
int len = s.size();
int x = 0, num1 = 0, num2 = 0, ans = 0;
if(s[0] == '1')
{
while(s[x] == '1')
{
num1 ++;
x ++;
}
ans = num1;
}
for(int i = x ; i < len ; i++)
{
num2 = 0;
while(s[i] == '1')
{
num2 ++;
i ++;
}
ans = max(ans, num2);
}
if(s[len - 1] == '1'){
ans = max(ans, num1 + num2);
}
cout << ans << endl;
}
return 0;
}