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;




}

猜你喜欢

转载自blog.csdn.net/weixin_42640692/article/details/86516932