使用C++使兩個字串相等的所需最小運算元。


問題陳述

給定兩個字串str1和str2,兩個字串都包含字元'a'和'b'。兩個字串長度相等。兩個字串中都包含一個下劃線_(空格)。任務是透過執行以下操作的最小次數將第一個字串轉換為第二個字串:

  • 如果下劃線位於位置i,則下劃線可以與位置i+1或i-1處的字元交換。

  • 如果位置i+1和i+2處的字元不同,則下劃線可以與位置i+1或i+2處的字元交換。

  • 類似地,如果位置i-1和i-2處的字元不同,則下劃線可以與位置i-1或i-2處的字元交換。

如果str1 = “aba_a”並且str2 = “_baaa”,則需要2次移動才能將str1轉換為str2:

1. str1 = “ab_aa” (Swapped str1[2] with str1[3])
2. str2 = “_baaa” (Swapped str1[0] with str1[2])

演算法

1. Apply a simple Breadth First Search over the string and an element of the queue used for BFS will contain the pair str, pos where pos is the position of _ in the string str.
2. Also maintain a map namely ‘vis’ which will store the string as key and the minimum moves to get to the string as value.
3. For every string str from the queue, generate a new string tmp based on the four conditions given and update the vis map as vis[tmp] = vis[str] + 1.
4. Repeat the above steps until the queue is empty or the required string is generated i.e. tmp == B
5. If the required string is generated, then return vis[str] + 1 which is the minimum number of operations required to change A to B.

示例

#include <iostream>
#include <string>
#include <unordered_map>
#include <queue>
using namespace std;
int transformString(string str, string f){
   unordered_map<string, int> vis;
   int n;
   n = str.length();
   int pos = 0;
   for (int i = 0; i < str.length(); i++) {
      if (str[i] == '_') {
         pos = i;
         break;
      }
   }
   queue<pair<string, int> > q;
   q.push({ str, pos });
   vis[str] = 0;
   while (!q.empty()) {
      string ss = q.front().first;
      int pp = q.front().second;
      int dist = vis[ss];
      q.pop();
      if (pp > 0) {
         swap(ss[pp], ss[pp - 1]);
         if (!vis.count(ss)) {
            if (ss == f) {
               return dist + 1;
               break;
            }
            vis[ss] = dist + 1;
            q.push({ ss, pp - 1 });
         }
         swap(ss[pp], ss[pp - 1]);
      }
      if (pp < n - 1) {
         swap(ss[pp], ss[pp + 1]);
         if (!vis.count(ss)) {
         if (ss == f) {
            return dist + 1;
            break;
         }
         vis[ss] = dist + 1;
         q.push({ ss, pp + 1 });
      }
      swap(ss[pp], ss[pp + 1]);
   }
   if (pp > 1 && ss[pp - 1] != ss[pp - 2]) {
      swap(ss[pp], ss[pp - 2]);
      if (!vis.count(ss)) {
         if (ss == f) {
            return dist + 1;
            break;
         }
         vis[ss] = dist + 1;
         q.push({ ss, pp - 2 });
      }
      swap(ss[pp], ss[pp - 2]);
   }
   if (pp < n - 2 && ss[pp + 1] != ss[pp + 2]) {
      swap(ss[pp], ss[pp + 2]);
      if (!vis.count(ss)) {
         if (ss == f) {
            return dist + 1;
            break;
         }
         vis[ss] = dist + 1;
         q.push({ ss, pp + 2 });
      }
      swap(ss[pp], ss[pp + 2]);
      }
   }
   return 0;
}
int main(){
   string str1 = "aba_a";
   string str2 = "_baaa";
   cout << "Minimum required moves: " << transformString(str1, str2) << endl;
   return 0;
}

輸出

編譯並執行上述程式時,將生成以下輸出:

Minimum required moves: 2

更新於:2019年10月31日

426 次瀏覽

開始您的職業生涯

完成課程獲得認證

開始
廣告