• POJ 1256 Anagram


    Anagram
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 16533   Accepted: 6723

    Description

    You are to write a program that has to generate all possible words from a given set of letters. 
    Example: Given the word "abc", your program should - by exploring all different combination of the three letters - output the words "abc", "acb", "bac", "bca", "cab" and "cba". 
    In the word taken from the input file, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order. 

    Input

    The input consists of several words. The first line contains a number giving the number of words to follow. Each following line contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different. The length of each word is less than 13.

    Output

    For each word in the input, the output should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output in alphabetically ascending order. An upper case letter goes before the corresponding lower case letter.

    Sample Input

    3
    aAb
    abc
    acba
    

    Sample Output

    Aab
    Aba
    aAb
    abA
    bAa
    baA
    abc
    acb
    bac
    bca
    cab
    cba
    aabc
    aacb
    abac
    abca
    acab
    acba
    baac
    baca
    bcaa
    caab
    caba
    cbaa
    题目大意:对字符串进行全排列,除去重复项,字符的大小规则: 'A'<'a'<'B'<'b'<...<'Z'<'z'.
    #include <stdio.h>
    #include <iostream>
    #include <stack>
    #include <algorithm>
    #include <string.h>
    #include <vector>
    using namespace std;
    
    char str[15];
    
    bool cmp(char x, char y)
    {
        if (tolower(x) == tolower(y))
        {
            return x < y;
        }
        else
        {
            return tolower(x) < tolower(y);
        }
    }
    
    
    
    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 *pstr, int nLen)
    {
        if (nLen == 1)
        {
            return false;
        }
        char *pEnd = pstr + nLen - 1;
        char *p = pEnd;
        char *q = pEnd;
        while(p != pstr)
        {
            q = p;
            p--;
            if (cmp(*p, *q))
            {
                char *pfind = pEnd;
                while(cmp(*pfind, *p) || *pfind == *p)
                {
                    --pfind;
                }
                Swap(p, pfind);
                Reserve(q, pEnd);
                return true;
            }
        }
        Reserve(pstr, pEnd);
        return false;
    }
    
    int main()
    {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
        {
            scanf("%s", str);
            sort(str, str + strlen(str), cmp);
            do 
            {
                printf("%s
    ", str);
            } while (Next_Permutation(str, strlen(str)));
        }
        return 0;
    }
  • 相关阅读:
    Javascript的异步和回调
    JS-使用工厂方法创建对象
    PHPUnit使用教程——PHP环境变量+x-debug+composer+phpunit配置安装(超详细!)
    JQuery 纵向二级菜单与对齐方式
    图像映射<map>、<area>
    打开另一个窗口
    多行文本省略号
    replace 正则
    jquery each用法
    li前面的原点或者方的样式修改html中列表项li所显示的圆点的颜色?,以及相关样式的设定
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3198455.html
Copyright © 2020-2023  润新知