在给定条件下从给定数组中选择字符串后的最大字符串长度
给定一个大小为N的字符串S[]的数组,任务是找到通过添加一些字符串并遵循给定条件形成的结果字符串的最大大小,如果选择一个大小为K的字符串添加到结果字符串中,那么不能选择下一个K/2 个字符串作为结果数组的一部分。
例子:
Input: S[] = {“well”, “do”, “hi”, “by”}
Output: 6
Explanation: Choose “well” and skip “do” and “hi”(sizeof(“well”)/2) and then choose “by”. So, size will be 6.
Input: s[] = {“geeks”, “for”, “geeks”, “is”, “best”}
Output: 9
方法:这个问题可以使用记忆化来解决。请按照以下步骤操作:
- 对于每个字符串S[i] ,有两个选项,即是否选择当前字符串。
- 因此,如果选择了字符串,它的长度,比如K将有助于结果数组的长度,现在,只能选择K/2之后的字符串。
- 现在,如果字符串被排除在外,只需进一步移动。
- 根据以上观察打印答案
下面是上述方法的实现
C++
// C++ implementation of the above approach
#include
using namespace std;
// Recursive function to find the
// maximum length of the resultant string
int maxsum(string S[], int N, int i,
vector& dp)
{
// If index gets out of bound return 0
if (i >= N)
return 0;
// If value not already computed
// then compute it
if (dp[i] == -1) {
// To include the current string
int op1
= S[i].size()
+ maxsum(S, N,
(i + S[i].size() / 2)
+ 1,
dp);
// To exclude the current string
int op2 = maxsum(S, N, i + 1, dp);
// Maximum of both the options
dp[i] = max(op1, op2);
}
return dp[i];
}
// Driver Code
int main()
{
string S[] = { "geeks", "for", "geeks",
"is", "best" };
int N = sizeof(S) / sizeof(S[0]);
vector dp(N, -1);
cout << maxsum(S, N, 0, dp);
return 0;
}
Java
// Java implementation of the above approach
import java.util.Arrays;
class GFG {
// Recursive function to find the
// maximum length of the resultant string
static int maxsum(String S[], int N, int i, int[] dp)
{
// If index gets out of bound return 0
if (i >= N)
return 0;
// If value not already computed
// then compute it
if (dp[i] == -1) {
// To include the current string
int op1 = S[i].length()
+ maxsum(S, N,
(i + S[i].length() / 2)
+ 1,
dp);
// To exclude the current string
int op2 = maxsum(S, N, i + 1, dp);
// Maximum of both the options
dp[i] = Math.max(op1, op2);
}
return dp[i];
}
// Driver Code
public static void main(String args[]) {
String S[] = { "geeks", "for", "geeks", "is", "best" };
int N = S.length;
int[] dp = new int[N];
Arrays.fill(dp, -1);
System.out.println(maxsum(S, N, 0, dp));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python implementation of the above approach
# Recursive function to find the
# maximum length of the resultant string
def maxsum(S, N, i, dp):
# If index gets out of bound return 0
if (i >= N):
return 0
# If value not already computed
# then compute it
if (dp[i] == -1):
# To include the current string
op1 = int(len(S[i]) + maxsum(S, N, (i + len(S[i]) // 2)+1, dp))
# To exclude the current string
op2 = int(maxsum(S, N, i + 1, dp))
# Maximum of both the options
dp[i] = max(op1, op2)
return dp[i]
# Driver Code
S = ["geeks", "for", "geeks", "is", "best"]
N = len(S)
dp = []
for i in range(0, N):
dp.append(-1)
print(maxsum(S, N, 0, dp))
# This code is contributed by Taranpreet
C#
// C# implementation of the above approach
using System;
public class GFG
{
// Recursive function to find the
// maximum length of the resultant string
static int maxsum(String []S, int N, int i, int[] dp)
{
// If index gets out of bound return 0
if (i >= N)
return 0;
// If value not already computed
// then compute it
if (dp[i] == -1) {
// To include the current string
int op1 = S[i].Length + maxsum(S, N, (i + S[i].Length / 2) + 1, dp);
// To exclude the current string
int op2 = maxsum(S, N, i + 1, dp);
// Maximum of both the options
dp[i] = Math.Max(op1, op2);
}
return dp[i];
}
// Driver Code
public static void Main(String []args)
{
String []S = { "geeks", "for", "geeks", "is", "best" };
int N = S.Length;
int[] dp = new int[N];
for(int i = 0;i
Javascript
输出:
9
时间复杂度: O(N)
辅助空间: O(N)