博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #646 (Div. 2)B.Subsequence Hate(贪心)---题目+题解
阅读量:4034 次
发布时间:2019-05-24

本文共 2627 字,大约阅读时间需要 8 分钟。

来源:

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.

Input

The first line of the input contains a single integer t (1≤t≤100) — the number of test cases.

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

Output

For every string, output the minimum number of operations required to make it good.

Example

inputCopy
7
001
100
101
010
0
1
001100
outputCopy
0
0
1
1
0
0
2
Note
In test cases 1, 2, 5, 6 no operations are required since they are already good strings.

For the 3rd test case: “001” can be achieved by flipping the first character — and is one of the possible ways to get a good string.

For the 4th test case: “000” can be achieved by flipping the second character — and is one of the possible ways to get a good string.

For the 7th test case: “000000” can be achieved by flipping the third and fourth characters — and is one of the possible ways to get a good string.

题意:

给你一个只有0和1组成的字符串,你每次操作可以将某一位上的0翻转成1,1翻转成0。求至少反转多少次才能使得字符串中没有“101”或者“010”。

思路:

只有4种情况

0000000000000…11111111111111

1111111111111…00000000000000

00000000000000000000000000000

111111111111111111111111111111111

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

代码:

#include 
using namespace std;const int N = 1009;int n;char s[N];int a[N], b[N];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]; ans = min(ans, a[i] + last_zero); ans = min(ans, b[i] + last_one); } ans = min(ans, a[strlen(s + 1)]); ans = min(ans, b[strlen(s + 1)]); cout << ans << endl; }}

转载地址:http://uofdi.baihongyu.com/

你可能感兴趣的文章
Word Break
查看>>
Surrounded Regions
查看>>
Largest Rectangle in Histogram
查看>>
免费馅饼
查看>>
Common Subsequence
查看>>
Humble Numbers
查看>>
Merge Intervals
查看>>
Insert Interval
查看>>
Trapping Rain Water
查看>>
Pow(x, n)
查看>>
Sort Colors
查看>>
Restore IP Addresses
查看>>
Word Break II
查看>>
Anagrams
查看>>
Letter Combinations of a Phone Number
查看>>
Combination Sum
查看>>
Combination Sum II
查看>>
Word Search
查看>>
Count and Say
查看>>
Palindrome Partitioning
查看>>