next_permunation用时更短0.000,固定几个字母的组合都可考虑next_permutation
前一篇的递归枚举用时0.100
/*******************************************************/
/* UVa 729 The Hamming Distance Problem */
/* Author: LanyiKnight [at] knightzone.org */
/* Version: 2016/07/27 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int main(){
int numberOfDataset;
while( scanf("%d", &numberOfDataset) != EOF ){
for( int testNumber = 0 ; testNumber < numberOfDataset ; ++testNumber ){
if( testNumber > 0 ){
printf("\n");
}
int N, H;
scanf("%d%d", &N, &H);
string s;
for( int i = 0 ; i < N-H ; ++i ){
s += '0';
}
for( int i = 0 ; i < H ; ++i ){
s += '1';
}
do{
printf("%s\n", s.c_str());
} while( next_permutation(s.begin(), s.end()) );
}
}
return 0;
}