zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    流行的编程思维_面向对象-1
    db_Driver Manager
    SpringAOP
    Spring ioc的核心源码及拓展
    linux内核中把自己写的驱动添加到make menuconfig
    如何参考am335x ti官方技术
    AM335X linux嵌入式平台can驱动移植
    linux AM335x嵌入式平台spi驱动调试
    linux 设备树 gpio获取
    linux 为开发板添加板级文件config.h
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3198455.html
Copyright © 2011-2022 走看看