问题描述
我想编写一个Java程序,该程序搜索密文并返回密文中字符的频率计数,例如密文:"jshddllpkeldldwgbdpked"将具有以下结果:
I want to write a java program that searches through a cipher text and returns a frequency count of the characters in the cipher, for example the cipher:"jshddllpkeldldwgbdpked" will have a result like this:
出现2个字母:
pk = 2,ke = 2,ld = 2
pk = 2, ke = 2, ld = 2
3次出现字母:
pke = 2.
有什么算法可以让我尽可能高效地做到这一点?
Any algorithm that allows me to do this as efficiently as possible?
推荐答案
地图策略是一种很好的策略,但我会选择HashMap<String, Integer>
,因为它是要计算的元组.
The map strategy is a good one, but I'd go for HashMap<String, Integer>
, since it's tuples of characters being counted.
遍历密文中的字符,可以保存最后的X个字符,这将为您提供所有出现的长度为X + 1的子字符串的映射.
Iterating over the characters in the ciphertext, you can save the last X characters and that will give you a map over all occurrences of substrings of length X+1.
这篇关于频率分析算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!