• POJ 1731 Orders


    Orders
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 8656   Accepted: 5348

    Description

    The stores manager has sorted all kinds of goods in an alphabetical order of their labels. All the kinds having labels starting with the same letter are stored in the same warehouse (i.e. in the same building) labelled with this letter. During the day the stores manager receives and books the orders of goods which are to be delivered from the store. Each order requires only one kind of goods. The stores manager processes the requests in the order of their booking. 

    You know in advance all the orders which will have to be processed by the stores manager today, but you do not know their booking order. Compute all possible ways of the visits of warehouses for the stores manager to settle all the demands piece after piece during the day. 

    Input

    Input contains a single line with all labels of the requested goods (in random order). Each kind of goods is represented by the starting letter of its label. Only small letters of the English alphabet are used. The number of orders doesn't exceed 200. 

    Output

    Output will contain all possible orderings in which the stores manager may visit his warehouses. Every warehouse is represented by a single small letter of the English alphabet -- the starting letter of the label of the goods. Each ordering of warehouses is written in the output file only once on a separate line and all the lines containing orderings have to be sorted in an alphabetical order (see the example). No output will exceed 2 megabytes. 

    Sample Input

    bbjd
    

    Sample Output

    bbdj
    bbjd
    bdbj
    bdjb
    bjbd
    bjdb
    dbbj
    dbjb
    djbb
    jbbd
    jbdb
    jdbb
    题目大意:字符串的非重复全排列;
    解题方法:可以用递归和非递归两种方法解答:
    非递归方法:
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    
    void Swap(char *a, char *b)
    {
        char temp = *a;
        *a = *b;
        *b = temp;
    }
    
    void Reserve(char *a, char *b)
    {
        while(a < b)
        {
            Swap(a++, b--);
        }
    }
    
    bool Next_Permutation(char *str)
    {
        int nLen = strlen(str);
        char *pEnd = str + nLen - 1;
        char *p = pEnd;
        char *q = pEnd;
        while(p != str)
        {
            q = p;
            p--;
            if (*p < *q)
            {
                char *pfind = pEnd;
                while(*pfind <= *p)
                {
                    --pfind;
                }
                Swap(p, pfind);
                Reserve(q, pEnd);
                return true;
            }
        }
        return false;
    }
    
    int main()
    {
        char str[205];
        scanf("%s", str);
        sort(str, str + strlen(str));
        do 
        {
            printf("%s
    ", str);
        } while (Next_Permutation(str));
        return 0;
    }

    超时递归代码:

    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    
    char Stack[205];
    int nCount;
    int Used[205];
    char result[205];
    char str[205];
    
    void PushStack(char ch)
    {
        for (int i = 0; i < nCount; i++)
        {
            if (ch == Stack[i])
            {
                ++Used[i];
                return;
            }
        }
        Stack[nCount] = ch;
        ++Used[nCount++];
    }
    
    void DFS(int index)
    {
        int nLen = strlen(str);
        if (index == nLen)
        {
            for (int i = 0; i < nLen; i++)
            {
                printf("%c", result[i]);
            }
            printf("
    ");
            return;
        }
        for (int i = 0; i < nCount; i++)
        {
            if (Used[i])
            {
                --Used[i];
                result[index] = Stack[i];
                DFS(index + 1);
                ++Used[i];
            }
        }
    }
    
    int main()
    {
        scanf("%s", str);
        sort(str, str + strlen(str));
        memset(Used, 0, sizeof(Used));
        for (int i = 0; i < strlen(str); i++)
        {
            PushStack(str[i]);
        }
        DFS(0);
        return 0;
    }
  • 相关阅读:
    day2流程控制
    day1初识java
    SVG路径PATH
    Android开发 ---Media
    Android开发 ---ContentProvider数据提供者,Activity和Service就是上下文对象,短信监听器,内容观察者
    Android开发 ---ORMLite实现数据的增删改查,单例模式,Dao栈
    Android开发 ---SQLite数据库,lock文件,结果集游标,适配器,安全退出,给连接设置下划线,编辑器,投影,ContentValues存储,DbHelper,activity栈
    Android开发 ---多线程操作:Handler对象,消息队列,异步任务下载
    深入理解之 Android Handler
    Android开发 ---xml构建选项菜单、上下文菜单(长按显示菜单)、发通知、发送下载通知
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3202178.html
Copyright © 2020-2023  润新知