UVA 195 Anagram

Source: Internet
Author: User
Tags lowercase strlen

Original question:
You is to write a program, which has the 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 is appear more than once. For a given word, your program should not produce the same word more than once, and the words should is output in alphabet ically ascending order.

Input

The input file 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 is to be considered differ Ent.

Output

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

Sample Input

3
AAb
Abc
Acba
Sample Output

Aab
Aba
AAb
AbA
BAa
AaB
Abc
Acb
Bac
Bca
Cab
Cba
Aabc
Aacb
Abac
Abca
Acab
Acba
Baac
Baca
Bcaa
Caab
Caba
Cbaa

English:
Give you a string, composed of uppercase and lowercase letters, with duplicate elements that now let you output all the string permutations. AABB by string size ..... The order of the ZZ is given.

#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
int mark[140];
Vector<string> vs; int CMP (const char &c1,const char &c2) {return mark[c1]<mark[c2];} void permutation (int n,char *p,char *a
        , int cur) {if (cur==n) {string tmp;
        Tmp.assign (A,n);
    Vs.push_back (TMP); } else {for (int i=0;i<n;i++) {if (!i| |
                P[i]!=p[i-1]) {int c1=0,c2=0;
                for (int j=0;j<cur;j++) if (a[j]==p[i]) c1++;
                for (int j=0;j<n;j++) if (p[i]==p[j]) c2++;
                    if (C1&LT;C2) {a[cur]=p[i];
                Permutation (n,p,a,cur+1);
    }}}}} int main () {Ios::sync_with_stdio (false);
    memset (Mark,0,sizeof (Mark)); for (int i= ' A ', j= ' a ', k=0;i<= ' z ' &&j<= ' z '; i++,j++,k+=2) {mark[i]=k;
    mark[j]=k+1;
    } Char s[100];
    Char p[100];
    int t;
    cin>>t;
        while (t--) {cin>>p;
        Vs.clear ();
        Sort (P,p+strlen (p), CMP);
        Permutation (strlen (p), p,s,0);
    for (int i=0;i<vs.size (); i++) cout<<vs[i]<<endl;
} return 0;
 }

Answer:
First write a sort of function, the given string according to the rules of order, using purple Book can be re-set template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.