Binary Strings

                                          Binary Strings

题目描述:

            这个题目讲述的是,给定两个字符串AB,这两个字符串由0和1组成,给定了两个操作,第一个操作是可以将字符串的最后一个字符移到字符串的最前面,第二个操作是可以将连续的两个字符反转,这个题目是将第一个字符和最后一个字符视为连续,然后题目问我们最少可以用多少次第二个操作来使字符串A变成B,如果不能通过以上两个操作使字符串A变成B的话,那么输出-1。

题目分析:

            首先,我们先判断-1的情况,我们会发现第一个操作只是改变字符串各个字符之间的位置,没有改变到0和1的数量,而第二种操作可以分为四种情况:00,01,10以及11。经过反转之后,可以变成11,10,01,00,我们可以发现0增加或者减少的数量以及1增加或者减少的数量均为偶数。因此,可以得出结论,如果字符串A中0的数量与字符串B中0的数量奇偶性不一样的话,输出-1。

            接下来,我们要求出从字符串A变成B的最少操作次数,首先我们要知道,不同位置实行第二个操作来使字符串A变成B,操作次数是不一样的,因此我们要对字符串A的每个位置进行枚举,然后从这个位置开始进行第二个操作,然后使字符串A变成B,求出最少的操作次数。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <deque>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define eps 1e-3
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int Maxn=5005;
char A[Maxn*2],B[Maxn];
int n,ans;
void solve(char *s,int cnt)
{
    for (int i=0;i<n-1;i++)
    if (s[i]!=B[i])
    {
        s[i]='1'-(s[i]-'0');
        s[i+1]='1'-(s[i+1]-'0');
        cnt++;
    }
    if (s[n-1]==B[n-1]) ans=min(ans,cnt);
}
int main()
{
	scanf("%s%s",A,B);
	int num1=0,num2=0;
	n=strlen(A);
	for (int i=0;i<n;i++)
    if (A[i]=='0') num1++;
    for (int i=0;i<n;i++)
    if (B[i]=='0') num2++;
    if ((num1&1)!=(num2&1))
    {
        printf("-1\n");
        return 0;
    }
    for (int i=0;i<n;i++) A[i+n]=A[i];
    ans=INF;
    for (int i=0;i<n;i++)
    {
        char c[Maxn];
        memcpy(c,A+i,sizeof(char)*n);
        solve(c,0);
        memcpy(c,A+i,sizeof(char)*n);
        c[0]='1'-(c[0]-'0');
        c[n-1]='1'-(c[n-1]-'0');
        solve(c,1);
    }
    printf("%d\n",ans);
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
You have two binary strings � a and � b of length � n. You would like to make all the elements of both strings equal to 0 0. Unfortunately, you can modify the contents of these strings using only the following operation: You choose two indices � l and � r ( 1 ≤ � ≤ � ≤ � 1≤l≤r≤n); For every � i that respects � ≤ � ≤ � l≤i≤r, change � � a i ​ to the opposite. That is, � � : = 1 − � � a i ​ :=1−a i ​ ; For every � i that respects either 1 ≤ � < � 1≤i<l or � < � ≤ � r<i≤n, change � � b i ​ to the opposite. That is, � � : = 1 − � � b i ​ :=1−b i ​ . Your task is to determine if this is possible, and if it is, to find such an appropriate chain of operations. The number of operations should not exceed � + 5 n+5. It can be proven that if such chain of operations exists, one exists with at most � + 5 n+5 operations. Input Each test consists of multiple test cases. The first line contains a single integer � t ( 1 ≤ � ≤ 1 0 5 1≤t≤10 5 ) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer � n ( 2 ≤ � ≤ 2 ⋅ 1 0 5 2≤n≤2⋅10 5 ) — the length of the strings. The second line of each test case contains a binary string � a, consisting only of characters 0 and 1, of length � n. The third line of each test case contains a binary string � b, consisting only of characters 0 and 1, of length � n. It is guaranteed that sum of � n over all test cases doesn't exceed 2 ⋅ 1 0 5 2⋅10 5 . Output For each testcase, print first "YES" if it's possible to make all the elements of both strings equal to 0 0. Otherwise, print "NO". If the answer is "YES", on the next line print a single integer � k ( 0 ≤ � ≤ � + 5 0≤k≤n+5) — the number of operations. Then � k lines follows, each contains two integers � l and � r ( 1 ≤ � ≤ � ≤ � 1≤l≤r≤n) — the description of the operation. If there are several correct answers, print any of them.
最新发布
06-11

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值