如何在表中查找的话? [英] How to find words in a table?

查看:138
本文介绍了如何在表中查找的话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个编码的运动。假设有一个字母表和大量的词汇。我一定要找到表中的单词位置。一个字可以任意位置开始在表中,可定向或者垂直水平。 (我们可以假设,一个行/列只包含一个字)。

This is a coding exercise. Suppose there is a table of letters and a number of words. I have to find positions of the words in the table. A word may begin anywhere in the table and may be oriented either vertically of horizontally. (We can assume that a row/column may contain only one word).

例如:


table = xabcx
        xxxdx
        xxfex

words = ["abc", "edc", "fe"]

expected output is (0,1), (2,3), (2,2)

这种简单的解决办法是遍历所有的行/列,并检查各行/列包含任何的话。它采用 0(列数*行数*字数*字长)。有没有更好的解决办法?也许我应该pre-过程中的生字表,以建立一个更有效的数据结构?

The straightforward solution is to loop over all rows/columns and check if each row/column contains any of the words. It takes O(number of columns * number of rows * number of words * word length). Is there a better solution? Maybe I should pre-process the words list to build a more efficient data structure?

推荐答案

您可以使用特里数据结构来存储表。查一查的话是很容易的,一旦你拥有了特里。

You can use Trie data structure to store the table. Look up of words is very easy once you have the Trie.

这篇关于如何在表中查找的话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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