-
题目描述:
-
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
-
输入:
-
每个测试案例包括1行。
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
-
输出:
-
对应每组数据,按字典序输出所有排列。
-
样例输入:
-
abc
-
BCA
-
样例输出:
-
abc
-
acb
-
bac
-
bca
-
cab
-
cba
-
-
ABC
-
ACB
-
BAC
-
BCA
-
CAB
-
CBA
-
-
解题问题:使用C++的string和cout居然超时,改为C的字符数组和printf能AC。
-
-
#include <iostream> #include <cstdio> #include <vector> #include <string> #include <algorithm> #include <cstring> using namespace std; char str[12]; void getAllPermutation(int start, int length) { if (start == length) { printf("%s\n", str); return; } for (int i = start; i < length; ++i) { if (i != start && str[i] == str[i - 1]) continue; swap(str[start], str[i]); sort(str + start + 1, str + length); getAllPermutation(start + 1, length); sort(str + start, str + length); } } int main(void) { while (scanf("%s", str) != EOF) { int length = strlen(str); sort(str, str + length); getAllPermutation(0, length); } return 0; }