博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces - 1183E Subsequences (easy version) (字符串bfs)
阅读量:4352 次
发布时间:2019-06-07

本文共 2744 字,大约阅读时间需要 9 分钟。

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 (1n,k1001≤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 5asdf
Output
4
Input
5 6aaaaa
Output
15
Input
5 7aaaaa
Output
-1
Input
10 100ajihiushda
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 
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 = 0; queue
q; set
st; q.push(s); st.insert(s); while (!q.empty() && int(st.size()) < k) { string v = q.front(); q.pop(); for (int i = 0; i < int(v.size()); ++i) { string nv = v; nv.erase(i, 1); if (!st.count(nv) && int(st.size()) + 1 <= k) { q.push(nv); st.insert(nv); ans += n - nv.size(); } } } if (int(st.size()) < k) cout << -1 << endl; else cout << ans << endl; return 0;}

 

转载于:https://www.cnblogs.com/yzm10/p/11102020.html

你可能感兴趣的文章
有道语料库爬虫
查看>>
VS2019 实用设置
查看>>
for循环语句之求和,阶乘,求偶,求n次篮球蹦起高度
查看>>
CFileDialog
查看>>
[转载]EXTJS学习
查看>>
SQL Server2012完全备份、差异备份、事务日志备份和还原操作
查看>>
Flash动画播放
查看>>
springmvc+mybatis+dubbo+zookeeper 分布式架构
查看>>
HDUOJ-----Computer Transformation
查看>>
HDUOJ-----2838Cow Sorting(组合树状数组)
查看>>
自定义控件之---抽屉式弹窗控件.
查看>>
一款纯css3实现的机器人看书动画效果
查看>>
加班与效率
查看>>
轻量级Modal模态框插件cta.js
查看>>
MyEclipse下SpringBoot+JSP整合过程及踩坑
查看>>
重定向和管道
查看>>
实验五
查看>>
STL学习笔记(第二章 C++及其标准程序库简介)
查看>>
Operator_countByValue
查看>>
Java 日期往后推迟n天
查看>>