Files
python/LuoGu/RuMen/P_002.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

59 lines
1.4 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
/**
* 功能:字符串的所有组合
* 作者:网上大神
* 时间: 2019-11-27
* @param ptr
* @param n
* @param result
*/
vector<string> v_list;
void combination(char *ptr, int n, vector<char> &result) {
if (ptr == NULL)
return;
if (n == 0) {
vector<char>::iterator iter = result.begin();
string c1 = "";
for (; iter != result.end(); ++iter) {
c1 += *iter;
}
v_list.push_back(c1);
return;
}
if (*ptr == '\0')
return;
result.push_back(*ptr);
combination(ptr + 1, n - 1, result); //若把第一个字符放到组合中去则需要在剩下的n-1个字符中选取m-1个字符。
result.pop_back(); //弹出容器的顶层元素
combination(ptr + 1, n, result);
}
void combination(char *ptr) {
if (ptr == NULL)
return;
vector<char> result;
int i, length = strlen(ptr);
for (i = 1; i <= length; ++i) {
combination(ptr, i, result);
}
}
int main() {
//读入输出优化的强迫症
ios::sync_with_stdio(false);
char s[] = "abc";
//求出所有组合
combination(s);
//输出所有组合(注意:不是排列,是所有组合)
for (auto val : v_list) {
cout << val << endl;
}
return 0;
}