什么是最好的算法组一样串起来? [英] What's the best algorithm to group the same string together?

查看:110
本文介绍了什么是最好的算法组一样串起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个排序算法将是工作,但它是一个矫枉过正。

Each sort algorithm will be the job, but it's a OVERKILL.

有关的输入,如:

aa
cc
aa
bb
dd
bb
cc

我只是需要一些这样的:

I just need something like:

aa
aa
cc
cc
bb
bb
dd

时,不要求每个模式的顺序

The order of each pattern is not required.

有没有这样的算法,这样的工作?

Is there such an algorithm for this kind of job?

推荐答案

您只是想使用哈希表在这里,或者更抽象的关联数组。遍历输入,具有值将它添加到散列表(标签,如果preFER)的 1 的,如果它尚未看到的,或者被一个递增计数,如果它已经存在在哈希表。

You simply want to use a hashtable here, or more abstractly an associative array. Iterate over the input, adding it to the hashtable with value (tag, if you prefer) of 1 if it hasn't yet been seen, or incrementing the count by one if it already exists in the hashtable.

的算法是这样的 O(N)的在时间和空间,这是因为你可以合理地预期那么好。我推荐一些在哈希表读了起来,因为它是出现在各种各样的算法和软件设计的地方一个非常有用的数据结构。

The algorithm is thus O(n) in both time and space, which is as good as you could reasonably expect. I recommend some reading up on hashtables, as it is a highly useful data structure that appears in all sorts of places in algorithm and software design.

这篇关于什么是最好的算法组一样串起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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