📜  形式为 R^NK^N 的最大长度子序列可能

📅  最后修改于: 2021-10-27 16:46:09             🧑  作者: Mango

给定一个只包含两个字符的字符串,即 R 和 K(如 RRKRRKKKKK)。任务是为可能形式为 R-N 次然后 K-N 次(即形式为 R^NK^N)的子序列找到 N 的最大值。
注意: k 的字符串应该在 R 的字符串之后开始,即第一个被考虑用于 ‘K’字符串的 k 必须出现在给定字符串’R’字符串的最后一个 R 之后。此外,结果子序列的长度将为 2*N。
例子

方法:

  1. 在 K 之前计算 R 的数量。
  2. 计算一个 K 之后的 K 个数,包括那个 K。
  3. 将它们存储在一个表中,在 table[x][0] 中有多个 R,在 table[x][1] 中有多个 K。
  4. 两者中的最小值给出每个 K 的 n 值,我们将返回最大值 n。

下面是上述方法的实现:

C++
// C++ program to find the maximum
// length of a substring of form R^nK^n
#include
using namespace std;
 
    // function to calculate the maximum
    // length of substring of the form R^nK^n
    int find(string s)
    {
        int max = 0, i, j = 0, countk = 0, countr = 0;
        int table[s.length()][2];
 
        // Count no. Of R's before a K
        for (i = 0; i < s.length(); i++) {
            if (s[i] == 'R')
                countr++;
            else
                table[j++][0] = countr;
        }
        j--;
 
        // Count no. Of K's after a K
        for (i = s.length() - 1; i >= 0; i--) {
            if (s[i] == 'K') {
                countk++;
                table[j--][1] = countk;
            }
 
            // Update maximum length
            if (min(table[j + 1][0], table[j + 1][1]) > max)
                max = min(table[j + 1][0], table[j + 1][1]);
        }
 
        return max;
    }
 
    // Driver code
    int main()
    {
        string s = "RKRRRKKRRKKKKRR";
        int n = find(s);
        cout<<(n);
    }
// This code is contributed by
// Surendra_Gangwar


Java
// Java program to find the maximum
// length of a substring of form R^nK^n
public class gfg {
 
    // function to calculate the maximum
    // length of substring of the form R^nK^n
    int find(String s)
    {
        int max = 0, i, j = 0, countk = 0, countr = 0;
        int table[][] = new int[s.length()][2];
 
        // Count no. Of R's before a K
        for (i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'R')
                countr++;
            else
                table[j++][0] = countr;
        }
        j--;
 
        // Count no. Of K's after a K
        for (i = s.length() - 1; i >= 0; i--) {
            if (s.charAt(i) == 'K') {
                countk++;
                table[j--][1] = countk;
            }
 
            // Update maximum length
            if (Math.min(table[j + 1][0], table[j + 1][1]) > max)
                max = Math.min(table[j + 1][0], table[j + 1][1]);
        }
 
        return max;
    }
 
    // Driver code
    public static void main(String srgs[])
    {
        String s = "RKRRRKKRRKKKKRR";
        gfg ob = new gfg();
        int n = ob.find(s);
        System.out.println(n);
    }
}


Python3
# Python3 program to find the maximum
# length of a substring of form R^nK^n
 
# Function to calculate the maximum
# length of substring of the form R^nK^n
def find(s):
      
    Max = j = countk = countr = 0
    table = [[0, 0] for i in range(len(s))]
 
    # Count no. Of R's before a K
    for i in range(0, len(s)): 
        if s[i] == 'R':
            countr += 1
        else:
            table[j][0] = countr
            j += 1
          
    j -= 1
 
    # Count no. Of K's after a K
    for i in range(len(s) - 1, -1, -1): 
        if s[i] == 'K': 
            countk += 1
            table[j][1] = countk
            j -= 1
          
        # Update maximum length
        if min(table[j + 1][0], table[j + 1][1]) > Max:
            Max = min(table[j + 1][0], table[j + 1][1])
          
    return Max
      
# Driver code
if __name__ == "__main__":
      
    s = "RKRRRKKRRKKKKRR"
    print(find(s))
     
# This code is contributed by Rituraj Jain


C#
// C# program to find the maximum
// length of a substring of
// form R^nK^n
using System;
 
class GFG
{
 
// function to calculate the
// maximum length of substring
// of the form R^nK^n
static int find(String s)
{
    int max = 0, i, j = 0,
        countk = 0, countr = 0;
    int [,]table= new int[s.Length, 2];
 
    // Count no. Of R's before a K
    for (i = 0; i < s.Length; i++)
    {
        if (s[i] == 'R')
            countr++;
        else
            table[(j++),0] = countr;
    }
    j--;
 
    // Count no. Of K's after a K
    for (i = s.Length - 1; i >= 0; i--)
    {
        if (s[i] == 'K')
        {
            countk++;
            table[j--, 1] = countk;
        }
 
        // Update maximum length
        if (Math.Min(table[j + 1, 0],
                     table[j + 1, 1]) > max)
            max = Math.Min(table[j + 1, 0],
                           table[j + 1, 1]);
    }
 
    return max;
}
 
// Driver code
static public void Main(String []srgs)
{
    String s = "RKRRRKKRRKKKKRR";
    int n = find(s);
    Console.WriteLine(n);
}
}
 
// This code is contributed
// by Arnab Kundu


Javascript


输出:
4

时间复杂度: O(n),其中 ln 是子串的长度。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程