Anagram
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13959 | Accepted: 5693 |
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.
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'.
So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'.
Source
Southwestern European Regional Contest 1995
这道题真是很经典的一道题,刚开始OLE了两次,就是找不到原因,后来一看输入AAB的时候竟然出现了重复(如图),后来一看,是因为输入了重复的字符的原因,就加上了个标记,AC
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int len,num[15];
struct point{
char s;
int index;
}str[15];
char name[15];
int cmp(const void *a,const void *b)
{
struct point *c,*d;
c=(struct point *)a;
d=(struct point *)b;
return c->index-d->index;
}
void DFS(int step)
{
int i;
char flag;//标记
if(step==len){
name[step]=0;
puts(name);
return ;
}
for(i=0;i<len;i++)
{
if(flag==str[i].s)continue;//如果该字符出现过,则不再全排列
if(num[i]==0)
{
num[i]=1;
name[step]=str[i].s;
flag=name[step];//记下出现过的字符
DFS(step+1);
num[i]=0;
}
}
}
int main()
{
int n,i;
char kk[15];
scanf("%d",&n);
while(n--)
{
scanf("%s",kk);
for(i=0;kk[i];i++)
{
str[i].s=kk[i];
if(kk[i]>='a'&&kk[i]<='z')
str[i].index=(kk[i]-'a')*2+1;
else str[i].index=(kk[i]-'A')*2;
}
len=strlen(kk);
for(i=0;i<len;i++)
num[i]=0;
qsort(str,len,sizeof(str[0]),cmp);
DFS(0);
}
return 0;
}
这道题真是很经典的一道题,刚开始OLE了两次,就是找不到原因,后来一看输入AAB的时候竟然出现了重复(如图),后来一看,是因为输入了重复的字符的原因,就加上了个标记,AC
代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int len,num[15];
struct point{
}str[15];
char name[15];
int cmp(const void *a,const void *b)
{
}
void DFS(int step)
{
}
int main()
{
}