Search series-ansible (full arrangement)

Source: Internet
Author: User

Ansible
Time Limit: 2000/1000 ms (Java/Other) Memory Limit: 20000/10000 K (Java/Other)
Total Submission (s): 16 Accepted Submission (s): 3
Problem 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 shocould-by grouping 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 shocould not produce the same word more than once, and the words shoshould 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 shoshould contain all different words that can be generated with the letters of the given word. the words generated from the same input word shoshould 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
 


Source
PKU


Again, the question is traced back to dfs. The question requires that all letters be arranged. It is said that C ++ has a function that can be implemented, but it is required to be arranged in alphabetical order, in addition, if the same letter is used, the upper case is better than the lower case. So I did it with pure C.
My idea is to construct two arrays of ch and val to store the values of letters and letters. The values of letters are converted to numerical values for storage. For example, a is 0.5, B is 1.5, and A is 0, B is 1 and then sorted from small to large. Then, the output in the pre-order traversal of dfs is the output required by the question.

However, after the compilation, we found a big problem. When there are duplicate letters, we will output a duplicate arrangement...
The Code is as follows (without AC ):

 

[Cpp]
# Include <stdio. h>
# Include <string. h>
 
Float val [13];
Int num, res [13];
Char ch [13];
 
Void dfs (int cur)
{
If (cur = num)
{
For (int I = 0; I <num; I ++)
Printf ("% c", ch [res [I]);
Printf ("\ n ");
Return;
}
For (int I = 0; I <num; I ++)
{
Int OK = 1;
Res [cur] = I;
For (int j = 0; j <cur; j ++)
If (res [j] = I)
{
OK = 0;
Break;
}
If (OK)
Dfs (cur + 1 );
}
Return;
}
 
Int main ()
{
Int n;
Num = 0;
Scanf ("% d", & n );
While (n --)
{
Num = 0;
Scanf ("% s", ch );
For (int I = 0; ch [I]! = '\ 0'; I ++)
{
Num ++;
If (ch [I]> = 'A' & ch [I] <= 'Z ')
Val [I] = ch [I]-'A ';
Else
Val [I] = ch [I]-'A' + 0.5;
}
For (int I = 0; I <num; I ++)
For (int j = I + 1; j <num; j ++)
If (val [I]> val [j])
{
Float e = val [I];
Val [I] = val [j];
Val [j] = e;
Char t = ch [I];
Ch [I] = ch [j];
Ch [j] = t;
}
Dfs (0 );
}
Return 0;
}

# Include <stdio. h>
# Include <string. h>

Float val [13];
Int num, res [13];
Char ch [13];

Void dfs (int cur)
{
If (cur = num)
{
For (int I = 0; I <num; I ++)
Printf ("% c", ch [res [I]);
Printf ("\ n ");
Return;
}
For (int I = 0; I <num; I ++)
{
Int OK = 1;
Res [cur] = I;
For (int j = 0; j <cur; j ++)
If (res [j] = I)
{
OK = 0;
Break;
}
If (OK)
Dfs (cur + 1 );
}
Return;
}

Int main ()
{
Int n;
Num = 0;
Scanf ("% d", & n );
While (n --)
{
Num = 0;
Scanf ("% s", ch );
For (int I = 0; ch [I]! = '\ 0'; I ++)
{
Num ++;
If (ch [I]> = 'A' & ch [I] <= 'Z ')
Val [I] = ch [I]-'A ';
Else
Val [I] = ch [I]-'A' + 0.5;
}
For (int I = 0; I <num; I ++)
For (int j = I + 1; j <num; j ++)
If (val [I]> val [j])
{
Float e = val [I];
Val [I] = val [j];
Val [j] = e;
Char t = ch [I];
Ch [I] = ch [j];
Ch [j] = t;
}
Dfs (0 );
}
Return 0;
}

 

 

 

As a result, the branches cannot be cut for a long time. I am too weak...

According to the solution Report on the internet, I found that many of them use the next_permutation function in stl in C ++ to find a dfs. After pruning, the submission times out, I thought that my manual sorting was slower than sort, and I had to change and time out. After several changes, I became more and more like other people's code. Then I simply pasted his code, which also timed out!

Haokeng!

The code after pruning is as follows (without AC ):


[Cpp]
# Include <stdio. h>
# Include <string. h>
 
Float val [13];
Int num, res [13], visited [13];
Char ch [13];
 
Void dfs (int cur)
{
If (cur = num)
{
For (int I = 0; I <num; I ++)
Printf ("% c", ch [res [I]);
Printf ("\ n ");
Return;
}
For (int I = 0; I <num; I ++)
If (! Visited [I])
{
Res [cur] = I;
Visited [I] = 1;
Dfs (cur + 1 );
Visited [I] = 0;
While (I + 1 <num & val [I] = val [I + 1])
I ++;
}
}
 
Int main ()
{
Int n;
Num = 0;
Scanf ("% d", & n );
While (n --)
{
Memset (visited, 0, sizeof (visited ));
Num = 0;
Scanf ("% s", ch );
For (int I = 0; ch [I]! = '\ 0'; I ++)
{
Num ++;
If (ch [I]> = 'A' & ch [I] <= 'Z ')
Val [I] = ch [I]-'A ';
Else
Val [I] = ch [I]-'A' + 0.5;
}
For (int I = 0; I <num; I ++) // sort
For (int j = I + 1; j <num; j ++)
If (val [I]> val [j])
{
Float e = val [I];
Val [I] = val [j];
Val [j] = e;
Char t = ch [I];
Ch [I] = ch [j];
Ch [j] = t;
}
Dfs (0 );
}
Return 0;
}

# Include <stdio. h>
# Include <string. h>

Float val [13];
Int num, res [13], visited [13];
Char ch [13];

Void dfs (int cur)
{
If (cur = num)
{
For (int I = 0; I <num; I ++)
Printf ("% c", ch [res [I]);
Printf ("\ n ");
Return;
}
For (int I = 0; I <num; I ++)
If (! Visited [I])
{
Res [cur] = I;
Visited [I] = 1;
Dfs (cur + 1 );
Visited [I] = 0;
While (I + 1 <num & val [I] = val [I + 1])
I ++;
}
}

Int main ()
{
Int n;
Num = 0;
Scanf ("% d", & n );
While (n --)
{
Memset (visited, 0, sizeof (visited ));
Num = 0;
Scanf ("% s", ch );
For (int I = 0; ch [I]! = '\ 0'; I ++)
{
Num ++;
If (ch [I]> = 'A' & ch [I] <= 'Z ')
Val [I] = ch [I]-'A ';
Else
Val [I] = ch [I]-'A' + 0.5;
}
For (int I = 0; I <num; I ++) // sort
For (int j = I + 1; j <num; j ++)
If (val [I]> val [j])
{
Float e = val [I];
Val [I] = val [j];
Val [j] = e;
Char t = ch [I];
Ch [I] = ch [j];
Ch [j] = t;
}
Dfs (0 );
}
Return 0;
}

 

 


I decided to use STL...

The Code is as follows ():


[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <string>
# Include <cstring>
Using namespace std;
 
Bool cmp (char a, char B)
{
Double t1 = a, t2 = B;
If (a> = 'A' & A <= 'Z ')
T1 ++ = 31.5;
If (B> = 'A' & B <= 'Z ')
T2 + = 31.5;
Return t1 <t2;
}
 
Int main ()
{
Int n, len;
Char str [15];
Cin> n;
While (n --)
{
Cin> str;
Len = strlen (str );
Sort (str, str + len, cmp );
Do
{
Cout <str <endl;
}
While (next_permutation (str, str + len, cmp ));
}
Return 0;
}

# Include <iostream>
# Include <algorithm>
# Include <string>
# Include <cstring>
Using namespace std;

Bool cmp (char a, char B)
{
Double t1 = a, t2 = B;
If (a> = 'A' & A <= 'Z ')
T1 ++ = 31.5;
If (B> = 'A' & B <= 'Z ')
T2 + = 31.5;
Return t1 <t2;
}

Int main ()
{
Int n, len;
Char str [15];
Cin> n;
While (n --)
{
Cin> str;
Len = strlen (str );
Sort (str, str + len, cmp );
Do
{
Cout <str <endl;
}
While (next_permutation (str, str + len, cmp ));
}
Return 0;
}

 

Related Article

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.