Results 1 to 6 of 6

Thread: How to show combinations of strings

  1. #1
    Join Date
    Aug 2009
    Posts
    59

    How to show combinations of strings

    I wanted to show all possible combinations of size 2, size 3 to size N
    For example there are 4 words (string):
    Code:
    one 
    two 
    three 
    four
    combinations are possible:
    Code:
    one two 
    one three 
    one four 
    two three 
    two four 
    three four 
    one two three 
    one two four 
    one three four 
    two three four 
    one two three four
    So we have N = 4 and the number of combinations = 11. It is not interested in forming a combination with 1 word (one, two, three and four).

    Here's a solution:
    Code:
    #include <stdio.h>
    #define N 5
    void display(int state[], char *t[])
    {
      int i;
      for (i = 0; i < N; i++)
        if (state[i])
          printf("%s ", t[i]);
      puts("" );
    }
    void part(int h, int state[], char *t[])
    {
      enum { ABSENT, PRESENT };
      if (h < 0)
        display(state, t);
      else
        {
          state[h] = ABSENT;
          part(h - 1, state, t);
          state[h] = PRESENT;
          part(h - 1, state, t);
        }
    }
    int main(void)
    {
      char *t[N] = { "name", "surname", "age", "address", "employment" };
      int state[N];
      part(N - 1, state, t);
      return 0;
    }
    it displays:
    name
    surname
    name surname
    age
    name age
    surname age
    name surname age
    address
    name address
    surname address
    name surname address
    age address
    name age address
    surname age address
    name surname age address
    employment
    name employment
    surname employment
    name surname employment
    age employment
    name age employment
    surname age employment
    name surname age employment
    address employment
    name address employment
    surname address employment
    name surname address employment
    age address employment
    name age address employment
    surname age address employment
    name surname age address employment

    How to modify the solution to get the combinations from size 2 and is also sorted (size 2 then 3 then 4 and 5)?


    The desired result is obtained:
    name surname
    name age
    name address
    name employment
    surname age
    surname address
    surname employment
    age address
    age employment
    address employment
    name surname age
    name surname address
    name surname employment
    name age address
    name age employment
    name address employment
    surname age address
    surname age employment
    surname address employment
    age address employment
    name surname age address
    name surname age employment
    name surname address employment
    name age address employment
    surname age address employment
    name surname age address employment
    Can you help me?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: How to show combinations of strings

    I found a solution that allows this, which is also based on a status table. But I made a function

    displayAllPossibilites void (char * t [N], unsigned nbAff)

    I call in the main
    Code:
    for (i=2;i<=N;i++)
           displayAllPossibilites(t,i);
    This function displays all the possible combinations of nbAff words. So the display is sorted.

    It makes the state the first word to 1 and then calls a recursive function that takes care to all possibilities with the words that remain. Then it passes the 2nd word and so on.

    I do not know if I am clear, but I hope to have you at least have a track.

  3. #3
    Join Date
    Aug 2009
    Posts
    59

    Re: How to show combinations of strings

    Please can you give me details of your solution yet because I do not understand? It is possible to understand the solution better because I need to post the rest of my work?

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: How to show combinations of strings

    No I cannot give the solution because this is not the custom here. I suppose you store your results and want to sort. It is a solution, especially if you have to reuse your results later. My solution is simply to show that if you want you can always store rather than display. But in this case, perhaps a sort is more efficient, I have not compared.

  5. #5
    Join Date
    Nov 2008
    Posts
    1,022

    Re: How to show combinations of strings

    If I were you, I will use the method of binary masks to validate/inhibit value and access the following. A simple table you can then return the names of values that match. For example: pseudo C code:

    Code:
    enum
    {    name              1<<0;
         surname         1<<1;
         address        1<<2;
    }
    You can then use the operators corresponding bits to express the full results. If you have the doc on the C: |, &, ^ and ~.

    I have not much time to explain more if you feel comfortable with these operators, I think it can go fast enough.

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: How to show combinations of strings

    It is an idea that he should dig, even if nothing comes to my mind like that, there might be ways to do something.

    The problem is if N = 256815, you will never have enough bits. Mine solution works, even if it probably would dynamically create tables, rather than return N words in hand with the statement.

    Well OK, human civilization will disappear until all possibilities are displayed but that is not the issue ....

Similar Threads

  1. Replies: 10
    Last Post: 06-10-2011, 06:19 PM
  2. Replies: 5
    Last Post: 12-05-2011, 11:50 AM
  3. Key Combinations in Linux based operating system
    By Cruzz in forum Operating Systems
    Replies: 5
    Last Post: 17-01-2010, 05:41 AM
  4. impressive combinations between hardware and softwares
    By genuinethief in forum Polls & Voting
    Replies: 2
    Last Post: 08-01-2010, 10:53 AM
  5. Key Combinations For The X-Windows server
    By devpat in forum Operating Systems
    Replies: 3
    Last Post: 19-02-2009, 01:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,598,346.96738 seconds with 16 queries