#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);
}
https://ideone.com/92Doin
import java.io.*;
public class Main
{
public static void main(String[] args)
{
Permutation.make(2,4,"ABCD".toCharArray(),(str) -> System.out.println(str) );
}
}
class Permutation
{
public interface YeldString { void Yeld(String str); }
private static boolean next(int[] idx,int maxvalue)
{
for(int i=idx.length-1;i>=0;idx[i--]=0) if(++idx[i]<maxvalue) return true;
return false;
}
private static String prepare(int[] idx,char[] alphas)
{
char[] str=new char[idx.length];
for(int i=0;i<idx.length;++i) str[i]=alphas[idx[i]];
return new String(str);
}
public static void make(int size,char[] alphas,YeldString yeld)
{
int[] idx=new int[size];
do { yeld.Yeld(prepare(idx,alphas)); } while(next(idx,alphas.length));
}
public static void make(int minsize,int maxsize,char[] alphas,YeldString yeld)
{
for(int size=minsize;size<=maxsize;++size) make(size,alphas,yeld);
}
}
https://ideone.com/lmMyz1