Codeforces1437 B. Reverse Binary Strings

2024-04-15 23:38

本文主要是介绍Codeforces1437 B. Reverse Binary Strings,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

You are given a string 𝑠 of even length 𝑛. String 𝑠 is binary, in other words, consists only of 0’s and 1’s.

String 𝑠 has exactly 𝑛2 zeroes and 𝑛2 ones (𝑛 is even).

In one operation you can reverse any substring of 𝑠. A substring of a string is a contiguous subsequence of that string.

What is the minimum number of operations you need to make string 𝑠 alternating? A string is alternating if 𝑠𝑖≠𝑠𝑖+1 for all 𝑖. There are two types of alternating strings in general: 01010101… or 10101010…

Input
The first line contains a single integer 𝑡 (1≤𝑡≤1000) — the number of test cases.

The first line of each test case contains a single integer 𝑛 (2≤𝑛≤105; 𝑛 is even) — the length of string 𝑠.

The second line of each test case contains a binary string 𝑠 of length 𝑛 (𝑠𝑖∈ {0, 1}). String 𝑠 has exactly 𝑛2 zeroes and 𝑛2 ones.

It’s guaranteed that the total sum of 𝑛 over test cases doesn’t exceed 105.

Output
For each test case, print the minimum number of operations to make 𝑠 alternating.

Example
inputCopy
3
2
10
4
0110
8
11101000
outputCopy
0
1
2
Note
In the first test case, string 10 is already alternating.

In the second test case, we can, for example, reverse the last two elements of 𝑠 and get: 0110 → 0101.

In the third test case, we can, for example, make the following two operations:

11101000 → 10101100;
10101100 → 10101010.

题意:
01序列,你可以翻转一段数使得0变成1,1变成0。
求翻转最少的段使得序列变成010101…或者101010…。

思路:
要是一个区间同时被翻转两次,那就等于没翻转,所以可以推出翻转的段一定是不交叉的。因此只需要枚举最终序列是1010,还是0101,然后算出要翻转多少段即可。

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;typedef long long ll;
const int maxn = 2e5 + 7;int a[maxn];
int main() {int T;scanf("%d",&T);while(T--) {int n;scanf("%d",&n);int ans1 = 0,ans2 = 0;int flag = 0; //之前是否加过for(int i = 1;i <= n;i++) {scanf("%1d",&a[i]);if(a[i] != i % 2) {if(!flag) {ans1++;flag = 1;}} else {flag = 0;}}flag = 0;for(int i = 1;i <= n;i++) {if(a[i] != !(i % 2)) {if(!flag) {ans2++;flag = 1;}} else {flag = 0;}}printf("%d\n",min(ans1,ans2));}return 0;
}

这篇关于Codeforces1437 B. Reverse Binary Strings的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907242

相关文章

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro

leetcode#541. Reverse String II

题目 Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of

[LeetCode] 7. Reverse Integer

题:https://leetcode.com/problems/reverse-integer/description/ 题目 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123Output: 321Example 2:Input: -123Output: -321Ex

[LeetCode] 583. Delete Operation for Two Strings

题:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目 Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in

[LeetCode] 190. Reverse Bits

题:https://leetcode.com/problems/reverse-bits/ 题目大意 将32位的数,二进制翻转。 解题思路 解法和 将int a =123,翻转的思路 相同。 int b= 0;while(a>0){b = b*10 + a %10;a /=10;} 将新数整体左移一位,然后 取每个数的第i位置,将该位放到 新数的最低位。循环32次遍可以得到翻转。

[LeetCode] 863. All Nodes Distance K in Binary Tree

题:https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/ 题目大意 求给树中,距给定 结点 指定长度的 所有结点的val 思路 tree -> graph 、 bfs 先遍历树,并用map记录每个结点的父结点 ,将树变为图,然后 bfs。 /*** Definition for a binary tree

LeetCode 67 Add Binary

题意: 两个二进制数相加,输出结果 思路: 各种模拟均可,比如先把A和B倒过来,再按位相加,最后把结果再倒回来。 不过为了快,我是这样做的——假设A比B长,那么我对位相加B的长度。这时如果没有进位,那么A长出B的部分就不会变了;如果有进位,那么继续往A的高位加,直到某一次进位为0,那么更高位的A就不变了;如果直到最后还有进位,那就最前面添加一个最高位1。 代码: cla

Classical Binary Search

Find any position of a target number in a sorted array. Return -1 if target does not exist. Example Example 1: Input: nums = [1,2,2,4,5,5], target = 2Output: 1 or 2 Example 2: Input: nums = [1,

Cousins in binary tree

Input: root = [1,2,3,null,4,null,5], x = 5, y = 4Output: true Input: root = [1,2,3,4], x = 4, y = 3Output: false 思路:就是level order traverse,BFS,记录一下parent, curNode, Depth; /*** Definition for