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;
    }
  • 相关阅读:
    MyEclipse------文件字符输入,输出流读写信息
    MyEclipse------快速写入内容到指定目录下的文件(字节输出流)
    MyEclipse------快速读取特定目录下的文件的内容(字节输入流)
    MyEclipse------在特定目录创建文件和书写内容
    MyEclipse------遍历某个路径下的(所有或特定)文件和目录
    MyEclipse------File类的各种方法
    MyEclipse------如何在特定目录下创建文件夹
    MyEclipse------PreparedStatement使用方法
    使php支持mbstring库
    web页面性能测试
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3198455.html
Copyright © 2011-2022 走看看