B. Subsequence Hate(贪心思维)

101 010 子序列中不能出现101和010

? 1 0 什么意思?就是说1和0不能交替来回放置

4 只有4种情况

0000000000000.......11111111111111 0000000000000.......11111111111111

1111111111111.......00000000000000 1111111111111.......00000000000000

0000000000000000000000000000000 0000000000000000000000000000000

11111111111111111111111111111111 11111111111111111111111111111111

, 1 , 0 全部考虑一下,用前缀和记录前面有多少个1,多少个0计算一下就好了

#include <bits/stdc++.h>
using namespace std;
const int maxn=1009;
int n;
char s[maxn];
int a[maxn],b[maxn];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int ans=1e9;
		cin>>(s+1);
		int one=0,zero=0;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		for(int i=1;i<=strlen(s+1);i++)
		{
			a[i]=a[i-1],b[i]=b[i-1];
			if(s[i]=='0')	zero++,b[i]++;
			else	one++,a[i]++;
		}
		for(int i=1;i<=strlen(s+1);i++)
		{
			int last_one=one-a[i];
			int last_zero=zero-b[i];
			//ǰ��0,����1
			ans=min(ans,a[i]+last_zero);
			//ǰ��1������0 
			ans=min(ans,b[i]+last_one); 
		}
		//ȫ0
		ans=min(ans,a[strlen(s+1)]);
		//ȫ1 
		ans=min(ans,b[strlen(s+1)]);
		cout<<ans<<endl; 
	}
}

猜你喜欢

转载自blog.csdn.net/jziwjxjd/article/details/106464628