跳到正文

题解

题解:CF1778C Flexible String

_jimyao约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年7月21日
更新
2026年7月21日
篇幅
约 2 分钟
题目
CF1778C

版本

  1. v1当前

枚举改了哪些种类的字母,统计答案即可

这里提供一种简易写法
本人不熟悉Markdown,见谅

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1 << 30;
const ll INF = 1ll << 60;

const int N = 1e5 + 3;

char a[N], s[N];
int ha[26], g[N];
int n, k;

void solve () {
    scanf ("%d%d%s%s", &n, &k, a + 1, s + 1);
    int cnt = 0;
    memset(ha, 255, sizeof(ha));
    for (int i = 1; i <= n; i++) if (ha[a[i] - 'a'] == -1)
        ha[a[i] - 'a'] = cnt++;
    ll ans = 0;
    for (int i = 0; i < 1 << cnt; i++)
        if (__builtin_popcount(i) <= k) {
            ll res = 0;
            for (int j = 1; j <= n; j++)
                if ((i & (1 << ha[a[j] - 'a'])) || a[j] == s[j]) {
                    g[j] = g[j - 1] + 1;
                    res += 1ll * g[j];
                }
                else
                    g[j] = 0;
            ans = max(ans, res);
        }
    printf("%lld\n", ans);
}

int main () {
    int tc;
    scanf ("%d", &tc);
    for (; tc--; )
        solve();
}
原文