字符组合排序

作者: Albert_Sun | 来源:发表于2016-09-14 11:20 被阅读52次

    1.将给定字符组合后,按字典序排序输出,示例:
    输入:a, b, c;
    输出:abc,acb,bac,bca,cab,cba
    2.将给定字符组合后,按字典序排序输出,示例:
    输入:a, b, c;
    输出:a,b,c,ab,ac,bc, abc

    //Example 1
    #include <stdio.h>
    #include <string.h>
    
    void combchar(char* str, int from, int to)
    {
        if(from >= to){
            for (int i = 0; i <= to; ++i){
                PRINT("%c", str[i]);
            }
            PRINT(" ");
            return;
        }
    
        int j = 0;
        for(int i = from; i <= to; i++){
            char tmp = str[i];
            for(j = i; j-1 >= from; --j){
                str[j] = str[j-1];
            }
            str[j] = tmp;
    
            combchar(str, from+1, to);
    
            for(j = from; j+1 <= i; ++j){
                str[j] = str[j+1];
            }
            str[j] = tmp;
    
            if(from == 0) PRINT("\n");
        }
    }
    
    int main(){
        char str[] = {'a', 'b', 'c', 'd'};
        combchar(str, 0, sizeof(str)-1);
        return 0;
    }
    

    Example 1 Result 结果如下所示:

    Example 1 Result:
    abcd abdc acbd acdb adbc adcb 
    bacd badc bcad bcda bdac bdca 
    cabd cadb cbad cbda cdab cdba 
    dabc dacb dbac dbca dcab dcba
    
    //Example 2
    #include <stdio.h>
    #include <string.h>
    #define PRINT   printf
    int num = 0;
    void combination(char* str, int from, int to, int len, int t)
    {
        if(len == 1){
            PRINT("%c\n", str[from]); ++num;
            for(int j = from+1; j <= to; j++){
                PRINT("%*c\n", 3*t+1, str[j]);
                ++num;
            }
            return;
        }
        for(int j = from; j <= to-len+1; j++){//1,2,3,4
            PRINT("%*c->", (j==from) ? 1 : 3*t+1, str[j]);
            combination(str, j+1, to, len-1, t+1);
        }
    }
    
    void combination2(char* str, int from, int to, int len, int t, char* tmpStr)
    {
        if(len == 1){
            for(int j = from; j <= to; j++){
                tmpStr[t] = str[j];
                for(int i = 0; i <= t; i++){
                    PRINT("%c", tmpStr[i]);
                }
                PRINT(" ");
            }
        }else{
            for(int j = from; j <= to-len+1; j++){//1,2,3,4
                tmpStr[t] = str[j];
                combination2(str, j+1, to, len-1, t+1, tmpStr);
    
            }
        }
        if(t == 0) PRINT("\n");
    }
    
    void findSubStr(char* str, int from, int to)
    {
        char tmpStr[from-to+1];
        for(int j = 1; j <= to-from+1; j++){//len=1,2,3,4
            //combination(str, from, to, j, 0);
            combination2(str, j+1, to, len-1, t+1, tmpStr);
            PRINT("-------------------%d|%d\n", j, num);
        }
    }
    
    int main(){
        char str[] = {'a', 'b', 'c', 'd'};
        int size = strlen(str);
        findSubStr(str, 0, size-1);
        return 0;
    }
    

    Example 2 Result 结果如下所示:

    Example 2 combination Result:
    a
    b
    c
    d
    a->b
       c
       d
    b->c
       d
    c->d
    a->b->c
          d
       c->d
    b->c->d
    a->b->c->d
    
    Example 2 combination2 Result
    a b c d 
    ab ac ad bc bd cd 
    abc abd acd bcd 
    abcd 
    

    相关文章

      网友评论

        本文标题:字符组合排序

        本文链接:https://www.haomeiwen.com/subject/lysvettx.html