C. Flexible String

import java.io.*;
import java.util.*;

//https://codeforces.com/problemset/problem/1778/C
public class Main{
	static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	//思路:暴力枚举每一堆可以变换的字符种类(总数最大只有10,不会T)
	static HashSet<Integer> set=new HashSet<>();
	static List<Integer> temp=new ArrayList<>();
	static boolean[] vis=new boolean[26];
	static char[] ch1,ch2;
	static int n,k;
	public static void main(String[] args) throws IOException {
		int t=Integer.parseInt(br.readLine());
		while(t-->0){
			String[] s=br.readLine().split(" ");
			n=Integer.parseInt(s[0]);
			k=Integer.parseInt(s[1]);
			temp.clear();
			set.clear();
			Arrays.fill(vis,false);
			ch1=br.readLine().toCharArray();
			ch2=br.readLine().toCharArray();
			for(char c:ch1)set.add(c-'a');
			temp.addAll(set);
			k=Math.min(k,temp.size());
			out.println(dfs(0,0));
		}
		out.close();
	}
	//枚举字符
	static long dfs(int index,int cnt){
		if(cnt==k)return solve();
		if(index==temp.size())return 0;
		long ans=0;
		ans=Math.max(ans,dfs(index+1,cnt));
		vis[temp.get(index)]=true;
		ans=Math.max(ans,dfs(index+1,cnt+1));
		vis[temp.get(index)]=false;
		return ans;
	}
	//统计对数
	static long solve(){
		long sum=0;
		for(int l=0;l<n;){
			int r=l;
			while(r<n&&(ch1[r]==ch2[r]||vis[ch1[r]-'a']))r++;
			sum+=(r-l+1)*(long)(r-l)/2;
			l=r+1;
		}
		return sum;
	}
	static int nextInt() throws IOException {
		in.nextToken();
		return (int) in.nval;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值