How can I get all combinations of 5 words from 10 words.
For example I have 3 words and I want to get all combinations of 2 words.
"A", "B", "C" it would like AB, BC, AC.
Maybe you know some usefull code or example.
Thanx a lot.
P.S. Sorry if I'm not right enough cause I don't know English very good.
With Perl and the CPAN module Math::Combinatoric:
% perl -MMath::Combinatorics -le'
@n = qw(A B C);
print join ",",
map { join "", @$_ }
combine 2, @n
'
AB,AC,BC
Actually,I think "AB" and "BA" should be treat as two different conditions.If so,my code shows how to do this(produce 2 words from 3 words).While if not,before doing the count(),you can sort the oss.str() to make sure there is only "AB" or "BA" in the set.
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <list>
#include <set>
using namespace std;
int main()
{
string ch[] = {"THIS","IS","OK"};
vector<string> vec(ch,ch+3);
set<string> st;
sort(vec.begin(),vec.end());
do{
ostringstream oss;
vector<string>::iterator pos = vec.begin();
oss<<*pos<<*(pos+1);
if(!st.count(oss.str()))
st.insert(oss.str());
}while(next_permutation(vec.begin(),vec.end()));
copy(st.begin(),st.end(),ostream_iterator<string>(cout,"\n"));
}