传送门:http://codeforces.com/contest/1084/problem/E
题意:给出两个字符串s,t,选出k个字符串,使得字典序在[s,t]内且前缀和不同的数量最多。
题解:题目是询问构建一颗字典树,最左边不小于s,最右边不大于t,且结点个数最多,那么我们只要左边贴着s走,右边贴着t走即可,也就是如果当前左边不能走a右边不能走b的情况,我们要减掉相应的方案数。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string a,b;
int main(){
int n;
ll k;
scanf("%d%lld",&n,&k);
cin>>a>>b;
ll now=1,ans=0;
if(k==1){
printf("%d\n",n);
return 0;
}
for(int i=0;i<n;i++){
now*=2;
if(a[i]!='a')now--;
if(b[i]!='b')now--;
now=min(now,k);
ans+=now;
}
printf("%lld\n",ans);
return 0;
}