CodeForces - 1183E Subsequences (easy version) (string BFS)

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 5
asdf
Output
4
Input
6 5 
Aaaaa
Output
15
Input
7 5 
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.

 

 

Meaning of the questions:

You give a string of length n, wherein k identify different sub-sequences (not continuous), so that the cost (number of characters to delete) minimum.

Ideas:

If you searched through all dfs substring will have 2 ^ 100 possible, obviously it does not work.

FIG string may be abstracted, the character is a node. Bfs use in combination with the set, queue stores the current string, a time to delete a character, if the set does not exist, the update queue and set.

bfs first layer can be deleted from the beginning of the string less, to ensure that the least costly optimal efficiency.

 

Official explanations

#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 = 0;
    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 = 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;
}

 

Guess you like

Origin www.cnblogs.com/yzm10/p/11102020.html