解题思路:求出每个字符串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;
}