📌  相关文章
📜  词典上最大的子序列,每个字符至少出现k次

📅  最后修改于: 2021-05-07 18:41:04             🧑  作者: Mango

给定字符串S和整数K。任务是找到字典上S的最大子序列,例如T,以使T中的每个字符必须出现至少K次。

例子:

输入:S =“ banana”,K =2。输出:nn每个字符至少存在两次的可能子序列是: 在以上子序列中,“ nn”在字典上最大。

这个想法是贪婪地解决上述问题。如果要按字典顺序使子序列最大,则必须优先考虑按字典顺序较大的字符。 ‘z’是最大的字符,假设z在S中出现f z次。如果f z > = K,则在字符串T中追加’z’z k次,并继续从S的左侧删除字符,直到所有z都为删除。应用带有“ y”,“ w”,…..,“ a”的策略。最后,您会找到答案。

让我们来看一个例子。假设S =“ zzwzawa”且K =2。从最大字符“ z”开始。这里f z = 3> =K。因此T将变为“ zzz”,我们将删除S左侧的字母,直到所有z都被删除为止。因此,现在S将变为“ awa”。下一个最大的是“ y”,但在k中出现0次,因此我们将其跳过。我们也将跳过“ w”,“ v”等,直到进入出现两次的“ a”。现在,T将成为“ zzzaa”,S将成为一个空字符串。我们的答案是“ zzzaa”。

以下是此方法的实现:

C++
// C++ program to find lexicographically largest
// subsequence where every character appears at
// least k times.
#include 
using namespace std;
  
// Find lexicographically largest subsequence of
// s[0..n-1] such that every character appears 
// at least k times. The result is filled in t[]
void subsequence(char s[], char t[], int n, int k)
{
    int last = 0, cnt = 0, new_last = 0, size = 0;
  
    // Starting from largest charter 'z' to 'a'
    for (char ch = 'z'; ch >= 'a'; ch--) {
        cnt = 0;
  
        // Counting the frequency of the character
        for (int i = last; i < n; i++) {
            if (s[i] == ch)
                cnt++;
        }
  
        // If frequency is greater than k
        if (cnt >= k) {
  
            // From the last point we leave
            for (int i = last; i < n; i++) {
  
                // check if string contain ch
                if (s[i] == ch) {
  
                    // If yes, append to output string
                    t[size++] = ch;
                    new_last = i;
                }
            }
  
            // Update the last point.
            last = new_last;
        }
    }
    t[size] = '\0';
}
  
// Driver code
int main()
{
    char s[] = "banana";
    int n = sizeof(s);
    int k = 2;
    char t[n];
    subsequence(s, t, n - 1, k);
    cout << t << endl;
    return 0;
}


Java
import java.util.Arrays;
  
  
// Java program to find lexicographically largest
// subsequence where every character appears at
// least k times.  
class GFG {
  
// Find lexicographically largest subsequence of
// s[0..n-1] such that every character appears 
// at least k times. The result is filled in t[]
static void subsequence(char s[], char t[], int n, int k)
{
    int last = 0, cnt = 0, new_last = 0, size = 0;
   
    // Starting from largest charter 'z' to 'a'
    for (char ch = 'z'; ch >= 'a'; ch--) {
        cnt = 0;
   
        // Counting the frequency of the character
        for (int i = last; i < n; i++) {
            if (s[i] == ch)
                cnt++;
        }
   
        // If frequency is greater than k
        if (cnt >= k) {
   
            // From the last point we leave
            for (int i = last; i < n; i++) {
   
                // check if string contain ch
                if (s[i] == ch) {
   
                    // If yes, append to output string
                    t[size++] = ch;
                    new_last = i;
                }
            }
   
            // Update the last point.
            last = new_last;
        }
    }
    t[size] = '\0';
}
   
// Driver code
    public static void main(String[] args) {
    char s[] = {'b','a','n','a','n','a'};
    int n = s.length;
    int k = 2;
    char t[] = new char[n];
    subsequence(s, t, n - 1, k);
    for(int i = 0;i


Python3
# Python3 program to find lexicographically largest
# subsequence where every character appears at
# least k times.
  
# Find lexicographically largest subsequence of
# s[0..n-1] such that every character appears
# at least k times. The result is filled in t[]
def subsequence(s, t, n, k):
    last = 0
    cnt = 0
    new_last = 0
    size = 0
  
    string = 'zyxwvutsrqponmlkjihgfedcba'
  
    # Starting from largest charter 'z' to 'a'
    for ch in string:
        cnt = 0
        for i in range(last, n):
            if s[i] == ch:
                cnt += 1
  
        # If frequency is greater than k
        if cnt >= k:
  
            # From the last point we leave
            for i in range(last, n):
  
                # check if string contain ch
                if s[i] == ch:
  
                    # If yes, append to output string
                    t[size] = ch
                    new_last = i
                    size += 1
  
            # Update the last point.
            last = new_last
  
# Driver Code
if __name__ == "__main__":
    s = ['b', 'a', 'n', 'a', 'n', 'a']
    n = len(s)
    k = 2
    t = [''] * n
    subsequence(s, t, n - 1, k)
    t = ''.join(t)
    print(t)
  
# This code is contributed by
# sanjeev2552


C#
// C# program to find lexicographically 
// largest subsequence where every 
// character appears at least k times. 
using System;
  
class GFG
{
  
// Find lexicographically largest subsequence 
// of s[0..n-1] such that every character 
// appears at least k times. The result is 
// filled in t[] 
static void subsequence(char []s, char []t,
                        int n, int k) 
{ 
    int last = 0, cnt = 0, 
        new_last = 0, size = 0; 
  
    // Starting from largest character
    // 'z' to 'a' 
    for (char ch = 'z'; ch >= 'a'; ch--)
    { 
        cnt = 0; 
  
        // Counting the frequency of
        // the character 
        for (int i = last; i < n; i++)
        { 
            if (s[i] == ch) 
                cnt++; 
        } 
  
        // If frequency is greater than k 
        if (cnt >= k) 
        { 
  
            // From the last point we leave 
            for (int i = last; i < n; i++)
            { 
  
                // check if string contain ch 
                if (s[i] == ch) 
                { 
  
                    // If yes, append to output string 
                    t[size++] = ch; 
                    new_last = i; 
                } 
            } 
  
            // Update the last point. 
            last = new_last; 
        } 
    } 
    t[size] = '\0'; 
} 
  
// Driver code 
public static void Main() 
{ 
    char []s = {'b','a','n','a','n','a'}; 
    int n = s.Length; 
    int k = 2; 
    char []t = new char[n]; 
    subsequence(s, t, n - 1, k); 
    for(int i = 0; i < t.Length; i++) 
        if(t[i] != 0) 
            Console.Write(t[i]); 
} 
} 
  
// This code contributed by Rajput-Ji


输出:

nn