[洛谷]P1691 有重复元素的排列问题 (#搜索 -1.16)(#STL -1.5)

版权声明:JCBP工作室 & A.pro https://blog.csdn.net/Apro1066/article/details/82390258

题目描述

设R={r1,r2,……,rn}是要进行排列的n个元素。其中元素r1,r2,……,rn可能相同。使设计一个算法,列出R的所有不同排列。

给定n以及待排列的n个元素。计算出这n个元素的所有不同排列。

输入输出格式

输入格式:

第1行:元素个数n(1<=n<500)

第2行:一行字符串,待排列的n个元素

输出格式:

计算出的n个元素的所有不同排列,最后一行是排列总数。

输入输出样例

输入样例#1

4
aacc

输出样例#1

aacc
acac
acca
caac
caca
ccaa
6

说明

输出按字典顺序排


思路

一段STL或许将成为本题最短代码?

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
char a[501],b[501];
int n,s(1);
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	cin>>a;
	sort(a,a+n);
	strcpy(b,a);
	cout<<a<<endl;
	while(next_permutation(a,a+n))
	{
		if(strcmp(b,a)!=0)
		{
			cout<<a<<endl;
			s++;
		}
	}
	cout<<s<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Apro1066/article/details/82390258
1.5
今日推荐