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

typedef void showproc(const string &result);
typedef showproc *showptr;

void show(const string &result)
{
    cout<<result<<endl;
}

string prepare(const vector<int> &idx,const string &alphas)
{
    string str(idx.size(),'\0');
    for(size_t i=0;i<idx.size();++i) str[i]=alphas[idx[i]];
    return str;
}

bool next(vector<int> &idx,size_t maxvalue)
{
    for(size_t i=idx.size()-1;i<idx.size();idx[i--]=0) if(++idx[i]<maxvalue) return true;
    return false;
}

void permutations(size_t size,const string &alphas,showptr show)
{
    vector<int> idx(size,0);
    do { show(prepare(idx,alphas)); } while(next(idx,alphas.size()));
}

void permutations(size_t minsize,size_t maxsize,string alphas,showptr show)
{
    for(size_t size=minsize;size<=maxsize;++size) permutations(size,alphas,show);
}

int main()
{
    permutations(2,4,"ABCD",&show);    
}