思路
把a看成0,把b看成1这样就形成了01字符串
用类似字典树的样子来建树
求每层s,t字典序大小之间(包括是s,t)的个数设为d
答案即为每层min(d,k)的和
代码
// Problem: The Fair Nut and Strings
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/problem/113276
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp(aa,bb) make_pair(aa,bb)
#define _for(i,b) for(int i=(0);i<(b);i++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,b,a) for(int i=(b);i>=(a);i--)
#define mst(abc,bca) memset(abc,bca,sizeof abc)
#define X first
#define Y second
#define lowbit(a) (a&(-a))
#define debug(a) cout<<#a<<":"<<a<<"\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
const int N=100010;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1.0);
ll ans;
void solve(){
int n;ll k;cin>>n>>k;
string s,t;cin>>s>>t;
ll ss=0,tt=0;
rep(i,0,n-1){
if(s[i]=='b') ss++;
if(t[i]=='b') tt++;
ll d=tt-ss+1;
if(d>=k){
ans+=k*(n-i);
break;
}
else ans+=d;
ss<<=1;tt<<=1;
}
cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
// int t;cin>>t;while(t--)
solve();
return 0;
}

京公网安备 11010502036488号