生成不是彼此镜像的置换 [英] Generating permutations which are not mirrors of each other

查看:74
本文介绍了生成不是彼此镜像的置换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成n个数字的置换,其中没有两个置换是彼此反向的(从最后一个字符读取到第一个字符的第一个与第二个字符相同).例如,n = 3,我想生成:

I want to generate permutations of n numbers, where there is no two permutations which are reversions of each other (the first one read from the last character to the first is the same as the second one). For instance, n = 3, I want to generate:

1 2 3 //but not 3 2 1
1 3 2 //but not 2 3 1
2 1 3 //but not 3 1 2

我不在乎会生成两者之一.该算法应适用于大n(> 20).是否有任何此类算法或方法可以检查生成的排列是否是先前生成的排列的镜像?

I do not care which one of the two will be generated. The algorith should be applicable for large n (>20). Is there any such algorithm or a way to check if the generated permutations is a mirror of previously generated one?

推荐答案

使用std::next_permutation并忽略第一个元素大于最后一个元素的排列.

Use std::next_permutation and ignore permutations whose first element is larger than its last.

这篇关于生成不是彼此镜像的置换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆