"abc" full arrange - II

来源:互联网 发布:数据库通配符 编辑:程序博客网 时间:2024/04/30 10:35

    学过数据结构与算法的人都知道,能用递归解决的问题都可以用回溯法解决,这篇文章就是针对上一篇("abc" full arrange - I : http://blog.csdn.net/ztz123123/article/details/47950091)中采用递归解决abc全排列问题的回溯解法。

    输入的数据依然是已排列且无重复元素的一串字符串。

    回溯所采用的思想其实依然是递归的思想,其只是在内存和运行上模拟递归的函数调用栈,采用前进和回退的控制机制,实现递归的仿制。

    对应于递归的处理函数,回溯的函数名为_b_arrange,其主体程序以while和switch为主,while负责无限的循环,switch则负责前进和回退的控制。在程序中,index为当前子字符串的头字符下标,b_i保存str字符串中的每个字符的下标,其含义为当前子字符串的下一个排列字符。例如:abc,index=0,当前排列字符为a,那么index=0的下一个排列字符应为b,即b_i[0]=1。假设当前index=0正在排列c,那么其已经不存在下一个排列字符,顾设置b_i[0]=-1表示当前子字符串下一排列不存在。

    同样对应于递归中的子字符串还原,该步骤被放到了回退的部分当中去。除此之外该部分还负责处理回溯出口判断,即当全排列全部完成的时候,负责结束函数。

    程序源文件为main.c,b.arrange.c,b.arrange.h以及makefile


    main.c

/********************************************************************************                                                                           **** --------------------------- file information ---------------------------- **** @file     : main.c                                                        **** @created  : Flee Elf - QQ 892737105                                       **** @date_crt : 2015/08/21                                                    **** @modified : Flee Elf                                                      **** @date_mod : 2015/08/24                                                    **** @version  : v1.1.0                                                        **** @brief    : main program of "abc" full arrange project                    **** ------------------------------------------------------------------------- ****                                                                           ********************************************************************************/#include <stdio.h>#include "b.arrange.h"/********************************************************************************                                                                           ****                           Interface Definition                            ****                                                                           ********************************************************************************//** @Interface: main ** @created  : Flee Elf ** @brief_crt: used in shell to get full arrange result ** @modified : Flee ELf ** @brief_mod: use backtracking instead of recursion ** @param    : argc - param count ** @param    : argv - param string array ** @retval   : int  - exit code number **/int main(int argc, char *argv[]){    if (argc != 2)    {        fprintf(stderr, "usage : %s char_string\n", argv[0]);        return 0;    }    b_arrange(argv[1]);    return 0;}

    b.arrange.c

/********************************************************************************                                                                           **** --------------------------- file information ---------------------------- **** @file     : b.arrange.c                                                   **** @created  : Flee Elf - QQ 892737105                                       **** @date_crt : 2015/08/24                                                    **** @modified :                                                               **** @date_mod :                                                               **** @version  : v1.1.0                                                        **** @brief    : "abc" full arrange in backtracking                            **** ------------------------------------------------------------------------- ****                                                                           ********************************************************************************/#include <stdio.h>#include <string.h>#include <stdlib.h>#include "b.arrange.h"/********************************************************************************                                                                           ****                        Inside Variable Definition                         ****                                                                           ********************************************************************************/static char *str;                       /* point to a copy of src-string 'p' */static int   len;                       /* count of char of 'p' without '\0' */static int  *b_i;                       /* index of each backtracking        *//********************************************************************************                                                                           ****                            Function Definition                            ****                                                                           ********************************************************************************//********************************************************************************                                                                           ****    Note:                                                                  ****    > there is a example below shows the process in _b_arrange             ****        str = "abc" = {'a', 'b', 'c', '\0'};                               ****        b_i =         { 0 ,  x ,  x };                                     ****        len = 3;                                                           ****                                                                           ****        index_last = 2;                                                    ****                                                                           ****        ---------------------------------------------------------------    ****                                                                           ****        |index|mode| i |  str  |   b_i   |                                 ****                                                                           ****        |             start              |                                 ****        |  0  | f  | x | a b c | 0  x  x |                                 ****        +-----+----+---+-------+---------+                                 ****        |  1  | f  | 0 | a b c | 1  1  x |                                 ****        |  2  | f  | 1 | a b c | 1  2  2 |                                 ****        |  2  | b  | 1 | a b c | 1  2  2 | puts abc                        ****        |  1  | f  | 1 | a b c | 1  2  2 |                                 ****        +-----+----+---+-------+---------+                                 ****        |  2  | f  | 2 | a c b | 1 -1  2 |                                 ****        |  2  | b  | 2 | a c b | 1 -1  2 | puts acb                        ****        |  1  | b  | 2 | a c b | 1 -1  2 |                                 ****        +-----+----+---+-------+---------+                                 ****        |  0  | f  | 2 | a b c | 1 -1  2 |                                 ****        +-----+----+---+-------+---------+                                 ****        |  1  | f  | 1 | b a c | 2  1  2 |                                 ****        |  2  | f  | 1 | b a c | 2  2  2 |                                 ****        |  2  | b  | 1 | b a c | 2  2  2 | puts bac                        ****        |  1  | f  | 1 | b a c | 2  2  2 |                                 ****        +-----+----+---+-------+---------+                                 ****        |  2  | f  | 2 | b c a | 2 -1  2 |                                 ****        |  2  | b  | 2 | b c a | 2 -1  2 | puts bca                        ****        |  1  | b  | 2 | b c a | 2 -1  2 |                                 ****        +-----+----+---+-------+---------+                                 ****        |  0  | f  | 2 | b a c | 2 -1  2 |                                 ****        +-----+----+---+-------+---------+                                 ****        |  1  | f  | 2 | c a b |-1  1  2 |                                 ****        |  2  | f  | 1 | c a b |-1  2  2 |                                 ****        |  2  | b  | 1 | c a b |-1  2  2 | puts cab                        ****        |  1  | f  | 1 | c a b |-1  2  2 |                                 ****        +-----+----+---+-------+---------+                                 ****        |  2  | f  | 2 | c b a |-1 -1  2 |                                 ****        |  2  | b  | 2 | c b a |-1 -1  2 | puts cba                        ****        |  1  | b  | 2 | c b a |-1 -1  2 |                                 ****        +-----+----+---+-------+---------+                                 ****        |  0  | b  | 2 | c a b |-1 -1  2 |                                 ****        |              end               |                                 ****                                                                           ****        ---------------------------------------------------------------    ****                                                                           ********************************************************************************//** @Function : _b_arrange ** @created  : Flee Elf ** @brief_crt: get full arrange in backtracking ** @modified : ** @brief_mod: ** @param    : None ** @retval   : None **/static void _b_arrange(void){    int index_last = len - 1;    int index      = 0;                 /* current child-string-head's index */    int i;    char mode = 'f';                    /* 'f' front 'b' back                */    char tmp;    while (1)    {        switch (mode)        {        case 'f':                       /* like call function in recursion   */            if (index == index_last)    /* one char one arrange, show result */            {                puts(str);                mode = 'b';                break;            }            /* get current arrange index & update to next arrange index      */            b_i[index] = ((i = b_i[index]) == index_last) ? -1 : i + 1;            tmp = str[i];               /* set child-string ready            */            str[i] = str[index];            str[index] = tmp;            index++;                    /* go next                           */            b_i[index] = index;         /* set child-string entrance         */            break;        case 'b':                       /* like return or } in recursion     */            if (index == 0)             /* current child-str-head's index = 0*/            {                return ;                /* next backtracking is -1 means end */            }            tmp = str[index];           /* recover child-string in sorted    */            for (i = index; i < index_last; i++)            {                str[i] = str[i + 1];            }            str[index_last] = tmp;            /* get back index & judge mode 'f' or 'b'                        */            mode = (b_i[--index] == -1) ? 'b' : 'f';            break;        default :            fprintf(stderr, "backtracking unknown error !\n");            return;        }    }}/********************************************************************************                                                                           ****                           Interface Definition                            ****                                                                           ********************************************************************************//** @Interface: b_arrange ** @created  : Flee Elf ** @brief_crt: show full arrange of string 'p' in backtracking ** @modified : ** @brief_mod: ** @param    : p - point to a string & each iterm is unique & been sorted ** @retval   : None **/void b_arrange(char *p){    int i;    if ((len = strlen(p)) == 0)    {        fprintf(stderr, "char string length is 0 !\n");        return ;    }    if ((str = malloc(sizeof(char) * (len + 1))) == NULL)    {        fprintf(stderr, "malloc string fault !\n");        return ;    }    if ((b_i = malloc(sizeof(int) * len)) == NULL)    {        fprintf(stderr, "malloc index array fault !\n");        free(str);                      /* it's necessary                    */        return ;    }    strcpy(str, p);                     /* we sure that str is big enough    */    b_i[0] = 0;                         /* initialise entrance               */    _b_arrange();    free(str);    free(b_i);}

    b.arrange.h

/********************************************************************************                                                                           **** --------------------------- file information ---------------------------- **** @file     : b.arrange.h                                                   **** @created  : Flee Elf - QQ 892737105                                       **** @date_crt : 2015/08/24                                                    **** @modified :                                                               **** @date_mod :                                                               **** @version  : v1.1.0                                                        **** @brief    : interface of "abc" full arrange in backtracking               **** ------------------------------------------------------------------------- ****                                                                           ********************************************************************************/#ifndef __B_ARRANGE_H__#define __B_ARRANGE_H__#ifdef __cplusplus    extern "C" {#endif/********************************************************************************                                                                           ****                           Interface Declaration                           ****                                                                           ********************************************************************************/extern void b_arrange(char *p);#ifdef __cplusplus    }#endif#endif

    makefile

# <configuration> ##CROSS=arm-linux-#OBJ_1=abc.arm.outCROSS=OBJ_1=abc.pc.out# </configuration> ## <all> #.PHONY:allall:image_1# </all> ## <image_1> #MAIN=main.cSRC=b.arrange.cimage_1:$(CROSS)gcc -o $(OBJ_1) $(MAIN) $(SRC)# </image_1> ## <clean> #.PHONY:cleanclean:clean_1# </clean> ## <clean_1> #clean_1:rm $(OBJ_1)# </clean_1> #<span style="font-family:Courier New;"></span>


    下图是程序在centos6.6的valgrind中运行的结果截图。


图1   程序截图


    代码规范:C语音编程规范-参考

0 0
原创粉丝点击