给定的长度为N的字符串str,任务是打印给定的字符串的所有可能的非空的子序列,使得所述子序列或者包含字符或从给定字符串中的字符的ASCII值。
例子:
Input: str = “ab”
Output: b 98 a ab a98 97 97b 9798
Explanation:
Possible subsequence of the strings are { b, a, ab }.
Possible subsequences of the string generated by including either the characters or the ASCII value of the characters from the given string are { 98, b, a, 97, ab, 97b, a98, 9798 }.
Therefore, the required output is { b, 98, a, ab, a98, 97, 97b, 9798 }.
Input: str = “a”
Output: a 97
方法:请按照以下步骤解决问题:
- 使用变量i遍历给定字符串的每个字符,以生成字符串的所有可能子序列。
- 对于每个第i个字符,可以执行以下三个操作:
- 在子序列中包括str的第i个字符。
- 不要在子序列中包含str的第i个字符。
- 在子序列中包括str的第i个字符的ASCII值。
- 因此,解决该问题的递归关系如下:
FindSub(str, res, i) = { FindSub(str, res, i + 1), FindSub(str, res + str[i], i + 1), FindSub(str, res + ASCII(str[i]), i + 1) }
res = subsequence of the string
i = index of a character in str
- 使用上述递归关系,根据给定条件打印所有可能的子序列。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print subsequences containing
// ASCII value of the characters or the
// the characters of the given string
void FindSub(string str, string res,
int i)
{
// Base Case
if (i == str.length())
{
// If length of the
// subsequence exceeds 0
if (res.length() > 0)
{
// Print the subsequence
cout << res << " ";
}
return;
}
// Stores character present at
// i-th index of str
char ch = str[i];
// If the i-th character is not
// included in the subsequence
FindSub(str, res, i + 1);
// Including the i-th characer
// in the subsequence
FindSub(str, res + ch, i + 1);
// Include the ASCII value of the
// ith character in the subsequence
FindSub(str, res + to_string(int(ch)), i + 1);
}
// Driver Code
int main()
{
string str = "ab";
string res = "";
// Stores length of str
int N = str.length();
FindSub(str, res, 0);
}
// This code is contributed by ipg2016107
Java
// Java program to implement
// the above approach
class GFG {
// Function to print subsequences containing
// ASCII value of the characters or the
// the characters of the given string
static void FindSub(String str, String res,
int i)
{
// Base Case
if (i == str.length()) {
// If length of the
// subsequence exceeds 0
if (res.length() > 0) {
// Print the subsequence
System.out.print(res + " ");
}
return;
}
// Stores character present at
// i-th index of str
char ch = str.charAt(i);
// If the i-th character is not
// included in the subsequence
FindSub(str, res, i + 1);
// Including the i-th characer
// in the subsequence
FindSub(str, res + ch, i + 1);
// Include the ASCII value of the
// ith character in the subsequence
FindSub(str, res + (int)ch, i + 1);
;
}
// Driver Code
public static void main(String[] args)
{
String str = "ab";
String res = "";
// Stores length of str
int N = str.length();
FindSub(str, res, 0);
}
}
Python3
# Python3 program to implement
# the above approach
# Function to print subsequences containing
# ASCII value of the characters or the
# the characters of the given string
def FindSub(string , res, i) :
# Base Case
if (i == len(string)):
# If length of the
# subsequence exceeds 0
if (len(res) > 0) :
# Print the subsequence
print(res, end=" ");
return;
# Stores character present at
# i-th index of str
ch = string[i];
# If the i-th character is not
# included in the subsequence
FindSub(string, res, i + 1);
# Including the i-th characer
# in the subsequence
FindSub(string, res + ch, i + 1);
# Include the ASCII value of the
# ith character in the subsequence
FindSub(string, res + str(ord(ch)), i + 1);
# Driver Code
if __name__ == "__main__" :
string = "ab";
res = "";
# Stores length of str
N = len(string);
FindSub(string, res, 0);
# This code is contributed by AnkitRai01
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to print subsequences containing
// ASCII value of the characters or the
// the characters of the given string
static void FindSub(string str, string res,
int i)
{
// Base Case
if (i == str.Length)
{
// If length of the
// subsequence exceeds 0
if (res.Length > 0)
{
// Print the subsequence
Console.Write(res + " ");
}
return;
}
// Stores character present at
// i-th index of str
char ch = str[i];
// If the i-th character is not
// included in the subsequence
FindSub(str, res, i + 1);
// Including the i-th characer
// in the subsequence
FindSub(str, res + ch, i + 1);
// Include the ASCII value of the
// ith character in the subsequence
FindSub(str, res + (int)ch, i + 1);
}
// Driver Code
public static void Main(String[] args)
{
string str = "ab";
string res = "";
// Stores length of str
int N = str.Length;
FindSub(str, res, 0);
}
}
// This code is contributed by AnkitRai01
b 98 a ab a98 97 97b 9798
时间复杂度: O(3 N )
辅助空间: O(N)