转换字符串使其包含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作为子序列。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP