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;
    }
  • 相关阅读:
    真正明白了引用与对象的关系,就能避开下面这个陷阱
    python 垃圾回收
    字典
    表的操作
    MySQL数据库中的存储引擎
    MySQL数据库的基本操作
    MySQL数据库安装文件夹与配置文件简易说明
    数据库概述
    Arrays类
    Math类
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3198455.html
Copyright © 2011-2022 走看看