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;
    }
  • 相关阅读:
    Android实现文件上传功能(接收端用strust2)
    创建一个多页面,基于UINavigationController的控制的跳转的iOS应用程序
    NSURL中带有汉字参数的乱码处理
    Google Directions API通过请求获取导航数据简介
    iOS下用UIWebView打开文档
    关于Objectivec和Java下DES加密保持一致的方式
    struts2中通过Action以InputStream的下载文件以及在iOS以及Android端接收的实现方式
    NSURL中的参数带有特殊字符的处理
    UIColor设置自定义的颜色不成功问题
    Android中TabHost中实现标签的滚动以及一些TabHost开发的奇怪问题
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3198455.html
Copyright © 2011-2022 走看看