stephenbrooks.orgForumTalkSensibleAlgorithms - string array combinations
 tgkprog2008-04-04 00:18:23 i have a sorted list of words can someone come up with a algorithm for combinations of two or more words seperated by space?it can be unique,examplehomeran tomAnswerhome ranhome tomran tomhome ran tomnote :1. order is not important so the answer should not have 'home ran tom' and 'home tom ran' only 1 of them2. the answer set can have 'home ran' or 'ran home' but not both meaning the order of the words can be any in the final line items but each line item should contain unique set of wordscombinations have to include 2 and larger sets so if there are 4 words:fasthomeran tomAnswer:fast homehome ranran tomfast ranhome tomfast tomfast home ranhome ran tomfast home tomfast home ran tom http://en.wikipedia.org/wiki/Permutation#Counting_permutations has background, but above me to make a algorithm.super would be working code in c or java or vba (anyone who has excel can do it then!)thank you
 Stephen Brooks2008-04-05 18:08:26 You missed "fast ran tom" from the second list I think?What you do is for N words, form all binary numbers of N bits.  So for N=4:0000000100100011010001010110011110001001101010111100110111101111And then exclude 0000, 0001, 0010, 0100, 1000 (i.e. 0 and powers of two with only 1 bit set).  Then use each remaining number's bits to determine whether to add a word to the list:0011 = ran tom0101 = home tom0110 = home ran0111 = home ran tom1001 = fast tom1010 = fast ran1011 = fast ran tom1100 = fast home1101 = fast home tom1110 = fast home ran1111 = fast home ran tom Stephen Brooks2008-04-05 18:12:03 C code:`int n,i;char *words[]={"fast","home","ran","tom"}; int m=sizeof(words)/sizeof(*words);for (n=(1<0;n--) if (n&(n-1)){ for (i=m-1;i>=0;i--) if (n&(1<