您的位置:首页 > 其它

[算法] 输出 字符串的全部子组合 [dfs - 递归神技]

2013-02-05 16:35 281 查看
给定一个字符串S,其中S中的字符互不相同,输出S中字符的所有组合,

如S = "abc", 则输出:空,a, b, c, ab, ac, bc, abc,

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
const int N = 1005;
void dfs(char *arr, int id, int count, char* buf) {
if(arr[id] == '\0') {
buf[count] = '\0';
cout << buf << endl;
}
else {
dfs(arr, id+1, count, buf);
buf[count] = arr[id];
dfs(arr, id+1, count+1, buf);
}
}
void test(char *str) {
int len = strlen(str);
char *buf = new char[len+1];
dfs(str, 0, 0, buf);
}
int main() {
test("abcde");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: