📜  字符串中最长递增子序列的长度

📅  最后修改于: 2021-09-17 07:24:54             🧑  作者: Mango

给定一个字符串S ,任务是找到给定字符串存在的最长递增子序列的长度。

例子:

方法:想法是使用动态规划。请按照以下步骤解决问题:

  • 初始化一个数组,比如大小为26 的dp[] ,以在每个i索引处存储最长递增子序列的长度,其中第(‘a’ + i)字符作为子序列中的最后一个字符。
  • 初始化变量,比如lis ,以存储所需子序列的长度。
  • 迭代字符串S 的每个字符。
    • 对于遇到的每个字符,即S[i] – ‘a’ ,检查所有字符,例如j ,其 ASCII 值小于当前字符。
    • 初始化一个变量,比如curr ,以存储以当前字符结尾的 LIS 的长度。
    • max(curr, dp[j])更新curr
    • 使用max(lis, curr + 1)更新 LIS 的长度,比如lis
    • 使用d[S[i] – ‘a’]curr 的最大值更新dp[S[i] – ‘a’]
  • 最后,将lis的值打印为 LIS 所需的长度。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find length of longest
// increasing subsequence in a string
int lisOtimised(string s)
{
    // Stores at every i-th index, the
    // length of the longest increasing
    // subsequence ending with character i
    int dp[30] = { 0 };
 
    // Size of string
    int N = s.size();
 
    // Stores the length of LIS
    int lis = INT_MIN;
 
    // Iterate over each
    // character of the string
    for (int i = 0; i < N; i++) {
 
        // Store position of the
        // current character
        int val = s[i] - 'a';
 
        // Stores the length of LIS
        // ending with current character
        int curr = 0;
 
        // Check for all characters
        // less then current character
        for (int j = 0; j < val; j++) {
            curr = max(curr, dp[j]);
        }
 
        // Include current character
        curr++;
 
        // Update length of longest
        // increasing subsequence
        lis = max(lis, curr);
 
        // Updating LIS for current character
        dp[val] = max(dp[val], curr);
    }
 
    // Return the length of LIS
    return lis;
}
 
// Driver Code
int main()
{
    string s = "fdryutiaghfse";
    cout << lisOtimised(s);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
static int mn = -2147483648;
   
// Function to find length of longest
// increasing subsequence in a string
static int lisOtimised(String s)
{
     
    // Stores at every i-th index, the
    // length of the longest increasing
    // subsequence ending with character i
    int []dp = new int[30];
    Arrays.fill(dp, 0);
 
    // Size of string
    int N = s.length();
 
    // Stores the length of LIS
    int lis = mn;
 
    // Iterate over each
    // character of the string
    for(int i = 0; i < N; i++)
    {
         
        // Store position of the
        // current character
        int val =  (int)s.charAt(i) - 97;
 
        // Stores the length of LIS
        // ending with current character
        int curr = 0;
 
        // Check for all characters
        // less then current character
        for(int j = 0; j < val; j++)
        {
            curr = Math.max(curr, dp[j]);
        }
 
        // Include current character
        curr++;
 
        // Update length of longest
        // increasing subsequence
        lis = Math.max(lis, curr);
 
        // Updating LIS for current character
        dp[val] = Math.max(dp[val], curr);
    }
 
    // Return the length of LIS
    return lis;
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "fdryutiaghfse";
     
    System.out.print(lisOtimised(s));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the above approach
 
# Function to find length of longest
# increasing subsequence in a string
def lisOtimised(s):
   
    # Stores at every i-th index, the
    # length of the longest increasing
    # subsequence ending with character i
    dp = [0]*30
 
    # Size of string
    N = len(s)
 
    # Stores the length of LIS
    lis = -10**9
 
    # Iterate over each
    # character of the string
    for i in range(N):
 
        # Store position of the
        # current character
        val = ord(s[i]) - ord('a')
 
        # Stores the length of LIS
        # ending with current character
        curr = 0
 
        # Check for all characters
        # less then current character
        for j in range(val):
            curr = max(curr, dp[j])
 
        # Include current character
        curr += 1
 
        # Update length of longest
        # increasing subsequence
        lis = max(lis, curr)
 
        # Updating LIS for current character
        dp[val] = max(dp[val], curr)
 
    # Return the length of LIS
    return lis
 
# Driver Code
if __name__ == '__main__':
    s = "fdryutiaghfse"
    print (lisOtimised(s))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 static int mn = -2147483648;
   
 // Function to find length of longest
// increasing subsequence in a string
static int lisOtimised(string s)
{
   
    // Stores at every i-th index, the
    // length of the longest increasing
    // subsequence ending with character i
    int []dp = new int[30];
    Array.Clear(dp, 0, 30);
 
    // Size of string
    int N = s.Length;
 
    // Stores the length of LIS
    int lis = mn;
 
    // Iterate over each
    // character of the string
    for (int i = 0; i < N; i++) {
 
        // Store position of the
        // current character
        int val =  (int)s[i] - 97;
 
        // Stores the length of LIS
        // ending with current character
        int curr = 0;
 
        // Check for all characters
        // less then current character
        for (int j = 0; j < val; j++) {
            curr = Math.Max(curr, dp[j]);
        }
 
        // Include current character
        curr++;
 
        // Update length of longest
        // increasing subsequence
        lis = Math.Max(lis, curr);
 
        // Updating LIS for current character
        dp[val] = Math.Max(dp[val], curr);
    }
 
    // Return the length of LIS
    return lis;
}
 
// Driver Code
public static void Main()
{
    string s = "fdryutiaghfse";
    Console.Write(lisOtimised(s));
}
}
 
// This code is contributed by SURENDRA_GAANGWAR.


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(1)

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