codeforces 1084 E. The Fair Nut and Strings(字典树)

题目:http://codeforces.com/contest/1084/problem/E

给你2个长度为n的字符串,找到m个字典序在两个字符串之间的串拥有的最大前缀

画出01字典树
0代表a 1代表b
在这里插入图片描述
这里可以看出来从第一层开始 前缀数量翻倍,但如果左边为b或者右边为a,那么这以层数目就要减一,还有k个不同的字符串每一层最多的不同的前缀也只有k个,防止爆int设置一个mx

#include<bits/stdc++.h>
#define fi first
#define se second
#define FOR(a) for(int i=0;i<a;i++)
#define sc(a) scanf("%d",&a)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 3e5 + 10;
const ll mod = 1e9+7;

map<string, int>ml;



ll c[N], vis[N][10],  num[N], t, n, m, x, y, k, a[N];
ll ex, ey, cnt, ans, sum;
ll dist[N];
ll dp[N];
deque <int> q;
vector<P> v[N];
char s[500005],ss[500005];

int main()
{
	ios::sync_with_stdio ( false );
	cin.tie ( 0 );

	cin>>n>>m;
	cin>>s>>ss;
	cnt=1;
	ll mx=1e9;
	for(int i=0;i<n;i++)
	{
		cnt*=2;
		if(s[i]=='b') cnt--;
		if(ss[i]=='a') cnt--;
		cnt=min(cnt,mx);
		ans+=min(cnt,m);
	}
	cout<<ans<<endl;




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值