给定字符串S ,任务是找到给定字符串存在的最长递增子序列的长度。
A sequence of characters placed in increasing order of their ASCII values is called an increasing sequence.
例子:
Input: S = “abcfgffs”
Output: 6
Explanation: Subsequence “abcfgs” is the longest increasing subsequence present in the string. Therefore, the length of the longest increasing subsequence is 6.
Input: S = “aaabac”
Output: 3
方法:想法是使用动态编程。请按照以下步骤解决问题:
- 初始化一个数组,大小为26的dp [] ,以在每个第i个索引处存储最长递增子序列的长度,该子序列具有第(a)+ i个字符作为子序列中的最后一个字符。
- 初始化变量lis ,以存储所需子序列的长度。
- 遍历字符串S的每个字符。
- 对于遇到的每个字符,即S [i] –’a’ ,检查所有字符,例如j ,其ASCII值小于当前字符的ASCII值。
- 初始化一个变量,例如curr ,以存储以当前字符结尾的LIS的长度。
- 用max(curr,dp [j])更新curr 。
- 用max(lis,curr + 1)更新LIS的长度,例如lis 。
- 用d [S [i] –’a’]的最大值更新dp [S [i] –’a ‘]和curr。
- 最后,将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.
输出:
4
时间复杂度: O(N)
辅助空间: O(1)