ACM题目————Anagram

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‘.

STL的全排列问题,写好排序方案,直接调用 next_permutation()函数便可!

#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

int n;
char str[15];

bool cmp(char a,char b)
{
    if(tolower(a)==tolower(b))
        return a<b;
    else
        return tolower(a)<tolower(b);
}

int main()
{
    scanf("%d",&T);
    while( T-- )
    {
        scanf("%s",str);
        sort(str,str+strlen(str),cmp);//自定义排序
        do
        {
            cout << str << endl ;
        }while(next_permutation(str,str+strlen(str),cmp));
    }

    return 0;
}
时间: 2024-08-01 21:54:53

ACM题目————Anagram的相关文章

杭电ACM题目分类

杭电ACM题目分类 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028. 1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092. 1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196. 1197.1201.1202.1205.1219.1234.123

超强的ACM题目类型总结

转:初期: 一.基本算法:       (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.       (4)递推.       (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:       (1)图的深度优先遍历和广度优先遍历.       (2)最短路径算法(dijkstra,bellman-

我的ACM题目测试数据产生方法

到网上搜了下ACM题目产生方法,却没有搜索到有用的资料,那只能自己钻研一下了,下面把钻研出的结果给大家分享一下. 我们知道,ACM的题目对算法要求很高,OJ上而如何评判一个算法的复杂度靠的是测试数据.一般地,题目需要数据量比较大,不可能人工地去产生,只能借助计算机产生测试数据,而为了避免数据出现规律性,那应该尽量让数据随机.接下来,我们就研究最一般的问题,如何产生int,long long ,大整数,以及字符串? 我们知道,C语言提供了我们一个随机函数rand(),可惜的是它只能产生(1~2^1

ACM题目-Who&#39;s in the Middle

#这题主要用到了sort函数去简化排序的过程(需要声明#include <algorithm>) 描述 FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median' cow gives: half of the cows give as much or more than the median; half give as much or less. Give

HDOJ ACM题目分类

模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 120

有一种acm题目叫做,奇葩!

本文全然没有技术含量,纯粹是娱乐. 我事实上想写点东西.可是近期好像做计算几何做得太多了,一种想说说不出东西的感觉,唯有写一下一些奇葩的题目了. HDU3337:Guess the number pid=3337"> http://acm.hdu.edu.cn/showproblem.php? pid=3337 题目意思: 猜数字!(32位整数范围内) 正确答案: <span style="font-size:18px;"> #include "s

写给一心刷ACM题目的学生

[来信] 老师你好,我大2013级大二的学生.大一时自学了数据结构,大二的时候参加了ACM. 现在寒假在家里刷题.可是我还想学习些东西,比如深入C 的学习,或者java.但是感觉除了刷题之外,就很迷茫,不知道以后出去工作是C 好还是java好.其实我更喜欢C ,但是如何去深入学习C 呢,我对C 的深入学习也是很迷茫的,因为对于C/C 除了用来刷题,我都不知道他们能够干什么? [回复] 你没有说你的专业,我猜想应该是计算机类的专业.下面就基于这个前提,谈谈我的想法. 很高兴你能找到一个提高编程能力

ACM 题目分类

转自: http://www.cnblogs.com/ltang/articles/1861284.html#top OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739 ,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期:一.基本算法:     (1)枚举. (poj1753,poj2965)    (2)贪心(poj1328,poj2109,poj2586)    (3)递归和分治法.     (4)递

推荐acm题目

杭电  http://acm.hdu.edu.cn/onlineuser.php. 浙大  http://acm.zju.edu.cn/onlinejudge/submit.do?problemId=1http://poj.org/status惟一的阿福 password:14420121 263273 14420121 学校原来网站      http://10.1.5.253:8080/acmhome/welcome.do?method=index