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

时间:2019-09-02 20:32:31

标签: string algorithm minimum

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

例如:

字符串:abcdefghijkllllll

K:5

答案:12(abcdeghijkl)

字符串:ababac

K:4

答案:3(aac)

字符串:aaaab

K:4

答案:1(b)

我想删除5个字符。这些字符将是5 l

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

但是我对下一步的工作感到困惑。

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

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

1 个答案:

答案 0 :(得分:0)

从您的描述和测试用例中我不确定您要寻找什么。您的答案是返回字符串中剩余的字符数,而更新后的函数将返回sum变量。如果是这种情况,为什么不返回字符串的长度减去k?

您的第二个测试用例是:

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

从“ ababac”(长度6)中删除4个字符将使其长度为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”。删除第四位可能会导致“ a”或“ b”。在这种情况下,您该怎么办?

这个问题有很多歧义,测试用例有点混乱。例如,给定“ aabbccdddd” k = 3,那么可接受的答案是什么? “ abcdddd”还是“ aabbccd”? “ abcdddd”将增加最少的字符数,而“ aabbccd”将减少最频繁出现的字符数。

我使用最大优先级队列/ max-heap(在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