You have a string aa and a string bb. Both of the strings have length nn. There are at most 1010 different characters in the string aa. You also have a set QQ. Initially, the set QQ is empty. You can apply the following operation on the string aa any number of times:
- Choose an index ii (1≤i≤n1≤i≤n) and a lowercase English letter cc. Add aiai to the set QQ and then replace aiai with cc.
For example, Let the string aa be "abeccaabecca". We can do the following operations:
- In the first operation, if you choose i=3i=3 and c=xc=x, the character a3=ea3=e will be added to the set QQ. So, the set QQ will be {e}{e}, and the string aa will be "abx––ccaabx_cca".
- In the second operation, if you choose i=6i=6 and c=sc=s, the character a6=aa6=a will be added to the set QQ. So, the set QQ will be {e,a}{e,a}, and the string aa will be "abxccs––abxccs_".
You can apply any number of operations on aa, but in the end, the set QQ should contain at most kk different characters. Under this constraint, you have to maximize the number of integer pairs (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that a[l,r]=b[l,r]a[l,r]=b[l,r]. Here, s[l,r]s[l,r] means the substring of string ss starting at index ll (inclusively) and ending at index rr (inclusively).
Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). The description of the test cases follows.
The first line contains two integers nn and kk (1≤n≤1051≤n≤105, 0≤k≤100≤k≤10) — the length of the two strings and the limit on different characters in the set QQ.
The second line contains the string aa of length nn. There is at most 1010 different characters in the string aa.
The last line contains the string bb of length nn.
Both of the strings aa and bb contain only lowercase English letters. The sum of nn over all test cases doesn't exceed 105105.
Output
For each test case, print a single integer in a line, the maximum number of pairs (l,r)(l,r) satisfying the constraints.
出现的字母不到十个,可以用二进制枚举,为1表示把当前字母替换掉
#include<iostream>
#include<cmath>
#include<cstring>
#include<map>
#define int long long
using namespace std;
const int N = 1e5 + 10;
char s[N];
int cnt1 = 0;
bool st[27];
int mp[27];
char a[N], b[N];
signed main()
{
int t;
scanf("%lld",&t);
while(t --)
{
cnt1 = 0;
memset(st, 0, sizeof st);
memset(mp, 0, sizeof mp);
int ans = 0;
int n, k;
scanf("%lld%lld",&n,&k);
scanf("%s%s",a, b);
int len = strlen(a) - 1;
for(int i = len; i >= 0; i --)
{
if(!st[a[i] - 'a'])
{
mp[a[i] - 'a'] = cnt1;
s[cnt1 ++] = a[i] - 'a';
st[a[i] - 'a'] = true;
}
}
int cnt = 0;
int con = 0;
int sum;
for(int i = 0; i < (1 << cnt1 + 1) - 1; i ++)
{
sum = 0;
cnt = 0;
con = 0;
int now = 0;
for(int j = 0; j < 11; j ++)
{
if((i >> j) & 1 == 1)
cnt ++;
}
if(cnt > k)
continue;
for(int j = 0; a[j]; j ++)
{
if(a[j] == b[j])
{
sum ++;
con ++;
continue;
}
else
{
if((i >> (mp[a[j] - 'a']) & 1)== 1)
{
sum ++;
con ++;
}
else
{
now += con * (con - 1) / 2;
con = 0;
}
}
}
now += con * (con - 1) / 2;
ans = max(ans, now + sum);
}
printf("%lld\n", ans);
}
}