STL-Anagram

source

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

Hint

An upper case letter goes before the corresponding lower case letter.
So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'.

主要思路:因为'A'<'a''<...<'Z'<'z',所以将字母的大小值映射到一个double值上,比如,A=96.5,a=97;B=97.5,b=98...然后循环用next_permutation(val,val+len)即可

#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char trans(double val)
{

    if((double)((int)val)==val) return (int)val;
    else return (int)(val-31.5);
}
int main()
{
int n;
cin>>n;
char *a=new char[14];
double *val;
while(n--)
{
    cin>>a;
    int len=strlen(a);
    val=new double[len];
    for(int i=0;i<len;i++)
    {
        if(('A'<=a[i])&&(a[i]<='Z')) val[i]=(int)a[i]+31.5;
        else val[i]=(int)a[i]+0.0;

    }
    sort(val,val+len);
     do {
    for(int i=0;i<len;i++)
        cout<<trans(val[i]);
    cout<<endl;
    } while (next_permutation(val,val+len));
}

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,601评论 0 23
  • 早上醒来,被朋友发来的一个信息给震惊了,除夕夜B站被攻击。 选择在除夕夜爆出这个事情,想必是提前已经规划好了,要造...
    cool_wier阅读 8,561评论 6 37
  • 《讨鬼传极》攻铳伐罪常夜女王与极级真渊视频 伐罪关于象:常夜女王 武器:创世铳+9 NPC :速鸟 大和 樱花 赛...
    光明神话阅读 4,865评论 0 0
  • 很久很久以前,我们的女王还是一个小公主。 她喜欢穿着粉红色的公主裙,头上戴着精致的白色蕾丝头纱,手里拿着棒棒糖蹦蹦...
    尘北阅读 3,008评论 0 0
  • 我张开手, 轻轻虚握。 想象你稚嫩的小手, 与我掌纹相合。 无尽的夜, 吞没虚拟的场景, 慢慢消融成, 刻骨铭心的...
    紫蝶冰魄阅读 1,405评论 0 0

友情链接更多精彩内容