如何从字符串中删除 K 个字符,使得每个字符最少

How to remove K characters from a string such that there is a minimum of each character

我想从字符串中删除 K 个字符,以使每个字符的出现次数最少。

例如:

字符串:abcdefghijkllllll

K: 5

答案:12 (abcdeghijkl)

字符串:ababac

K: 4

答案:3 (aac)

字符串:aaaab

K: 4

答案:1(b)

我要删除 5 个字符。这些字符将是 5 l's

到目前为止我所做的是使用地图计算每个字符的出现次数

但我不知道下一步该怎么做。

#include <bits/stdc++.h>
using namespace std;
string s;
int l, k;
map<char, int> m;
int main() {
    getline(cin, s);
    scanf("%d %d", &l, &k);
    for(int i=0; i<s.length(); i++) {
        m[s[i]]++;
    }
    for(auto &x : m) {
        cout << x.second << "\n";
    }
    return 0;
}

预期结果是删除任何给定字符串(可以排序或未排序)的字符后的字符串的最小长度。

您可以删除字符串中的任何字符

更新:

#include <bits/stdc++.h>
using namespace std;
string s;
int l, k;
map<char, int> m;
int main() {
    getline(cin, s);
    cin >> l >> k;
    for(int i = 0; i < s.length(); i++) {
        m[s[i]]++;
    }
    for(auto it = m.end(); it != m.begin(); it--) {
        // cout << it->second << "\n";
    }

    vector<pair<int, int>> pairs;
    for (auto itr = m.begin(); itr != m.end(); itr++) {
        pairs.push_back(*itr);
    }
    sort(pairs.begin(), pairs.end(), [=](pair<int, int>& a, pair<int, int>& b) { return a.second < b.second; } );

    for(auto it = m.end(); it != m.begin(); it--) {
        if(it->second - k >= 1) {
            it->second-=k;
            k -= it->second;
        }
    }
    int sum = 0;
    for(auto it = m.end(); it != m.begin(); it--) {
        sum += it->second;
        // cout << it->second << "\n";
    }
    cout << sum << "\n";
    return 0;
}

当前的问题是它没有读取所有字符并将它们正确映射到地图上。

根据您的描述和测试用例,我不确定您在寻找什么。您的答案是 return 字符串中剩余的字符数,而您更新的函数 return 是 sum 变量。如果是这样,为什么不只是 return 字符串的长度减去 k?

你的第二个测试用例是:

String: ababac
K: 4
Answer: 3 (aac)

从 "ababac" 中删除 4 个字符(长度为 6)会使它的长度为 2,而不是 3。这是如何工作的?

字符可以按任意顺序删除吗?对于第三个测试用例,您有:

String: aaaab
K: 4
Answer: 1(b)

给定描述:I want to remove K characters from a string such that the occurrence of each character is at a minimum. 删除 3 个字符得到结果 "ab"。删除第 4 个可能会导致 "a" 或 "b"。在这种情况下你会怎么做?

这道题有很多歧义,测试用例有点乱。例如,给定 "aabbccdddd" k=3,可接受的答案是什么? "abcdddd" 还是 "aabbccd"? "abcdddd" 会增加最少的字符数,而 "aabbccd" 会减少最常出现的字符数。

我已经使用最大优先级队列/最大堆(在 Java 中)和上面的后面的例子组合了一个答案。这假设您的所有输入都很好。

    import java.util.*;
    public class SO {

        //Helper class to put in the priority queue.
        class CharInt {
            char c;
            int count;
            public CharInt(char c) {
                this.c = c;
                this.count = 1;
            }

            void increment() {
                this.count++;
            }

            void decrement() {
                this.count--;
            }
        }

        public int minChar(String s, int k) {   
            Map<Character, CharInt> map = new HashMap<Character, CharInt>();

            for (Character c : s.toCharArray()) {
                if (map.get(c) == null) {
                    map.put(c, new CharInt(c));
                }else {
                    map.get(c).increment();
                }
            }

            //Makes a Max-Heap  from a PriorityQueue object. The comparator makes sure the top of the PriorityQueue is the character with the highest count.
            PriorityQueue<CharInt> maxHeap = new PriorityQueue<CharInt>(new Comparator<CharInt>() {
                @Override
                public int compare(CharInt o1, CharInt o2) {
                    return - Integer.compare(o1.count, o2.count);
                }
            });

            //Add all values to the heap.
            for (CharInt c : map.values()) {
                maxHeap.add(c);
            }

            //Take the top value off, decrement its count, add it back to the heap. Do this k times.
            while (k-- > 0) {
                CharInt c = maxHeap.poll();
                c.decrement();
                maxHeap.add(c);
            }

            StringBuilder builder = new StringBuilder();     // Used to make output string. Can be left out.
            int sum = 0;

            //Remove every element from the heap and get its count value.
            while(!maxHeap.isEmpty()) {
                CharInt c = maxHeap.poll();
                for (int i = 0; i < c.count; i++) {
                    sum += c.count;
                    builder.append(c.c);                     // Used to make output string. Can be left out.
                }
            }
            char[] chars = builder.toString().toCharArray(); // Used to make output string. Can be left out.
            Arrays.sort(chars);                              // Used to make output string. Can be left out.
            System.out.println(chars);                       // Used to make output string. Can be left out.
            return sum;
        }
        public static void main(String...bannaa) {
            int s = new SO().minChar("abcdefghijkllllll", 5);
            int s2 = new SO().minChar("ababac", 4);
            int s3 = new SO().minChar("aaaab", 4);
            int s4 = new SO().minChar("abbbccc", 4);

            System.out.println(s + " " + s2 + " " + s3 + " " + s4);
        }
    }

输出:

abcdefghijkl
ac
a
abc
12 2 1 3