【问题标题】:How to remove K characters from a string such that there is a minimum of each character如何从字符串中删除 K 个字符,使得每个字符最少
【发布时间】:2020-01-05 19:33:31
【问题描述】:

我想从字符串中删除 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;
}

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

【问题讨论】:

  • 您能否详细说明一下任务和预期结果是什么?字段总是排序的?仅当它们彼此相邻且相同时才能删除它们...?
  • @libik 已更新。
  • 还不清楚,所以从abac我可以做abc吗?
  • 是的,给定 K >= 1,inteded 结果将是 abc
  • 你能提供这个问题的原始出处吗?

标签: string algorithm minimum


【解决方案1】:

根据您的描述和测试用例,我不确定您在寻找什么。您的答案是返回字符串中剩余的字符数,更新后的函数返回 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”。删除第 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 2011-11-18
    • 1970-01-01
    • 2017-08-19
    • 2016-01-21
    相关资源
    最近更新 更多