- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有些摩尔斯电码丢失了字母之间的空格,我的挑战是找出消息中所说的内容。到目前为止,由于可能存在大量的组合,我有点迷失了。
这是关于我收到的消息的所有信息。
-..-...-...-...-..-.-.-.-.-..-.-.-.-.-.-.-.-.-.-..-...-.
最佳答案
这不是一个容易的问题,因为如ruakh所建议的,给定消息有很多可行的句子。例如,“JACK AND JILL WENT UP HILL”具有与“JACK AND JILL WALK CHISELED”相同的编码。由于这些都是语法句子,并且每个句子中的单词都很常见,因此在不深入研究自然语言的情况下,如何挑选一个或另一个(或其他40141055989476564163599个具有相同编码格式的英语单词序列中的任何一个)尚不明确加工。
无论如何,这是一个动态编程解决方案,用于解决查找最短句子(如果有平局,字符最少)的问题。它还可以计算与给定消息具有相同编码的句子总数。它需要一个英语单词词典在一个文件中。
接下来的增强应该是更好地衡量句子的可能性:单词频率,莫尔斯电码中的假阳性率(例如,“I”是一个常用词,但通常作为莫尔斯电码序列的其他序列的一部分出现) 。棘手的部分将是制定一个好的得分函数,该函数可以通过使用动态编程来计算的方式来表示。
MORSE = dict(zip('ABCDEFGHIJKLMNOPQRSTUVWXYZ', [
'.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....',
'..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.',
'--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-',
'-.--', '--..'
]))
# Read a file containing A-Z only English words, one per line.
WORDS = set(word.strip().upper() for word in open('dict.en').readlines())
# A set of all possible prefixes of English words.
PREFIXES = set(word[:j+1] for word in WORDS for j in xrange(len(word)))
def translate(msg, c_sep=' ', w_sep=' / '):
"""Turn a message (all-caps space-separated words) into morse code."""
return w_sep.join(c_sep.join(MORSE[c] for c in word)
for word in msg.split(' '))
def encode(msg):
"""Turn a message into timing-less morse code."""
return translate(msg, '', '')
def c_trans(morse):
"""Construct a map of char transitions.
The return value is a dict, mapping indexes into the morse code stream
to a dict of possible characters at that location to where they would go
in the stream. Transitions that lead to dead-ends are omitted.
"""
result = [{} for i in xrange(len(morse))]
for i_ in xrange(len(morse)):
i = len(morse) - i_ - 1
for c, m in MORSE.iteritems():
if i + len(m) < len(morse) and not result[i + len(m)]:
continue
if morse[i:i+len(m)] != m: continue
result[i][c] = i + len(m)
return result
def find_words(ctr, i, prefix=''):
"""Find all legal words starting from position i.
We generate all possible words starting from position i in the
morse code stream, assuming we already have the given prefix.
ctr is a char transition dict, as produced by c_trans.
"""
if prefix in WORDS:
yield prefix, i
if i == len(ctr): return
for c, j in ctr[i].iteritems():
if prefix + c in PREFIXES:
for w, j2 in find_words(ctr, j, prefix + c):
yield w, j2
def w_trans(ctr):
"""Like c_trans, but produce a word transition map."""
result = [{} for i in xrange(len(ctr))]
for i_ in xrange(len(ctr)):
i = len(ctr) - i_ - 1
for w, j in find_words(ctr, i):
if j < len(result) and not result[j]:
continue
result[i][w] = j
return result
def shortest_sentence(wt):
"""Given a word transition map, find the shortest possible sentence.
We find the sentence that uses the entire morse code stream, and has
the fewest number of words. If there are multiple sentences that
satisfy this, we return the one that uses the smallest number of
characters.
"""
result = [-1 for _ in xrange(len(wt))] + [0]
words = [None] * len(wt)
for i_ in xrange(len(wt)):
i = len(wt) - i_ - 1
for w, j in wt[i].iteritems():
if result[j] == -1: continue
if result[i] == -1 or result[j] + 1 + len(w) / 30.0 < result[i]:
result[i] = result[j] + 1 + len(w) / 30.0
words[i] = w
i = 0
result = []
while i < len(wt):
result.append(words[i])
i = wt[i][words[i]]
return result
def sentence_count(wt):
result = [0] * len(wt) + [1]
for i_ in xrange(len(wt)):
i = len(wt) - i_ - 1
for j in wt[i].itervalues():
result[i] += result[j]
return result[0]
msg = 'JACK AND JILL WENT UP THE HILL'
print sentence_count(w_trans(c_trans(encode(msg))))
print shortest_sentence(w_trans(c_trans(encode(msg))))
关于puzzle - 不带空格地翻译摩尔斯电码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8349136/
以下是来自Interviewstreet的问题我没有从他们的网站上得到任何帮助,所以在这里提问。我对算法/解决方案不感兴趣,但我不理解他们作为第二个输入示例给出的解决方案。任何人都可以帮助我理解问题陈
根据我的理解,创建一个可解决的滑动拼图必须遵循以下规则: A. If the grid width is odd, then the number of inversions in a solvabl
我编写了一系列函数来尝试解决 N 难题/8 难题。 我对自己处理拼图的能力感到非常满意,但在如何迭代和找到最佳路径方面苦苦挣扎。我的技能也不是 OOP,所以功能很简单。 这个想法显然是为了减少 her
我有些摩尔斯电码丢失了字母之间的空格,我的挑战是找出消息中所说的内容。到目前为止,由于可能存在大量的组合,我有点迷失了。 这是关于我收到的消息的所有信息。 输出将为英语 总是会有有意义的翻译 这是示例
如果我知道,变量 a,b,c,d,e 有多少可能的组合: a+b+c+d+e = 500 并且它们都是整数且 >= 0,所以我知道它们是有限的。 最佳答案 @Torlack、@Jason Cohen:
检查 n 的数组是否整数包含 3 个可以形成三角形的数字(即两个数字中任何一个的总和大于第三个)。 显然,这可以在 O(n) 中完成。时间。 (明显的 O(n log n) 解决方案是对数组进行排序,
我有兴趣实现 14-15 puzzle : 我正在创建一个值 0 - 15 按递增顺序排列的数组: S = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
Closed. This question is off-topic。它当前不接受答案。 想要改善这个问题吗? Update the question,所以它是用于堆栈溢出的on-topic。 已关闭
所以,我对数独游戏的生成做了大量的阅读。据我所知,获得所需难度的数独谜题的标准方法是生成一个谜题,然后对其进行评分,然后重复,直到获得可接受的评分。这可以通过使用一些更复杂的求解模式(XY-wing、
如何混合两个ARGB像素? 例 在这里,A是(带有Alpha的红色),而B是(带有Alpha的蓝色)。 最佳答案 取自获得图像的同一Wikipedia文章: 转换为介于0到255之间的值: rOut
Closed. This question is off-topic。它当前不接受答案。 想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。 已关闭8
Joel 在他的 Guerrilla Guide to Interviewing 中提到计算一个字节中设置位的数量是一个编程问题。 ,并谈到了一种利用查找表中出现的模式的方法。在我发现这种模式后不久,
我在做一些回顾时遇到了这个报告的面试问题(以下引用是我找到的关于这个问题的所有信息): Given a function for a fair coin, write a function for a
我遇到了这个问题:说给定两个权重1和3,您可以权衡1,2(乘以3-1),3,4(乘以3 + 1)(使用平衡的两面)。现在找到最小的砝码数量,以便可以测量1到1000。 答案是1,3,9,27 ...
Locked. This question and its answers are locked因为该问题是题外话,但具有历史意义。它当前不接受新的答案或互动。 我使用这些规则制作了最终的笑声发生器。
左边是布局的正常状态。右边是布局的展开状态。 我的问题是我不知道如何让粉色框在它们的单元格中居中,并且随着布局向任何方向增长,粉色框之间的绿线连接起来。 只有这两个 View 的 AutoSizing
考虑以下代码: template struct S { }; template struct B; template class C> struct B> { void f() { } };
问题: 编写一个程序来删除出现在“所有”字符串中的片段,其中一个片段是 3 个或更多个连续的单词。 示例: 输入: s1 = "正在下雨,我想开车回家。"; s2 = "下雨了,我想去滑雪。"; s3
请检查以下程序。 #include struct st { int a ; } fn () { struct st obj ; obj.a = 10 ; return obj ; } int
我一直在研究这个 Prolog 逻辑问题。它得到了不正确的结果,运行程序本身需要一段时间,这是我更关心的。逻辑问题如下: Alex, Bret, Chris, Derek, Eddie, Fred,
我是一名优秀的程序员,十分优秀!