0
点赞
收藏
分享

微信扫一扫

B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)

穆风1818 2022-06-27 阅读 43

原题链接: ​​https://codeforces.com/contest/1437/problem/B​​

B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_子串
测试样例

input
3
2
10
4
0110
8
11101000
output
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 s and get: 0110 → 0101.

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

  1. 11101000 → 10101100;
  2. 10101100 → 10101010.


题意: 给你一个长度为B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_子串_02的二进制字符串,其中B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_#define_03字符数量为B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_子串_04,B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_ios_05字符数量为B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_子串_04。你可以选择任意长度的连续子串进行翻转。现在你需要使得相邻字符不相等,问你需要进行的最小翻转次数。

解题思路: 这道题应该要做的就是样例分析,我们发现我们反转一个子字符串是不是只会影响子字符串两端的字符,而我们反转的目的就是为了使得相邻字符不相等。这也证实了我们通常选择反转的两端字符是不相等的。 故我们有了这个思想即可来做题目了,我们每反转一次就可以使两个连续的字符变得不连续。更有甚者我们可以进行的反转操作刚好使得远处的连续变得不连续。(连续字符不相同才可以)。如果这样还不太明白,我们来分析一下样例吧。

11100001
对于这个样例我们发现连续相同的长度为2的B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_#define_07子串有2个,连续相同的长度为B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_ios_08的子串有3个。这我们先不做结论,先来验证。
看我们首先要进行反转使得连续变为不连续,所以我们贪心考虑会进行如下操作:10110001。
这个时候我们发现连续相同的长度为2的B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_#define_07子串有1个,连续相同的长度为B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_ios_08的子串有2个。
那么我们还会继续进行操作,所以如下:
10101001.
这个时候我们发现连续相同的长度为2的B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_#define_07子串有0个,连续相同的长度为B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_ios_08的子串有1个。
到了这个时候我们只能贪心消掉一个子串了,故最后变为如下:
10101010.
我们发现了什么?每进行一次反转操作对应的减1,最后进行的最小操作数为max(长度为2的相同字符为B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_#define_07子串数,长度为2的相同字符为B. Reverse Binary Strings(思维)Educational Codeforces Round 97 (Rated for Div. 2)_ios_08子串数)。

则此题易解。

AC代码

/*
*
*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
string s;
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>n>>s;
int cnt0=0,cnt1=0;
rep(i,0,n-2){
if(s[i]==s[i+1]&&s[i]=='1'){
cnt1++;
}
if(s[i]==s[i+1]&&s[i]=='0'){
cnt0++;
}
}
cout<<max(cnt0,cnt1)<<endl;
}
}
return 0;
}


举报

相关推荐

0 条评论