小编E. *_*urg的帖子

为什么使用字符串会导致退出代码 3 而使用 char[] 则不会

我正在练习我的编码技能,我正在解决以下回溯问题(原始指南的解决方案也在那里)。

问题总结:

给定一个字符串,您需要打印所有可能的字符串,这些字符串可以通过在它们之间放置空格(零或一)来构成。

我的解决方案如下:

#include <iostream>
#include <cstring>
#include <string>


using namespace std;

void permutationWithSpacesAux(const char* s, string buf, int s_index, int b_index, int len_s){

    // stop condition
    if(s_index == len_s){
        cout << buf << endl;
        return;
    }
    // print w\o space
    buf[b_index] = s[s_index];
    // recursive call w\ next indices
    permutationWithSpacesAux(s, buf, s_index + 1, b_index + 1, len_s);

    // print w\ space
    buf[b_index] = ' ';
    buf[b_index + 1] = s[s_index];
    // recursive call w\ next …
Run Code Online (Sandbox Code Playgroud)

c++ string char backtracking c++11

2
推荐指数
1
解决办法
72
查看次数

标签 统计

backtracking ×1

c++ ×1

c++11 ×1

char ×1

string ×1