📌  相关文章
📜  必须附加一个字符串A的最小子序列才能获得字符串B

📅  最后修改于: 2021-04-17 14:38:12             🧑  作者: Mango

给定两个字符串AB ,任务是通过以下操作计算构造字符串B所需的最少操作数:

  • 选择字符串A的子序列。
  • 将子序列追加到新形成的字符串(最初为空)上。

打印所需的最少操作数。如果无法通过应用给定的操作使新字符串等于B ,则打印-1

例子:

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

  1. 初始化地图绘制目前在各自的指数的字符串中的字符。
  2. 对于字符串A中的每个字符,请跟踪其所有出现的情况。
  3. 初始化一个变量,例如ans ,以存储所需的操作计数。由于操作次数必须大于1 ,因此请设置ans = 1
  4. 遍历字符串B和检查的字符,如果字符是存在于字符串A或不使用地图
  5. 最后,为每个操作最大化从字符串A中选择的子序列的长度。
  6. 最后,打印所需的最少操作。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the minimum
// subsequences of a string A required
// to be appended to obtain the string B
void countminOpsToConstructAString(string A,
                                   string B)
{
    // Size of the string
    int N = A.length();
 
    int i = 0;
 
    // Maps characters to their
    // respective indices
    map > mp;
 
    // Insert indices of characters
    // into the sets
    for (i = 0; i < N; i++) {
        mp[A[i]].insert(i);
    }
 
    // Stores the position of the last
    // visited index in the string A.
    // Initially set it to -1.
    int previous = -1;
 
    // Stores the required count
    int ans = 1;
 
    // Iterate over the characters of B
    for (i = 0; i < B.length(); i++) {
        char ch = B[i];
 
        // If the character in B is
        // not present in A, return -1
        if (mp[ch].size() == 0) {
            cout << -1;
            return;
        }
 
        // Fetch the next index from B[i]'s set
        auto it = mp[ch].upper_bound(previous);
 
        // If the iterator points to
        // the end of that set
        if (it == mp[ch].end()) {
 
            previous = -1;
            ans++;
            --i;
            continue;
        }
 
        // If it doesn't point to the
        // end, update  previous
        previous = *it;
    }
 
    // Print the answer
    cout << ans;
}
 
// Driver Code
int main()
{
    string A = "abc", B = "abac";
    countminOpsToConstructAString(A, B);
 
    return 0;
}


Python3
# Python3 program for the above approac
from bisect import bisect_right
 
# Function to count the minimum
# subsequences of a A required
# to be appended to obtain the B
def countminOpsToConstructAString(A, B):
   
    # Size of the string
    N = len(A)
    i = 0
 
    # Maps characters to their
    # respective indices
    mp = [[] for i in range(26)]
 
    # Insert indices of characters
    # into the sets
    for i in range(N):
        mp[ord(A[i]) - ord('a')].append(i)
 
    # Stores the position of the last
    # visited index in the A.
    # Initially set it to -1.
    previous = -1
 
    # Stores the required count
    ans, i = 1, 0
 
    # Iterate over the characters of B
    while i < len(B):
        ch = B[i]
 
        # If the character in B is
        # not present in A, return -1
        if (len(mp[ord(ch) - ord('a')]) == 0):
            print(-1)
            return
 
        # Fetch the next index from B[i]'s set
        it = bisect_right(mp[ord(ch) - ord('a')], previous)
 
        # If the iterator points to
        # the end of that set
        if (it == len(mp[ord(ch) - ord('a')])):
            previous = -1
            ans += 1
            # i -= 1
            continue
 
        # If it doesn't poto the
        # end, update  previous
        previous = mp[ord(ch) - ord('a')][it]
        i += 1
 
    # Prthe answer
    print (ans)
 
# Driver Code
if __name__ == '__main__':
    A, B = "abc", "abac"
    countminOpsToConstructAString(A, B)
 
    # This code is contributed by mohit kumar 29.


输出:
2

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