交换


嚌㔸䎣簵(解题思路)
解释:求一串 010101 串中最长连续的 111 的长度(开头和结尾视为同一段)
那不就简单了,用一个 max\maxmax 求最长的 111,前后相加,特判全为 111 的情况即可。
code
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
string a;
int n,s,maxn,ts;
int main()
{
cin>>a;
n=a.size();
for(int i=0;i<n;i++)
if(a[i]=='1')
{
if(ts==i)
ts++;
s++;
maxn=max(maxn,s);
}
else
s=0;
if(a[n-1]=='1'&&ts!=n)
maxn=max(maxn,s+ts);
cout<<maxn<<endl;
}
本文介绍了一种求解字符串中最长连续1串的方法。通过遍历字符串并使用计数器来跟踪当前连续1串的长度,同时更新最大长度记录。特别地,考虑到了字符串开始和结束位置的情况。
494

被折叠的 条评论
为什么被折叠?



