30:字符串重排
// we have defined the necessary header files here for this problem.
// If additional header files are needed in your program, please import here.
int ans=0;
static int cmp(const void *a, const void *b)
{
return *(char*)a - *(char*)b;
}
static void dfs(int idx, int *vis, int sum, int len, char *str)
{
if (sum == len)
{
ans++;
return;
}
for (int i = 0; i < len; i++)
{
if (i > 0 && str[i] == str[i-1] && !vis[i-1]) {
continue; // "同一层" 相同的跳过
}
if (!vis[i]) {
vis[i] = 1;
dfs(idx+1, vis, sum+1, len, str);
vis[i] = 0;
}
}
}
int main()
{
// please define the C input here. For example: int n; scanf("%d",&n);
// please finish the function body here.
// please define the C output here. For example: printf("%d\n",a);
int len = 0;
char str[11] = {
0};
scanf("%s", str);
len = strlen(str);
qsort(str, len, sizeof(char), cmp);
int vis[11] = {
0};
dfs(0, vis, 0, len, str);
printf("%d", ans);
return 0;
}
46.全排列