Rders
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 8100 Accepted: 5048
Description
The stores manager has sorted all kinds of goods in an alphabetical order of their labels. all the kinds having labels starting with the same letter are stored in the same warehouse (I. e. in the same building) labeled with this letter. during the day the stores manager has es and books the orders of goods which are to be delivered from the store. each order requires only one kind of goods. the stores manager processes the requests in the order of their booking.
You know in advance all the orders which will have to be processed by the stores manager today, but you do not know their booking order. compute all possible ways of the visits of warehouses for the stores manager to settle all the demands piece after piece during the day.
Input
Input contains a single line with all labels of the requested goods (in random order ). each kind of goods is represented by the starting letter of its label. only small letters of the English alphabet are used. the number of orders doesn't exceed 200.
Output
Output will contain all possible orderings in which the stores manager may visit his warehouses. every warehouse is represented by a single small letter of the English alphabet -- the starting letter of the label of the goods. each ordering of warehouses is written in the output file only once on a separate line and all the lines containing orderings have to be sorted in an alphabetical order (see the example ). no output will exceed 2 megabytes.
Sample Input
Bbjd
Sample Output
Bbdj
Bbjd
Bdbj
Bdjb
Bjbd
Bjdb
Dbbj
Dbjb
Djbb
Jbbd
Jbdb
Jdbb
Source
Question:
All the derived strings of the input string and output string are duplicated. the maximum length of the string is 200 characters in lowercase letters.
I am so depressed that writing times out. How can I put it into map? My idea is that putting it in map can prevent duplication. In fact, next_permutation won't generate a duplicate string at all.
It produces a string with a changing Lexicographic Order (this is a rising Lexicographic Order)
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <string>
# Include <algorithm>
# Include <iostream>
# Include <map>
Using namespace std;
Int main ()
{
Char s [300];
Int I, cnt;
Map <string, int> mp;
Map <string, int >:: iterator it;
While (scanf ("% s", s )! = EOF)
{
Mp. clear ();
Sort (s, s + strlen (s); // puts (s );
Cnt = strlen (s );
Do {
Mp [s] = 1;
} While (next_permutation (s, s + cnt ));
For (it = mp. begin (); it! = Mp. end (); it ++)
Cout <it-> first <endl; // note that some pop-up windows are not supported by printf.
}
Return 0;
}
Later it was changed to 32 ms.
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;
Int main ()
{
Char s [300];
Int I, cnt;
While (scanf ("% s", s )! = EOF)
{
Sort (s, s + strlen (s ));
Cnt = strlen (s );
Do {
Puts (s );
} While (next_permutation (s, s + cnt ));
}
Return 0;
}
Below is the DFS method and deduplication
[Cpp]
/* The deduplication method is: if the current flag of a is 1, it is used, and the previous a is not used, it jumps out.
Stored as a flag [I] = 1 & flag [I-1] = 0 & s [I] = s [I-1] & I> 0 s is the sorted string */
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <algorithm>
Using namespace std;
Int len;
Int flag [201];
Char str [201];
Char res [201];
Void dpoutput (int n)
{
Int I;
If (n = len)
{
Printf ("% s \ n", res );
Return;
}
For (I = 0; I <len; I ++)
{
If (flag [I] | (I> 0 & flag [I-1] = 0 & str [I] = str [I-1])
Continue;
Res [n] = str [I];
Flag [I] = 1;
Dpoutput (n + 1 );
Flag [I] = 0;
}
}
Int main ()
{
While (scanf ("% s", str )! = EOF)
{
Len = strlen (str );
Memset (flag, 0, sizeof (flag ));
Sort (str, str + len );
Dpoutput (0 );
}
Return 0;
}