轉換字串使其包含abcd..z作為子序列
字串轉換,也稱為字串變換,是C++中的一種操作,其結果在整個過程執行後儲存在輸出陣列中。在C++中,有一個名為“transform()”的函式,存在於C++環境的目錄中,我們可以用它將一個字串轉換為新的字串。
此轉換函式有兩種形式:
一元運算
運算應用於輸入陣列的每個元素。
運算完成後,結果將儲存在輸出陣列中。
二元運算
運算應用於特定陣列的每個元素。
第一個輸入元素和第二個相應的輸入元素參與運算。
輸出資料將儲存在輸出陣列中。
子序列字串是一個全新的字串,它是透過對輸入字串執行各種操作(例如:刪除)生成的。對於子序列字串,操作發生時不會改變剩餘字元。
對於字串轉換,輸入包含長度為n+1的操作字串。原始字元屬於a到z的序列。此處列印的字串長度視為n,它是輸出字串。
在本文中,我們將學習如何使用C++環境轉換字串,使其包含abcd….z作為子序列。
遞迴子序列演算法
使用遞迴方法,這裡是一個可能的子序列演算法。這是一個特定的字串,T是完成操作所花費的時間。
步驟1 - 計數出現。
步驟2 - 如果i=length(s)且j=length(T)。
步驟3 - 則返回1。
步驟4 - 結束。
步驟5 - 如果i=length(S)。
步驟6 - 則返回0。
步驟7 - 結束。
步驟8 - Count <-- 0。
步驟9 - 如果j
步驟10 - Count <-- count + countOccurences(i+1,j+1)。
步驟11 - 結束。
步驟12 - Count <-- count + countOccurences(i+1,j)。
步驟13 - 返回count。
步驟14 - 結束。
子序列陣列語法
Here, we have two given sequences. X and Y. Initialize a table with a dimension of X.length * Y.length X.label1 = X Y.label2 = Y CS1[0][] = 0 CS2[][0] = 0 Start from CS[1][1] Compare X[i] and Y[j] If X[i] = Y[j] CS[i][j] = 1 + CS[i-1, j-1] Point an arrow to CS[i][j] Else CS[i][j] = max(CS[i-1][j], CS[i][j-1]) Point an arrow to max(CS[i-1][j], CS[i][j-1])
在這裡,我們建立了子序列陣列的基本工作語法。當存在兩個序列時,我們必須遵循以下步驟才能獲得輸出。
遵循的方法
方法1 - 使用C++轉換字串
方法2 - 使用C++對字串進行一元運算
方法3 - 使用C++對字串進行二元運算
方法4 - 使用C++列印所有可能的子序列字串
方法5 - 使用C++轉換字串,使其包含abcd….z作為子序列
使用C++轉換字串
在這個C++程式碼中,我們建立了一個新字串,並從輸入字串中刪除所有母音。在輸出中,#已新增到母音的位置。
示例1
#include <bits/stdc++.h> using namespace std; string change_case(string r) { int l = r.length(); for(int i = 0 ; i < l ; i++) { if(r[i] >= 'a' && r[i] <= 'z') r[i] = r[i] - 32; else if(r[i] >= 'A' && r[i] <= 'Z') r[i] = r[i] + 32; } return r; } string delete_vowels(string a) { string temp = ""; int l = a.length(); for(int i = 0 ; i < l ; i++) { if(a[i] != 'a' && a[i] != 'e' && a[i] != 'i' && a[i] != 'o' && a[i] != 'u' && a[i] != 'A' && a[i] != 'E' && a[i] != 'O' && a[i] != 'U'&& a[i] != 'I') temp += a[i]; } return temp; } string insert_hash(string a) { string temp = ""; int l = a.length(); for(int i = 0 ; i < l ; i++) { if((a[i] >= 'a' && a[i] <= 'z') || (a[i] >= 'A' && a[i] <= 'Z')) temp = temp + '#' + a[i]; else temp = temp + a[i]; } return temp; } void transformSting(string a) { string b = delete_vowels(a); string c = change_case(b); string d = insert_hash(c); if(d=="") cout<<"-1"<<endl; else cout << d<<endl; } int main() { string a = "RudraDevDas!!"; string b = "aeiou"; transformSting(a); transformSting(b); return 0; }
輸出
#r#D#R#d#V#d#S!! -1
使用C++對字串進行一元運算
在這段程式碼中,我們演示了一元運算如何作用於輸入陣列。該函式獲取單個輸入的起始和結束指標。運算後,結果儲存到輸出陣列的起始位置。
示例2
#include <iostream> #include <algorithm> using namespace std; int op_increment (int x) { x = x + 1; return x; } int main () { int n = 5; int input_array[] = {7, 16, 10, 97, 2001}; int output_array[n]; std::cout << "Input array present here:"; for(int i=0; i<5; i++){ cout << ' ' << input_array[i]; } cout << '\n'; transform (input_array, input_array+5, output_array, op_increment); std::cout << "The output array now contains with:"; for(int i=0; i<5; i++){ cout << ' ' << output_array[i]; } cout << '\n'; return 0; }
輸出
Input array present here: 7 16 10 97 2001 The output array now contains with: 8 17 11 98 2002
使用C++對字串進行二元運算
在這段程式碼中,我們演示了二元運算如何作用於輸入陣列。transform()函式將指標附加到起始點和第一個輸入陣列的結束點。請記住,二元運算始終作用於兩個輸入資料集。
示例3
#include <iostream> #include <algorithm> #include <vector> using namespace std; int op_add (int i, int j) { return i+j; } int main () { int n = 5; int arr1[] = {7, 16, 10, 2001, 1997}; int arr2[] = {1, 2, 3, 4, 5}; int output[n]; std::cout << "Input data in array1:"; for(int i=0; i<n; i++){ cout << ' ' << arr1[i]; } cout << '\n'; std::cout << "Input data in array2:"; for(int i=0; i<n; i++){ cout << ' ' << arr2[i]; } cout << '\n'; std::transform (arr1, arr1+n, arr2, output, op_add); std::cout << "Output array is here now:"; for(int i=0; i<5; i++){ cout << ' ' << output[i]; } cout << '\n'; return 0; }
輸出
Input data in array1: 7 16 10 2001 1997 Input data in array2: 1 2 3 4 5 Output array is here now: 8 18 13 2005 2002
使用C++列印所有子序列字串
應用“選擇”和“不選擇”的概念來找出特定陣列的所有子序列。在這個過程中,可能會刪除一些字元而不會改變元素的順序。此過程的時間複雜度為O(2^n),空間複雜度為O(n)。
示例4
#include <bits/stdc++.h> using namespace std; void printSubsequence(string input, string output) { if (input.empty()) { cout << output << endl; return; } printSubsequence(input.substr(1), output + input[0]); printSubsequence(input.substr(1), output); } int main() { string output = ""; string input = "rudraabonikoaa"; printSubsequence(input, output); return 0; }
輸出
rudraabonikoaa rudraabonikoa rudraabonikoa rudraaboniko rudraabonikaa rudraabonika rudraabonika rudraabonik rudraabonioaa rudraabonioa rudraabonioa rudraabonio rudraaboniaa rudraabonia rudraabonia
轉換字串使其包含abcd…z作為子序列
這是將字串轉換為包含abcd...z作為子序列的特定過程。
初始化字元。
如果長度小於26,則返回false。
迭代迴圈從0到s.size()-1。
如果字元達到z,則中斷迴圈。
如果當前字元小於或等於該字元。
將當前字元替換為加1。
如果字元小於或等於z,則返回false。
否則,返回true。
此過程的時間複雜度為O(n),輔助空間為O(1)。這裡,n是特定字串的長度。
示例5
#include <bits/stdc++.h> using namespace std; bool transformString(string& s) { char ch = 'a'; if (s.size() < 26) return false; for (int i = 0; i < s.size(); i++) { if (int(ch) > int('z')) break; if (s[i] <= ch) { s[i] = ch; ch = char(int(ch) + 1); } } if (ch <= 'z') return false; return true; } int main() { string str = "aaaaaaaaaaaaaaaaaaaaaaaaaaa"; if (transformString(str)) cout << str << endl; else cout << "Not Possible" << endl; return 0; }
輸出
abcdefghijklmnopqrstuvwxyza
結論
在本文中,我們學習了使用C++環境進行字串轉換及其不同形式。透過遵循特定的演算法和語法,我們檢查並構建了一些不同的C++程式碼,並瞭解瞭如何轉換字串,使其包含abcd...z作為子序列。