打印字符串的所有子序列
原文:
给定一个字符串 str ,任务是打印 str 的所有子序列。 a 是一个序列,可以通过删除一些元素或不删除元素而不改变剩余元素的顺序,从另一个序列中派生出来。 示例:
输入:【str = " abc】 输出:a b ab ac bc abc 输入:【str = " geek】输出:g e ge ee gee gk ek geke
方法:编写一个递归函数,在每个子序列的开头追加字符串的第一个字符str【0】之后,打印从第二个字符str【1,n–1】开始的子字符串的每个子序列。终止条件将是当传递的字符串为空时,在这种情况下,函数将返回一个空的。 以下是上述方法的实施:
c
// c implementation of the approach
#include
using namespace std;
// utility function to print the contents
// of the list
void printlist(vector arrl)
{
arrl.erase(find(arrl.begin(), arrl.end(), ""));
for (int i = 0; i < arrl.size(); i )
cout << arrl[i] << " ";
}
// function to returns the arraylist which contains
// all the sub-sequences of str
vector getsequence(string str)
{
// if string is empty
if (str.length() == 0)
{
// return an empty arraylist
vector empty;
empty.push_back("");
return empty;
}
// take first character of str
char ch = str[0];
// take sub-string starting from the
// second character
string substr = str.substr(1);
// recurvise call for all the sub-sequences
// starting from the second character
vector subsequences = getsequence(substr);
// add first character from str in the beginning
// of every character from the sub-sequences
// and then store it into the resultant arraylist
vector res;
for(string val : subsequences)
{
res.push_back(val);
res.push_back(ch val);
}
// return the resultant arraylist
return res;
}
int main()
{
string str = "geek";
printlist(getsequence(str));
return 0;
}
// this code is contributed by rameshtravel07.
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the approach
import java.util.arraylist;
public class gfg {
// utility function to print the contents
// of the arraylist
static void printarraylist(arraylist arrl)
{
arrl.remove("");
for (int i = 0; i < arrl.size(); i )
system.out.print(arrl.get(i) " ");
}
// function to returns the arraylist which contains
// all the sub-sequences of str
public static arraylist getsequence(string str)
{
// if string is empty
if (str.length() == 0) {
// return an empty arraylist
arraylist empty = new arraylist<>();
empty.add("");
return empty;
}
// take first character of str
char ch = str.charat(0);
// take sub-string starting from the
// second character
string substr = str.substring(1);
// recurvise call for all the sub-sequences
// starting from the second character
arraylist subsequences =
getsequence(substr);
// add first character from str in the beginning
// of every character from the sub-sequences
// and then store it into the resultant arraylist
arraylist res = new arraylist<>();
for (string val : subsequences) {
res.add(val);
res.add(ch val);
}
// return the resultant arraylist
return res;
}
// driver code
public static void main(string[] args)
{
string str = "geek";
printarraylist(getsequence(str));
// system.out.print(getsequence(str));
}
}
python 3
# python implementation of the approach
# utility function to print the contents
# of the arraylist
def printarraylist(arrl):
arrl.remove("")
print(*arrl, sep = " ")
# function to returns the arraylist which contains
# all the sub-sequences of str
def getsequence(str):
# if string is empty
if(len(str) == 0):
# return an empty arraylist
empty = []
empty.append("")
return empty
# take first character of str
ch = str[0]
# take sub-string starting from the
# second character
substr = str[1:]
# recurvise call for all the sub-sequences
# starting from the second character
subsequences = getsequence(substr)
# add first character from str in the beginning
# of every character from the sub-sequences
# and then store it into the resultant arraylist
res = []
for val in subsequences:
res.append(val)
res.append(ch val)
# return the resultant arraylist
return res
# driver code
str = "geek"
printarraylist(getsequence(str))
# this code is contributed by avanitrachhadiya2155
c
// c# implementation of the approach
using system;
using system.collections.generic;
class gfg{
// utility function to print the contents
// of the list
static void printlist(list arrl)
{
arrl.remove("");
for (int i = 0; i < arrl.count; i )
console.write(arrl[i] " ");
}
// function to returns the arraylist which contains
// all the sub-sequences of str
public static list getsequence(string str)
{
// if string is empty
if (str.length == 0)
{
// return an empty arraylist
list empty = new list();
empty.add("");
return empty;
}
// take first character of str
char ch = str[0];
// take sub-string starting from the
// second character
string substr = str.substring(1);
// recurvise call for all the sub-sequences
// starting from the second character
list subsequences = getsequence(substr);
// add first character from str in the beginning
// of every character from the sub-sequences
// and then store it into the resultant arraylist
list res = new list();
foreach (string val in subsequences)
{
res.add(val);
res.add(ch val);
}
// return the resultant arraylist
return res;
}
// driver code
public static void main(string[] args)
{
string str = "geek";
printlist(getsequence(str));
// console.write(getsequence(str));
}
}
// this code is contributed by rohit_ranjan
java 描述语言
output:
g e ge e ge ee gee k gk ek gek ek gek eek geek
备选方案:逐个固定字符,并从它们开始递归生成所有子集。
c
// c implementation of the approach
#include
using namespace std;
// function to print all the sub-sequences
// of a string
void printsubseq(string sub, string ans)
{
if (sub.length() == 0) {
cout << "" << ans << " ";
return;
}
// first character of sub
char ch = sub[0];
// sub-string starting from second
// character of sub
string ros = sub.substr(1);
// excluding first character
printsubseq(ros, ans);
// including first character
printsubseq(ros, ans ch);
}
// driver code
int main()
{
string str = "abc";
printsubseq(str, "");
return 0;
}
// this code is contributed by divyesh072019
java 语言(一种计算机语言,尤用于创建网站)
// java implementation of the approach
public class sub_sequence {
// function to print all the sub-sequences
// of a string
public static void printsubseq(string sub,
string ans)
{
if (sub.length() == 0) {
system.out.print("" ans " ");
return;
}
// first character of sub
char ch = sub.charat(0);
// sub-string starting from second
// character of sub
string ros = sub.substring(1);
// excluding first character
printsubseq(ros, ans);
// including first character
printsubseq(ros, ans ch);
}
// driver code
public static void main(string[] args)
{
string str = "abc";
printsubseq(str, "");
}
}
python 3
# python3 implementation of the approach
# function to print all the sub-sequences
# of a string
def printsubseq(sub, ans) :
if (len(sub) == 0) :
print(ans , end = " ")
return
# first character of sub
ch = sub[0]
# sub-string starting from second
# character of sub
ros = sub[1 : ]
# excluding first character
printsubseq(ros, ans)
# including first character
printsubseq(ros, ans ch)
str = "abc"
printsubseq(str, "")
# this code iscontributed by divyeshrabadiya07
c
// c# implementation of the approach
using system;
class gfg
{
// function to print all the
// sub-sequences of a string
public static void printsubseq(string sub,
string ans)
{
if (sub.length == 0)
{
console.write("" ans " ");
return;
}
// first character of sub
char ch = sub[0];
// sub-string starting from second
// character of sub
string ros = sub.substring(1);
// excluding first character
printsubseq(ros, ans);
// including first character
printsubseq(ros, ans ch);
}
// driver code
public static void main()
{
string str = "abc";
printsubseq(str, "") ;
}
}
// this code is contributed by ryuga
java 描述语言
output:
c b bc a ac ab abc
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处