Orders
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8656 | Accepted: 5348 |
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) labelled with this letter. During the day the stores manager receives 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.
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
题目大意:字符串的非重复全排列;
解题方法:可以用递归和非递归两种方法解答:
非递归方法:
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; void Swap(char *a, char *b) { char temp = *a; *a = *b; *b = temp; } void Reserve(char *a, char *b) { while(a < b) { Swap(a++, b--); } } bool Next_Permutation(char *str) { int nLen = strlen(str); char *pEnd = str + nLen - 1; char *p = pEnd; char *q = pEnd; while(p != str) { q = p; p--; if (*p < *q) { char *pfind = pEnd; while(*pfind <= *p) { --pfind; } Swap(p, pfind); Reserve(q, pEnd); return true; } } return false; } int main() { char str[205]; scanf("%s", str); sort(str, str + strlen(str)); do { printf("%s ", str); } while (Next_Permutation(str)); return 0; }
超时递归代码:
#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace std; char Stack[205]; int nCount; int Used[205]; char result[205]; char str[205]; void PushStack(char ch) { for (int i = 0; i < nCount; i++) { if (ch == Stack[i]) { ++Used[i]; return; } } Stack[nCount] = ch; ++Used[nCount++]; } void DFS(int index) { int nLen = strlen(str); if (index == nLen) { for (int i = 0; i < nLen; i++) { printf("%c", result[i]); } printf(" "); return; } for (int i = 0; i < nCount; i++) { if (Used[i]) { --Used[i]; result[index] = Stack[i]; DFS(index + 1); ++Used[i]; } } } int main() { scanf("%s", str); sort(str, str + strlen(str)); memset(Used, 0, sizeof(Used)); for (int i = 0; i < strlen(str); i++) { PushStack(str[i]); } DFS(0); return 0; }