【AtCoder】B - Electric Board(思维)

 

 

 

解题思路:求出每个字符串0前面的1的个数,如果不相等需要进行一次交换,最后输出交换次数就可以了

下面附上ac代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define dbg(a)  cout<<#a<<" : "<<a<<endl;
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);
#define PAUSE   system("pause")
#define sd(a)       scanf("%d",&a)
#define sll(a)      scanf("%lld",&a)
#define sdd(a,b)    scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a)       scanf("%lf",&a)
#define sff(a,b)    scanf("%lf%lf",&a,&b)
#define sfff(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)
using namespace std;
typedef long long ll;
const int maxn=500005;
int n,ans,cntt,cnts;
string s;
string t;
int a[maxn];
int b[maxn];
int main()
{
    cin>>n;
    ans=0;
    cnts=0;
    cntt=0;
    int idx1=1;
    int idx2=1;
    cin>>s>>t;
    if(s==t)
    {
        cout<<"0"<<endl;
    }
    else
    {
        for(int i=0; i<n; i++)
        {
            if(s[i]=='0') a[idx1++]=cnts;
            else    cnts++;
            if(t[i]=='0') b[idx2++]=cntt;
            else    cntt++;
        }
        for(int i=1;i<=idx1;i++)
        {
            cout<<a[i]<<" ";
        }
        cout<<endl;
        for(int i=1;i<=idx1;i++)
        {
            cout<<b[i]<<" ";
        }
        cout<<endl;
        if(cntt!=cnts)
        {
            cout<<-1<<endl;
        }
        else
        {
            for(int i=1; i<=idx1; i++)
            {
                if(a[i]!=b[i])  ans++;
            }
            cout<<ans<<endl;
        }
    }
    //PAUSE;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值