解法一:基于字典树的思想dfs,字典树我之前有写过。
代码:

#include<string>
#include<iostream>
using namespace std;

int sum;
int n;
string s;
int sn[510];
char ch[510];
void dfs(int depth, char ch[]) {
	if (depth > n) {
		sum++;
		for (int i = 1; i <= n; i++) {
			cout << ch[i];
		}
		cout << endl;
		return;
	}
	for (int i = 1; i <= 26; i++) {
		if (sn[i] > 0) {
			ch[depth] = i + 'a' - 1;
			--sn[i];
			dfs(depth + 1, ch);
			++sn[i];
		}
	}
}
int main()
{
	cin >> n;
	cin >> s;
	for (int i = 0; i < s.size(); i++) {
		++sn[s[i] - 'a' + 1];
	}
	dfs(1, ch);
	cout << sum << endl;
}

解法二:

#include<algorithm>
next_permutation(a,a+n)

next_permutation(a,a+n)放在一个循环里,可以输出该数之后的所有排列,不包括自己,一般我们会先对他升序,然后输出全排列。(stl牛逼!!!

代码:

#include<bits/stdc++.h>
using namespace std;
char a[510];
int n;
int sum=1;
int main()
{
	scanf("%d",&n);
	scanf("%s",a);
	sort(a,a+n);
	printf("%s\n",a);
	while(next_permutation(a,a+n)){
		printf("%s",a);
		printf("\n");
		sum++;
	} 
	printf("%d\n",sum);
}