一种算法可以告诉我们需要多少个不同的前导字符才能从列表中识别一个单词?
因此,如果您有一个包含 50 个单词的列表,并且您想了解读者必须深入了解某个单词才能计算出所有唯一单词,您会如何做呢?
我基本上正在考虑将字符逐一加载到数组中,然后比较它们。不过,有很多字符和很多数组需要比较。我想知道如果已经有一种有效的方法,那么最有效的方法是什么?
我现在正在尝试使用 Javascript。
var words = [sort(prompt("Please, insert the word list", "default value in the text field"););];
var encr_int: Number=0;
for (i=0, j=0, maxdif=0; j < word.length; i++) {
if(word[j].text.charAt(i) == word[j+1].text.charAt(i) AND i > maxdif) {
maxdif = i;
}
else if(word[j].text.charAt(i) != word[j+1].text.charAt(i) {
j+=1;
}
else if(word[j].text.charAt(i) == "") {
i = 0;
}
}
document.write(maxdif);
以上是我根据第一个答案编写程序的努力。
So if you have a list of 50 words, and you want to see how deep into a word a reader must look to be able to count all of the unique words, how would you go about doing that?
I'm basically thinking about loading characters into an array, one-by-one, and then comparing them. There are so many characters and so many arrays to compare, though. I wonder what's the most efficient way, if there's already an efficient way out there?
I'm trying to use Javascript, right now.
var words = [sort(prompt("Please, insert the word list", "default value in the text field"););];
var encr_int: Number=0;
for (i=0, j=0, maxdif=0; j < word.length; i++) {
if(word[j].text.charAt(i) == word[j+1].text.charAt(i) AND i > maxdif) {
maxdif = i;
}
else if(word[j].text.charAt(i) != word[j+1].text.charAt(i) {
j+=1;
}
else if(word[j].text.charAt(i) == "") {
i = 0;
}
}
document.write(maxdif);
Above is my effort at writing the program based on the first answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更有效的方法可能是将单词集存储在 trie 结构而不是列表中。这是一个层次结构,其中每个节点都包含其子节点的前缀字符。这意味着您不必与所有单词进行比较 - 一旦发现某个前缀与没有该前缀的单词匹配,则无需进行比较。
尽管对于 50 个单词,速度不太可能成为问题,但 trie 将使您能够最大限度地减少所需的字符比较数量,并且您可以在向下递归层次结构时跟踪字符计数。
如果绝对效率是一个要求,那么组织特里树的精确方式可能会变得很重要,例如,考虑到对其进行的搜索的实际统计数据,可以将其组织得更加高效。
A more efficient approach might be to store your set of words in a trie structure rather than a list. This is a hierarchical structure where each node contains the prefix characters of its children. This means that you don't have to compare against all words - once a certain prefix is found to match words without that prefix need are eliminated.
Although for 50 words speed is not likely to be an issue, the trie will enable you to minimise the number of character comparisons needed and you can keep track of the character count as you recurse down the hierarchy.
If absolute efficiency is a requirement then the precise way in which you organise the trie might become important for example it could be organised to be efficient taking account of the actual statistics of the searches being made against it.
对列表进行排序,然后迭代它,将每个单词与后续单词进行比较。与告诉您在发现差异之前必须检查多少个字符的例程进行比较。边走边跟踪最大“深度”。
edit — 一个根据前导字符告诉您两个单词的“相似性”的函数:
Sort the list and then iterate through it, comparing each word with the subsequent word. Compare with a routine that tells you how many characters had to be checked before a difference was found. Keep track of the maximum "depth" as you go.
edit — a function to tell you the "similarity" of two words based on leading characters: