A13527 | Subsequence Hate
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line of the input contains a single integer $t$ $(1\le t \le 100)$ — the number of test cases.
Each of the next $t$ lines contains a binary string $s$ $(1 \le |s| \le 1000)$ .
Each of the next $t$ lines contains a binary string $s$ $(1 \le |s| \le 1000)$ .
输出格式
For every string, output the minimum number of operations required to make it good.
输入输出样例
输入 #1
7 001 100 101 010 0 1 001100
输出 #1
0 0 1 1 0 0 2
In test cases $1$ , $2$ , $5$ , $6$ no operations are required since they are already good strings.
For the $3$ rd 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 $4$ th 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 $7$ th 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.
For the $3$ rd 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 $4$ th 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 $7$ th 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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted