牛妹和01串

题目描述
牛妹有一个01串,串中只包含0和1,牛妹要把这个串划分成连续的m段,使得每一段至少包含一个0和一个1。

牛妹想最大化m,m最大是多少呢?

输入描述:

输入包含一行一个01串S。保证中至少包含一个0和一个1。

输出描述:

输出一行一个整数表示答案。

输入

10101111000010101111010101

输出

9

参考代码

#include<bits/stdc++.h>

using namespace std;
string str;
int s1,s2,cnt;
int main()
{
    
    
	cin>>str;
	for(int i = 0;i < str.size();i++){
    
    
		if(str[i]=='0'){
    
    
			s1=1;
		}else{
    
    
			s2=1;
		}
		if(s1&&s2){
    
    
			cnt++;
			s1=s2=0;
		}
	}
	cout<<cnt<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/LXYDSF/article/details/120658345