generate combinations

http://compprog.wordpress.com/2007/10/17/generating-combinations-1/

 

#include <iostream>
#include <vector>
using namespace std;


void print(vector<char> chars, char array[], int k){
        for(int j=0;j<k;j++) cout<<chars.at(array[j]);
        cout<<endl;
}

bool next(char array[], int n, int k){
        int i=k-1;
        while(i>0){
                if(array[i] >= n-k+i) i--;
                else break;
        }
        if(i==0 && array[0]== (n-k)) return false;
		array[i]++;
        for(int j=(i+1); j<= k-1; j++)
			array[j]=array[j-1]+1;
        return true;
}

void printAllCombination(std::vector<char> chars, int n, int k){
        char *array = new char[n];
        for(int i=0;i<k;i++)array[i]=i;
        print(chars, array, k);
        while(next(array, n, k)){
                print(chars, array, k);
        }
        delete array;
}
int main()
{
        int k ;
        int n ;
        cin>>k;
        cin>>n;
        std::vector<char> chars;
        for(int i=0;i<n;i++)
        {
                char temp;
                cin>>temp;				 
                chars.push_back(temp);
        }
        printAllCombination(chars, n, k);
        return  0;
}
                 
 

你可能感兴趣的:(wordpress,J#)