about string combination

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AsuWoo

    about string combination

    hi,
    I want to implement a function that prints all possible combinations of a
    characters in a string,eg. input "123"into a textbox,
    add "1","2","3","12 ","13","23","12 3",to a listbox,Or "ab" into a textbox,add
    "a","b","ab "in a listbox,could anyone help me out ?


  • smartnose

    #2
    Re: about string combination

    A long lost art of programming. £º£©

    You may design this algorithm in two step:
    1. get all the combinations of chars in your string.
    2. for every combination, compute its possible arragements.

    like "123" in first step, we get
    "1" "2" "3" "12" "23" "13" "123"

    the second step, we try to rearrange every terms, so we have:
    "1" "2" "3" "12" "21" "23" "32" "13" "31" "132" "123" "312"....

    The algorithm to choose combinations:
    for each string, we define a string of bits with the same length of it. if
    one char of the string is to be choosed, then the corresponding bit is set
    to 1, else 0. so you can see ,every possible combinations correspond to a
    binary number. now it's easy to understand the seudo codes below:
    Combination(cha r * s)
    {
    int t=slen(s);//get its length.
    for(int i=1;i<exp(2,t-1);i++)//searching all possible combinations
    {
    string trs="";//empty it
    for(int j=0;j<t;j++)
    if(i&exp(2,j)== 1)//find chars with corresponding bit set to 1
    trs+=s[j];//add the char into the set
    RearrangeAndOut put(trs);
    }
    }

    You may use recurse algorithm to rearrange the set. I think it's easy to
    copy one from your text book.

    The algorithm is very slow, you'd better redesign it using bit operations
    and check for duplicated results in case you input string like "112".


    "AsuWoo" <wucyivy@msn.co m> дÈëÓʼþ
    news:ODZW3AopDH A.2140@TK2MSFTN GP09.phx.gbl...[color=blue]
    > hi,
    > I want to implement a function that prints all possible combinations of a
    > characters in a string,eg. input "123"into a textbox,
    > add "1","2","3","12 ","13","23","12 3",to a listbox,Or "ab" into a[/color]
    textbox,add[color=blue]
    > "a","b","ab "in a listbox,could anyone help me out ?
    >
    >[/color]


    Comment

    • AsuWoo

      #3
      Re: about string combination

      thank you!

      but two combinations that differ only in ordering of their characters are
      the same combination. in other words "12" is the same as "21",

      and another newbie question how to put characters of a string into a string
      array?
      is there any String function?


      Comment

      • Fred Mellender

        #4
        Re: about string combination

        Consulting http://www.freevbcode.com/ShowCode.Asp?ID=2221 might help.

        This sort of problem can be researched via Google, with the query "generate
        all combinations". Any good book on combinatorial algorithms will also
        contain the information.


        "AsuWoo" <wucyivy@msn.co m> wrote in message
        news:ODZW3AopDH A.2140@TK2MSFTN GP09.phx.gbl...[color=blue]
        > hi,
        > I want to implement a function that prints all possible combinations of a
        > characters in a string,eg. input "123"into a textbox,
        > add "1","2","3","12 ","13","23","12 3",to a listbox,Or "ab" into a[/color]
        textbox,add[color=blue]
        > "a","b","ab "in a listbox,could anyone help me out ?
        >
        >[/color]


        Comment

        Working...