The Fair Nut and Strings

The Fair Nut and Strings

https://ac.nowcoder.com/acm/problem/113276

思路

把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;
}

牛客每日一题 文章被收录于专栏

全部评论

相关推荐

06-18 15:03
门头沟学院 Java
至少实习看起来比去年好?问了下群里的同学和身边的同学,人均有offer。有的还有好几个大厂offer
菜鸟1973:上一年暑期也是人均大厂实习offer,结果秋招跟不招人一样,大部分都转正了
点赞 评论 收藏
分享
白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
点赞 评论 收藏
分享
我面试,她问我有女朋友没
不太迷人的反派_:不过对象,还会结合你老家,意向城市等等,看你是否稳定。哥们,别多想
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务