Codeforces Round #570 (Div. 3)E. Subsequences (easy version)

原题出处:http://codeforces.com/contest/1183/problem/E

The only difference between the easy and the hard versions is constraints.

A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa".

You are given a string ss consisting of nn lowercase Latin letters.

In one move you can take any subsequence tt of the given string and add it to the set SS. The set SS can't contain duplicates. This move costs n−|t|n−|t|, where |t||t| is the length of the added subsequence (i.e. the price equals to the number of the deleted characters).

Your task is to find out the minimum possible total cost to obtain a set SS of size kk or report that it is impossible to do so.

Input

The first line of the input contains two integers nn and kk (1≤n,k≤1001≤n,k≤100) — the length of the string and the size of the set, correspondingly.

The second line of the input contains a string ss consisting of nn lowercase Latin letters.

Output

Print one integer — if it is impossible to obtain the set SS of size kk, print -1. Otherwise, print the minimum possible total cost to do it.

Examples

input

Copy

4 5
asdf

output

Copy

4

input

Copy

5 6
aaaaa

output

Copy

15

input

Copy

5 7
aaaaa

output

Copy

-1

input

Copy

10 100
ajihiushda

output

Copy

233

Note

In the first example we can generate SS = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in SS is 00 and the cost of the others is 11. So the total cost of SS is 44.

题意:

给你一个长度为n的字符串,找出其中k个不同子序列(可不连续),使得代价(删除字符数)最小。

思路:

如果通过dfs找遍所有子串将会有2^100种可能,显然行不通。

可以将字符串抽象成图,字符是一个个节点。利用bfs与set结合,队列存储当前字符串,每次删除
一个字符,若set不存在,更新队列与set。

bfs层先能够从删除少的字符串开始,保证了代价最小效率最优。
#include<bits/stdc++.h>
#include<algorithm>
 
using namespace std;
 
int main()
{
	int n,k;
	int ans = 0;
	string s;
	queue<string> que;
	set<string> st;
	cin>>n>>k;
	cin>>s;
	que.push(s);
	st.insert(s);
	while (!que.empty() && st.size()<k)
	{
		string dd = que.front();
		que.pop();
		for (int i=0;i<dd.size();i++)
		{
			string by = dd;
			by.erase(i,1);
			if (!st.count(by) && st.size()+1<=k)
			{
				que.push(by);
				st.insert(by);
				ans += n - by.size();
			}
		}
	}
	if (st.size()<k) 
		ans = -1;
	printf("%d\n",ans);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值