You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# 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 ;
}