zoukankan      html  css  js  c++  java
  • CodeForces 1363B.Subsequence Hate

    分析:一个二进制字符串,可以进行一些操作,把0改成1,把1改成0,求不包含010、101这样的子串,求最少的操作次数。

    子串是不连续的,所以我们可以得出,最终的结果只有4种,我们需要枚举分界点i,然后枚举两种情况,前面全是1的,后面全是0的,还有前面全是0的,后面全是1的,我们预处理一个0和1的前缀和数组,当我们处理前面全是1的,后面全是0的情况的时候,我们需要前面的0都反转过来,然后后面全是1的反转过来,然后去更新答案。为什么只枚举两种?因为当我们的i遍历到n + 1的时候,我们可以让左边全是1。另外两种情况同理。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    const int N = 1005;
    const int inf = 0x3f3f3f3f;
    char s[N];
    //0的个数
    int a[N];
    //1的个数
    int b[N];
    int main()
    {
    	int t;
    	scanf("%d", &t);
    
    	while (t--)
    	{
    		scanf("%s", s + 1);
    
    		int n = strlen(s + 1);
    
    		//0的个数,1的个数
    		int q1 = 0, q2 = 0;
    		int res = inf;
    		for (int i = 1; i <= n; ++i)
    		{
    			if (s[i] == '0') ++a[i], ++q1;
    			else ++b[i], ++q2;
    		}
    
    		for (int i = 1; i <= n; ++i)
    		{
    			a[i] = a[i] + a[i - 1];
    			b[i] = b[i] + b[i - 1];
    		}
    
    		//int q1 = a[n], q2 = b[n];
    
    		//枚举分界点
    		for (int i = 1; i <= n + 1; ++i)
    		{
    			//前面0,后面1
    			res = min(res, b[i - 1] + q1 - a[i - 1]);
    			res = min(res, a[i - 1] + q2 - b[i - 1]);
    		}
    
    		memset(a, 0, sizeof a);
    		memset(b, 0, sizeof b);
    
    		printf("%d
    ", res);
    	}
    	
    	return 0;
    }
    
    
    
    
    
    
    
    
    
    
    
    
  • 相关阅读:
    codeforces——模拟
    线段树水题
    编码格式分类: 前后端传递数据的编码格式contentType
    爬虫之爬取求职小网站
    auth 模块使用篇
    后端获取前端的多个数据用getlist
    字符串值的替换
    单例的5种开启方式
    forms 组件的功能和使用
    cookie和session 的初步介绍
  • 原文地址:https://www.cnblogs.com/pixel-Teee/p/13023314.html
Copyright © 2011-2022 走看看