已知一个有重复字符的字符串,打印其所有不同的字符排列
2016-10-17 13:50
603 查看
原文地址:Print all distinct permutations of a given string with duplicates
已知一个字符串,其中可能包括相同的字符。写一个函数打印这些字符的排列,但不能有重复的排列。
例子:
我们已经在前面讨论过了打印所有的排列,但是那个代码不能处理相同的字符,打印出来的会有相同的排序。
输出:
时间复杂度: O(n2 * n!)
附加空间: O(1)
已知一个字符串,其中可能包括相同的字符。写一个函数打印这些字符的排列,但不能有重复的排列。
例子:
Input: str[] = "AB" Output: AB BA Input: str[] = "AA" Output: AA Input: str[] = "ABC" Output: ABC ACB BAC BCA CBA CAB Input: str[] = "ABA" Output: ABA AAB BAA Input: str[] = "ABCA" Output: AABC AACB ABAC ABCA ACBA ACAB BAAC BACA BCAA CABA CAAB CBAA
我们已经在前面讨论过了打印所有的排列,但是那个代码不能处理相同的字符,打印出来的会有相同的排序。
// Program to print all permutations of a string in sorted order. #include <stdio.h> #include <stdlib.h> #include <string.h> /* Following function is needed for library function qsort(). */ int compare(const void *a, const void * b) { return ( *(char *)a - *(char *)b ); } // A utility function two swap two characters a and b void swap(char* a, char* b) { char t = *a; *a = *b; *b = t; } // This function finds the index of the smallest character // which is greater than 'first' and is present in str[l..h] int findCeil(char str[], char first, int l, int h) { // initialize index of ceiling element int ceilIndex = l; // Now iterate through rest of the elements and find // the smallest character greater than 'first' for (int i = l+1; i <= h; i++) if (str[i] > first && str[i] < str[ceilIndex]) ceilIndex = i; return ceilIndex; } // Print all permutations of str in sorted order void sortedPermutations(char str[]) { // Get size of string int size = strlen(str); // Sort the string in increasing order qsort(str, size, sizeof( str[0] ), compare); // Print permutations one by one bool isFinished = false; while (!isFinished) { // print this permutation static int x = 1; printf("%d %s \n", x++, str); // Find the rightmost character which is smaller than its next // character. Let us call it 'first char' int i; for (i = size - 2; i >= 0; --i) if (str[i] < str[i+1]) break; // If there is no such chracter, all are sorted in decreasing order, // means we just printed the last permutation and we are done. if (i == -1) isFinished = true; else { // Find the ceil of 'first char' in right of first character. // Ceil of a character is the smallest character greater than it int ceilIndex = findCeil(str, str[i], i + 1, size - 1); // Swap first and second characters swap(&str[i], &str[ceilIndex]); // Sort the string on right of 'first char' qsort(str + i + 1, size - i - 1, sizeof(str[0]), compare); } } } // Driver program to test above function int main() { char str[] = "ACBC"; sortedPermutations( str ); return 0; }
输出:
1 ABCC 2 ACBC 3 ACCB 4 BACC 5 BCAC 6 BCCA 7 CABC 8 CACB 9 CBAC 10 CBCA 11 CCAB 12 CCBA
时间复杂度: O(n2 * n!)
附加空间: O(1)
相关文章推荐
- 从键盘输入一个由字母构成的字符串(不大于30个字符),要求从该串中取出3个不重复的字符,求所有不同的取法。如果字符串中没有取到3个不同的字符,则提示没有结果。对取出的字符要求按字母升序排列成串,对于不
- 输入一个字符串,打印出该字符串中字符的所有排列。
- java输入一个字符串,打印出该字符串中字符的所有排列,随机打乱排序
- 输入一个字符串,打印出该字符串中字符的所有排列。
- 从标准输入读入一个由字母构成的串(不大于30个字符)。从该串中取出3个不重复的字符,求所有的取法。取出的字符,要求按字母升序排列成一个串。不同的取法输出顺序可以不考虑。
- 输入一个字符串,打印出该字符串中字符的所有排列
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
- 写正确函数需要注意的地方:输入一个字符串,打印出该字符串中字符的所有排列
- 已知一个字符串,输出它包含字符的所有排列(permutations)
- 输入一个字符串,打印出该字符串中字符的所有排列
- 输出一个字符串所有排列。注意有重复字符
- 给一个字符串(包含重复字符),打印它的所有可能的组合。
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。
- java 输入一个字符串,打印出该字符串中字符的所有排列
- java 输入一个字符串,打印出该字符串中字符的所有排列
- 输入一个字符串,打印出该字符串中字符的所有排列
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。C++
- 传入一个字符串参数 获取这个字符串内所有字符的 不同排列情况
- java 输入一个字符串,打印出该字符串中字符的所有排列