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
4 5
asdf
Output
4
Input
5 6
aaaaa
Output
15
Input
5 7
aaaaa
Output
-1
Input
10 100
ajihiushda
Output
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>

using namespace std;

int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif int n, k;
cin >> n >> k;
string s;
cin >> s; int ans = ;
queue<string> q;
set<string> st;
q.push(s);
st.insert(s);
while (!q.empty() && int(st.size()) < k) {
string v = q.front();
q.pop();
for (int i = ; i < int(v.size()); ++i) {
string nv = v;
nv.erase(i, );
if (!st.count(nv) && int(st.size()) + <= k) {
q.push(nv);
st.insert(nv);
ans += n - nv.size();
}
}
} if (int(st.size()) < k) cout << - << endl;
else cout << ans << endl; return ;
}

CodeForces - 1183E Subsequences (easy version) (字符串bfs)的更多相关文章

  1. UVA12569-Planning mobile robot on Tree (EASY Version)(BFS+状态压缩)

    Problem UVA12569-Planning mobile robot on Tree (EASY Version) Accept:138  Submit:686 Time Limit: 300 ...

  2. Codeforces Round #570 (Div. 3) E. Subsequences (easy version) (搜索,STL)

    题意:有一长度为\(n\)的字符串,要求得到\(k\)不同的它的子序列(可以是空串),每个子序列有\(|n|-|t|\)的贡献,求合法情况下的最小贡献. 题解:直接撸个爆搜找出所有子序列然后放到set ...

  3. Codeforces Round #570 (Div. 3) B. Equalize Prices、C. Computer Game、D. Candy Box (easy version)、E. Subsequences (easy version)

    B题题意: 给你n个物品的价格,你需要找出来一个值b,使得每一个物品与这个b的差值的绝对值小于k.找到最大的b输出,如果找不到,那就输出-1 题解: 很简单嘛,找到上下限直接二分.下限就是所有物品中最 ...

  4. Codeforces Round #602 Div2 D1. Optimal Subsequences (Easy Version)

    题意:给你一个数组a,询问m次,每次返回长度为k的和最大的子序列(要求字典序最小)的pos位置上的数字. 题解:和最大的子序列很简单,排个序就行,但是题目要求字典序最小,那我们在刚开始的时候先记录每个 ...

  5. UVA-12569 Planning mobile robot on Tree (EASY Version) (BFS+状态压缩)

    题目大意:一张无向连通图,有一个机器人,若干个石头,每次移动只能移向相连的节点,并且一个节点上只能有一样且一个东西(机器人或石头),找出一种使机器人从指定位置到另一个指定位置的最小步数方案,输出移动步 ...

  6. codeforces B. Ping-Pong (Easy Version) 解题报告

    题目链接:http://codeforces.com/problemset/problem/320/B 题目意思:有两种操作:"1 x y"  (x < y) 和 " ...

  7. CodeForces - 320B Ping-Pong (Easy Version)

    题目最开始 完全不懂 配合案例也看不懂-_- 总之就是用传递性 问能否从a区间到b区间 dfs(x,y) 走遍与第x区间所有的 联通区间 最后检验 第y区是否被访问过 是一道搜索好题 搜索还需加强 # ...

  8. CodeForces - 1183H Subsequences (hard version) (DP)

    题目:https://vjudge.net/contest/325352#problem/C 题意:输入n,m,给你一个长度为n的串,然后你有一个集合,集合里面都是你的子序列,集合里面不能重复,集合中 ...

  9. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

随机推荐

  1. JS实现倒计时效果,并退出系统

    背景:由于单点登录后,一直在本系统操作,可是门户体统的会话失效时间有30分钟,所以30分钟后,需要重新登录系统才可以进行操作. 方法:想过在本系统中的每个操作都先跟门户系统进行交互,渠道refresh ...

  2. STM8 定时器

    中断映射表 对应stm8_interrupt.c #pragma vector=1 __interrupt void TRAP_IRQHandler(void) { } #pragma vector= ...

  3. 虹软人脸识别 - faceId及IR活体检测的更新介绍

    虹软人脸识别 - faceId及IR活体检测的介绍 前几天虹软推出了 Android ArcFace 2.2版本的SDK,相比于2.1版本,2.2版本中的变化如下: VIDEO模式新增faceId(类 ...

  4. [LeetCode] 234. 回文链表 ☆(翻转链表)

    描述 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2输出: false示例 2: 输入: 1->2->2->1输出: true 进阶:你能否用 O(n) 时间复杂 ...

  5. MVC-Cache-2.应用程序缓存(Cache:1.输出缓存[2].应用程序缓存)

    2.应用数据缓存-Cache 1.引入CacheHelper.cs CacheHelper.cs文件源码在下面; 2.介绍用法: //键 string ips = "键"; //值 ...

  6. HTTP 协议部分常识简介

    1.状态码 具体的状态码可以百度查找,但是对于状态码的大致分类有一个清楚的了解 1XX   ----信息状态码------接受的请求正在处理 2XX ------成功状态码 ------请求正常处理完 ...

  7. ERROR: Cannot uninstall 'wrapt'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

    pip install imagededup 时,报错:Cannot uninstall 'wrapt'. It is a distutils installed project and thus w ...

  8. python练习题(二)

    题目: 已知以下几期双色球号码(最后一个数字为蓝球), 2019080 03 06 08 20 24 32 07 2019079 01 03 06 09 19 31 16 2019078 01 17 ...

  9. FreeMarker生成word

    FreeMarker生成word数据填充是通过,Map填充. Map dataMap = new HashMap<String, Object>(); List<User> l ...

  10. 数位DP【模板】

    经典题型 数位 DP 问题往往都是这样的题型,给定一个闭区间 $[l, r]$,让你求这个区间中满足 某种条件 的数的总数. 强烈推荐 OI Wiki——数位DP 例题 BZOJ 1026 题目:wi ...