cf1363B——Subsequence Hate

Subsequence Hate
d e c e p t i o n \color{blue}deception deception
Shubham has a binary string s. A binary string is a string containing only characters “0” and “1”.

He can perform the following operation on the string any amount of times:

Select an index of the string, and flip the character at that index. This means, if the character was “0”, it becomes “1”, and vice versa.
A string is called good if it does not contain “010” or “101” as a subsequence — for instance, “1001” contains “101” as a subsequence, hence it is not a good string, while “1000” doesn’t contain neither “010” nor “101” as subsequences, so it is a good string.

What is the minimum number of operations he will have to perform, so that the string becomes good? It can be shown that with these operations we can make any string good.

A string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters.

I n p u t \color{blue}Input Input
The first line of the input contains a single integer t ( 1 ≤ t ≤ 100 ) t (1≤t≤100) t(1t100) — the number of test cases.

Each of the next t t t lines contains a binary string s s s ( 1 ≤ ∣ s ∣ ≤ 1000 ) (1≤|s|≤1000) (1s1000).

O u t p u t \color{blue}Output Output
For every string, output the minimum number of operations required to make it good.

对样例进行分析,可以得出几种好的字符串的形式

001->001
100->100
101->001
010->000
0->0
1->1
001100->000000

猜的出来,要么全0,要么全1,要么一边是0,一边是1,要么反过来。
全1全0的情况最好解决,遍历一遍算出0和1的总数,然后取最小值就行了,我们用 s u m 0 sum0 sum0 s u m 1 sum1 sum1来表示0和1的总数, a n s = m i n ( s u m 0 , s u m 1 ) ans=min(sum0,sum1) ans=min(sum0,sum1)

假设我们在字符串的第 i i i个位置,它前面有 p r e 0 pre0 pre0个0和 p r e 1 pre1 pre1个1,现在我要把它变成 000. i . 111 000.i.111 000.i.111这种形式,那么首先先把前面的1变成0,需要 p r e 1 pre1 pre1次,再把后面的0变成1,需要 s u m 0 − p r e 0 sum0-pre0 sum0pre0次,那么总的就是 p r e 1 + s u m 0 − p r e 0 pre1+sum0-pre0 pre1+sum0pre0,同理,变成 111. i . 000 111.i.000 111.i.000的形式同样可以得出来,是 p r e 0 + s u m 1 − p r e 1 pre0+sum1-pre1 pre0+sum1pre1,那么答案 a n s = m i n ( a n s , m i n ( p r e 1 + s u m 0 − p r e 0 , p r e 0 + s u m 1 − p r e 1 ) ) ans=min(ans,min(pre1+sum0-pre0,pre0+sum1-pre1)) ans=min(ans,min(pre1+sum0pre0,pre0+sum1pre1))

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-7;
#define endl '\n'
#define pb(a) push_back(a)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) int(x.size())

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        string s;
        cin >> s;
        int sum1 = 0, sum0 = 0, ans = 0;
        for (int i = 0; i < SIZE(s); ++i) {
            sum1 += (s[i] == '1') ? 1 : 0;
            sum0 += (s[i] == '0') ? 1 : 0;
        }
        ans = min(sum1, sum0);  //全1或全0的情况
        int pre0 = 0, pre1 = 0;
        for (int i = 0; i < SIZE(s); ++i) {
            pre0 += (s[i] == '0') ? 1 : 0;
            pre1 += (s[i] == '1') ? 1 : 0;
            ans = min(ans, pre1 + (sum0 - pre0));  // 000..11
            ans = min(ans, pre0 + (sum1 - pre1));  // 111.00
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值