卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16960本站已运行3323

将一个字符串转换为其具有abcd..z作为子序列的形式

将一个字符串转换为其具有abcd..z作为子序列的形式

字符串转换(也称为字符串转换)是 C++ 中的一种操作,在整个过程执行后,将结果存储在输出数组中。在C++中,有一个名为“transform()”的函数,存在于C++环境的目录中,通过它我们可以将字符串转换为新的字符串。

有两种形式的转换函数−

  • 一元运算

    • 操作应用于输入数组的每个元素。

    • 手术完成后,结果将存储在一个输出数组中。

  • 二元操作

  • 操作适用于特定数组的每个元素。

  • 第一个输入元素和第二个相应的输入元素参与了操作。

  • 输出数据将存储在一个输出数组中。

子序列字符串是由对输入字符串执行各种操作(例如:删除)生成的全新字符串。对于子序列字符串,操作发生时不会影响剩余的字符。

对于字符串转换,输入包含长度为n+1的操作字符串。原始字符属于a到z的系列。打印字符串的长度在这里被视为n,这里是一个输出字符串。

在本文中,我们将学习如何在C++环境中转换一个字符串,使其具有abcd….z作为一个子序列。

递归算法生成后续字符串

通过使用递归方法,下面是一个可能的算法用于一个后续字符串。这是特定的字符串,T是完成操作所需的时间。

  • 步骤 1 - 计算出现次数。

  • 步骤2 - 如果i = length(s)且j = length(T)。

  • 第三步−然后返回1。

  • 第4步 - 结束。

  • 步骤5 - 如果i = length(S)。

  • 步骤6 - 然后返回0。

  • 第7步 - 结束。

  • 步骤 8 − 计数 <-- 0。

  • 步骤9 - 如果,j

  • 步骤10 − Count<-- count + countOccurences(i+1,j+1).

  • 第11步 - 结束。

  • 第12步 - Count<-- count + countOccurences(i+1,j)。

  • 第13步 - 返回计数。

  • 第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++转换字符串

  • 通过使用C++对字符串进行一元操作的方法2

  • 使用C++对字符串进行二进制操作的方法3

  • 使用C++打印所有可能的后续字符串

  • 使用C++将字符串转换为具有abcd….z作为子序列的方法5

使用 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++对字符串进行一元操作

在这段特定的代码中,我们展示了如何对输入数组进行一元操作。该函数接受一个指向单个输入的起始和结束位置的指针。并在输出数组的起始位置进行操作。

Example 2

的中文翻译为:

示例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()在起始点和第一个输入数组之间添加了一个指针。请记住,二进制操作总是在两个输入数据集上进行。

Example 3

的中文翻译为:

示例 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,则跳出循环。

  • 如果当前字符小于s或等于字符。

  • 将当前字符的增量替换为1。

  • 如果字符小于或等于 z,则返回 false。

  • 否则,返回true。

在这个过程中,时间复杂度为O(n),辅助空间为O(1)。这里,n是特定字符串的长度。

Example 5

的中文翻译为:

示例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作为子序列。

卓越飞翔博客
上一篇: php有哪些爬虫模块类型
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏