辞书最低排列,使得所有的相邻字母的顺序是不同的 [英] Lexicographic minimum permutation such that all adjacent letters are distinct

查看:271
本文介绍了辞书最低排列,使得所有的相邻字母的顺序是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个奖励学校的任务,这是我们没有收到任何教学还,我不是在寻找一个完整的code,但一些提示,以帮助将是pretty的凉爽。要发布什么,我已经在Java中迄今所做,当我回家,但这里的东西我已经做了。

It's a bonus school task for which we didn't receive any teaching yet and I'm not looking for a complete code, but some tips to get going would be pretty cool. Going to post what I've done so far in Java when I get home, but here's something I've done already.

所以,我们要做一个排序算法,这对于例如排序AAABBB到ABABAB。最大输入大小为10的6次方,而这一切有小于1秒的情况发生。如果有一个以上的答案,第一个按字母顺序是正确的。我开始测试不同的算法,甚至对它们进行排序没有记住,字母顺序的要求,只是为了看看是如何工作的。

So, we have to do a sorting algorithm, which for example sorts "AAABBB" to the ABABAB. Max input size is 10^6, and it all has to happen under 1 second. If there's more than one answer, the first one in alphabetical order is the right one. I started to test different algorithms to even sort them without that alphabetical order requirement in mind, just to see how the things work out.

第一个版本:

保存ASCII codeS的整数数组,其中指数是ASCII code,和值是该字符出现的字符数组中的金额。 然后我找到2最高的数字,开始他们垃圾邮件后,对方新的字符数组,直到一些数字较高,我交​​换到它。它运作良好,但当然的顺序是不正确的。

Save the ascii codes to the Integer array where index is the ascii code, and the value is amount which that character occurs in the char array. Then I picked 2 highest numbers, and started to spam them to the new character array after each other, until some number was higher, and I swapped to it. It worked well, but of course the order wasn't right.

第二个版本:

遵循了同样的想法,但停止采摘最出现的数量和刚摘的索引,他们在我的数组的顺序。效果很好,直到输入的是一样的东西CBAYYY。其算法排序的ABCYYY,而不是AYBYCY。当然,我可以尝试寻找一些免费景点对于那些个Y,但在这一点上,它开始花费太长的时间。

Followed the same idea, but stopped picking the most occurring number and just picked the indexes in the order they were in my array. Works well until the input is something like CBAYYY. Algorithm sorts it to the ABCYYY instead of AYBYCY. Of course I could try to find some free spots for those Y's, but at that point it starts to take too long.

推荐答案

这是有趣的问题,有一个有趣的调整。是的,这是置换或重新安排,而不是一种。不,报问题不重复。

An interesting problem, with an interesting tweak. Yes, this is a permutation or rearranging rather than a sort. No, the quoted question is not a duplicate.

算法。

  1. 计数的字符频率。
  2. 从两个最低的字母顺序输出交流字符。
  3. 当每个被耗尽,移动到下一个。
  4. 在一些点的最高频率炭会完全一半,剩下的字符。在这一点上切换到输出的所有字符交替依次按字母顺序排列的其他剩余的字符。

要求,以避免关闭接一个错误(奇VS偶数输入字符)一些照顾。否则,只是写了code和得到它的工作的权利是一个挑战。

Some care required to avoid off-by-one errors (odd vs even number of input characters). Otherwise, just writing the code and getting it to work right is the challenge.

请注意,有一个特殊的情况,其中的字符数是奇数,一个字符的频率开始于(半加1)。在这种情况下,你需要启动该算法在步骤4,所有输出一个字符与每个人的交替轮流。

Note that there is one special case, where the number of characters is odd and the frequency of one character starts at (half plus 1). In this case you need to start with step 4 in the algorithm, outputting all one character alternating with each of the others in turn.

还要注意的是,如果一个字符包括多于一半的输入然后分开这个特殊的情况下,没有解决方案是可能的。这种情况可能会提前,或在执行中检测到检查的频率时,尾部由所有一个字符。检测这种情况下是不是规范的一部分。

Note also that if one character comprises more than half the input then apart for this special case, no solution is possible. This situation may be detected in advance by inspecting the frequencies, or during execution when the tail consists of all one character. Detecting this case was not part of the spec.

由于不需要排序的复杂度为O(n)。每个字符被检查两次:一次时,它被计数,一旦当它被添加到输出。其他一切摊销。

Since no sort is required the complexity is O(n). Each character is examined twice: once when it is counted and once when it is added to the output. Everything else is amortised.

这篇关于辞书最低排列,使得所有的相邻字母的顺序是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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