c ++ 打印字符串_C ++程序打印字符串的所有子序列

c ++ 打印字符串

Description: Solution to print all the subsequences of a string using C++ program.

描述:使用C ++程序打印字符串的所有子序列的解决方案。

Problem statement:

问题陈述:

Write a program that accepts input from user and print all the subsequences of that string.

编写一个程序,接受用户输入并打印该字符串的所有子序列。

Example:

例:

    Input: abcd
    Output: :   a  ab  abcabcdabd  ac  acd  ad  b  bcbcd  bd  c  cd  d

Explanation:

说明:

What is Subsequences?

什么是子序列?

A subsequence is a sequence generated froma string after deleting some characters of string without changing the order of remaining string characters.

子序列是从字符串中删除一些字符串字符后不更改剩余字符串字符顺序而生成的序列。

For example, If we have a string : "abc", Then its subsequences are :- "a", "b", "c", "ab", "ac", "bc", "abc"

例如,如果我们有一个字符串: “ abc” ,那么它的子序列是:- “ a”“ b”“ c”“ ab”“ ac”“ bc”“ abc”

  • First we select first character i.e. 'a' , print it and fix it. ----> a

    首先,我们选择第一个字符即“ a”,将其打印并修复。 ---->一个

  • Take next char i.e. 'b' , print it and fix it ----> ab

    取下一个字符,即'b',打印并修复-------- ab

  • Keep taking next chars until string ends ---->abc

    继续获取下一个字符,直到字符串结束----> abc

  • Delete upto first char , start taking char from second

    删除最多第一个字符,从第二个开始获取字符

  • position from first char till string ends ----> ac

    从第一个字符到字符串结尾的位置----> ac

  • Similarly go for rest chars ---->b , bc , c

    同样去休息字符-> b,bc,c

  • Also we have a result:
    If we have string of N length. Then the number of its non-empty subsequences is (2n -1).

    我们也有一个结果:
    如果我们有N个长度的字符串。 那么其非空子序列的数量为(2 n -1)。

Subsequence vs Substring

子序列与子串

There is difference between subsequence and substring. Many times people thought these terms are same but they are totally different.

子序列和子字符串之间有区别。 很多时候人们认为这些术语是相同的,但完全不同。

Substring is the contiguous part of string. If there is a string of length n, then its number of substrings are n*(n+1)/2.

子字符串是字符串的连续部分。 如果存在长度为n的字符串,则其子字符串的数量为n *(n + 1)/ 2

For example, If we have a string : "abc", Then its substrings are :- "a", "b", "c", "ab", "bc", "abc"

例如,如果我们有一个字符串: “ abc” ,那么它的子字符串是:- “ a”“ b”“ c”“ ab”“ bc”“ abc”

Algorithm:

算法:

  1. Set start=-1, end=len, where len=length of string.

    设置start = -1 , end = len ,其中len =字符串的长度。

  2. Set curStr="", print it

    设置curStr =“” ,打印出来

  3. Fix character and add it into curStr and print curStr.

    修复字符并将其添加到curStr并打印curStr 。

  4. for i = start +1 to end

    因为我=开始+1到结束

    Fix character in

    固定字符

    curStr and prints the string

    curStr并打印字符串

  5. Recursively generate all subsets starting from fix character.

    从固定字符开始递归地生成所有子集。

  6. After each recursive call, remove the last character to generate the next sequence.

    在每个递归调用之后,删除最后一个字符以生成下一个序列。

  7. Clear the curStr

    清除当前

  8. Set start=start+1

    设置开始=开始+1

  9. if start < n , go to step 3.

    如果start <n ,请转到步骤3。

  10. Stop.

    停止。

Time Complexity: O(2n)

时间复杂度:O(2n)

Code

#include <iostream>
#include <string>
using namespace std;

void printSubsequences(string str, int start, int end, string curStr = ""){
	//base case
	if (start == end) {
		return;
	}
	//print current string permutation
	cout<<curStr<< "  ";
	for (int i = start + 1; i< end; i++) {
		curStr += str[i];
		printSubsequences(str, i, end, curStr);
		curStr = curStr.erase(curStr.size() - 1);
	}
}

int main(){
	string s;

	cout<< "Enter string : ";
	cin>> s;
	int len = s.size();
	
	cout<< "Subsequences : ";
	printSubsequences(s, -1, len);
	
	return 0;
}

Output

输出量

Enter string : abcd
Subsequences :   a  ab  abcabcdabd  ac  acd  ad  b  bcbcd  bd  c  cd  d


翻译自: https://www.includehelp.com/cpp-programs/print-all-subsequences-of-a-string.aspx

c ++ 打印字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值