[CF1083B]The Fair Nut and Strings

Posted memory-of-winter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[CF1083B]The Fair Nut and Strings相关的知识,希望对你有一定的参考价值。

题目大意:在给定的长度为$n(nleqslant5 imes10^5)$的字符串$A$和字符串$B$中找到最多$k$个字符串,使得这$k$个字符串不同的前缀字符串的数量最多(只包含字符$a$和$b$)。

题解:考虑给这$k$个字符串建一个$trie$树,答案就是它所含的节点数,考虑贪心,在每一层尽可能多的分叉。注意一层最多只能有$k$个点

卡点:

 

C++ Code:

#include <cstdio>
#define maxn 500010
int n, k;
long long ans;
char A[maxn], B[maxn];
int main() {
	scanf("%d%d", &n, &k);
	scanf("%s%s", A, B);
	long long now = 1;
	for (int i = 0; i < n; ++i) {
		now <<= 1;
		now -= (A[i] == ‘b‘) + (B[i] == ‘a‘);
		if (now >= k) {
			printf("%lld
", ans + static_cast<long long> (n - i) * k);
			return 0;
		}
		ans += now;
	}
	printf("%lld
", ans);
	return 0;
}

  

以上是关于[CF1083B]The Fair Nut and Strings的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 1084A The Fair Nut and Elevator 题解

D. The Fair Nut and the Best Path

D. The Fair Nut and the Best Path 树形dp (终于会了)

CodeForces 1084D The Fair Nut and the Best Path

Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path

Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings