📌  相关文章
📜  给定字符串S 中存在的子序列 T 的最大相邻索引差

📅  最后修改于: 2022-05-13 01:56:09.950000             🧑  作者: Mango

给定字符串S 中存在的子序列 T 的最大相邻索引差

给定两个长度分别为nm的字符串ST。查找字符串T 作为子序列存在于字符串S 中的索引的最大相邻索引差。

  • 成本定义为子序列 T 的索引之间的差异
  • 对于1 ≤ i < m ,序列的最大成本定义为max(a i+1 −a i )
  • 保证在 S中至少有 T 的子序列。

例子:

方法:对于字符串S 的某个子序列,令 a i表示字符T i在字符串S 中的位置。对于固定的 i,我们可以找到最大化a i+1 -a i的子序列。

请按照以下步骤解决问题:

  1. left iright i分别是所有有效 a 中 a i的最小和最大可能值。现在,很容易看出 a i+1 - a i最大可能值等于right i+1 - left i
  2. 要计算left i ,我们只需要找到left i-1之后的第一个元素,它等于T i ,right i可以通过反向迭代以相同的方式找到。
  3. 所以,在找到左右之后,答案是max(right i+1 −left i ) ,因为 1 ≤ i < m。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum
// cost of a subsequence
int maxCost(string s, string t, int n, int m)
{
    // mxCost stores the maximum
    // cost of a subsequence
    int mxCost = 0;
 
    // left[i] and right[i] store
    // the minimum and maximum
    // value of ai among all
    // valid a's respectively
    int left[m], right[m];
 
    int j = 0;
    for (int i = 0; i < n; i++) {
        if (j >= m)
            break;
 
        if (s[i] == t[j]) {
            left[j] = i;
            j++;
        }
    }
 
    j = m - 1;
    for (int i = n - 1; i >= 0; i--) {
        if (j < 0)
            break;
 
        if (s[i] == t[j]) {
            right[j] = i;
            j--;
        }
    }
 
    for (int i = 0; i < m - 1; i++) {
        mxCost = max(mxCost,
                     right[i + 1] - left[i]);
    }
 
    return mxCost;
}
 
// Driver function
int main()
{
    string S = "abbbc", T = "abc";
    int n = S.length(), m = T.length();
 
    // Function Call
    cout << maxCost(S, T, n, m);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
  // Function to find the maximum
  // cost of a subsequence
  static int maxCost(String s, String t, int n, int m)
  {
    // mxCost stores the maximum
    // cost of a subsequence
    int mxCost = 0;
 
    // left[i] and right[i] store
    // the minimum and maximum
    // value of ai among all
    // valid a's respectively
    int []left = new int[m];
    int []right = new int[m];
 
    int j = 0;
    for (int i = 0; i < n; i++) {
      if (j >= m)
        break;
 
      if (s.charAt(i) == t.charAt(j)) {
        left[j] = i;
        j++;
      }
    }
 
    j = m - 1;
    for (int i = n - 1; i >= 0; i--) {
      if (j < 0)
        break;
 
      if (s.charAt(i) == t.charAt(j)) {
        right[j] = i;
        j--;
      }
    }
 
    for (int i = 0; i < m - 1; i++) {
      mxCost = Math.max(mxCost,
                        right[i + 1] - left[i]);
    }
 
    return mxCost;
  }
 
  // Driver function
  public static void main(String[] args)
  {
    String S = "abbbc", T = "abc";
    int n = S.length(), m = T.length();
 
    // Function Call
    System.out.print(maxCost(S, T, n, m));
  }
}
 
// This code is contributed by 29AjayKumar


Python
# Python program for the above approach
 
# Function to find the maximum
# cost of a subsequence
def maxCost(s, t, n, m):
     
    # mxCost stores the maximum
    # cost of a subsequence
    mxCost = 0
 
    # left[i] and right[i] store
    # the minimum and maximum
    # value of ai among all
    # valid a's respectively
    left = []
    right = []
 
    j = 0
    for i in range(0, n):
        if (j >= m):
            break
 
        if (s[i] == t[j]):
            left.append(i)
            j += 1
 
    j = m - 1
    for i in reversed(range(n)):
        if (j < 0):
            break
 
        if (s[i] == t[j]):
            right.append(i)
            j -= 1
 
    for i in range(0, m - 1):
        mxCost = max(mxCost, right[i + 1] - left[i])
 
    return mxCost
 
# Driver function
S = "abbbc"
T = "abc"
n = len(S)
m = len(T)
 
print(maxCost(S, T, n, m))
 
# This code is contributed by Samim Hossain Mondal.


C#
// C# program for the above approach
using System;
class GFG{
 
  // Function to find the maximum
  // cost of a subsequence
  static int maxCost(String s, String t, int n, int m)
  {
 
    // mxCost stores the maximum
    // cost of a subsequence
    int mxCost = 0;
 
    // left[i] and right[i] store
    // the minimum and maximum
    // value of ai among all
    // valid a's respectively
    int []left = new int[m];
    int []right = new int[m];
 
    int j = 0;
    for (int i = 0; i < n; i++) {
      if (j >= m)
        break;
 
      if (s[i] == t[j]) {
        left[j] = i;
        j++;
      }
    }
 
    j = m - 1;
    for (int i = n - 1; i >= 0; i--) {
      if (j < 0)
        break;
 
      if (s[i] == t[j]) {
        right[j] = i;
        j--;
      }
    }
 
    for (int i = 0; i < m - 1; i++) {
      mxCost = Math.Max(mxCost,  right[i + 1] - left[i]);
    }
 
    return mxCost;
  }
 
  // Driver function
  public static void Main()
  {
    String S = "abbbc", T = "abc";
    int n = S.Length, m = T.Length;
 
    // Function Call
    Console.Write(maxCost(S, T, n, m));
  }
}
 
// This code is contributed by gfgking


Javascript



输出
3

时间复杂度 O(n + m)
辅助空间 O(n)