一道可以使用Trie高级数据结构解决的算法题
2008-04-14 07:11
281 查看
Problem Statement | ||||||||||||
You have been given a "word search" puzzle, which consists of a rectangular grid of letters, in which several words are hidden. Each word may begin anywhere in the puzzle, and may be oriented in any straight line horizontally, vertically, or diagonally. However, the words must all go down, right, or down-right. (see examples) You are given a String[], grid, indicating the letters in the grid to be searched. Character j of element i of grid is the letter at row i, column j. You are also given a String[], wordList, indicating the words to be found in the grid. You are to return a String[] indicating the locations of each word within the grid. The return value should have the same number of elements as wordList. Each element of wordList corresponds to the element of the return value with the same index. Each element of the return value should be formatted as "row col" (quotes added for clarity), where row is the 0-based row in which the first letter of the word is found, and col is the 0-based column in which the first letter of the word is found. If the same word can be found more than once, the location in the lowest-indexed row should be returned. If there is still a tie, return the location with the lowest-indexed column. If a word cannot be found in the grid, return an empty string for that element. | ||||||||||||
Definition | ||||||||||||
| ||||||||||||
Constraints | ||||||||||||
- | grid will contain between 1 and 50 elements, inclusive. | |||||||||||
- | Each element of grid will contain between 1 and 50 characters, inclusive. | |||||||||||
- | Each element of grid will contain the same number of characters. | |||||||||||
- | Each character of each element of grid will be 'A'-'Z'. | |||||||||||
- | wordList will contain between 1 and 50 elements, inclusive. | |||||||||||
- | Each element of wordList will contain between 1 and 50 characters, inclusive. | |||||||||||
- | Each character of each element of wordList will be 'A'-'Z'. | |||||||||||
Examples | ||||||||||||
0) | ||||||||||||
| ||||||||||||
1) | ||||||||||||
| ||||||||||||
2) | ||||||||||||
|
因为数据量不是很大,直接暴力搜也可以通过。但是我还是用Trie写了一种解决方案。代码如下:
1//Use Trie to match word text pattern
2
53class Trie
54
109class WordFind
110
相关文章推荐
- 不使用 for , while ,递归,如何解决一道简单的算法题?
- win10自带ie和Edge浏览器无法上网解决方法 第三方浏览器和QQ可以使用
- 使用127.0.0.1可以访问phpmyadmin但不能用localhost访问的解决办法
- win7,win8 下PLSQL developer 连接不上64位Oracle 的解决方法 不安装orcal 使用PLSQL的组件 使用它可以不安装oracle连接数据库
- 使用Apache POI生成Excel文档时,当进行单元格合并操作后,被合并的单元格边框会消失,使用如下方式可以解决。
- 使用matlab实现遗传算法解决飞行员侦查问题
- [Linux] CentOS7安装Chrome后yum不可以使用的解决方法
- QT使用MySql的配置(使用addLibraryPath载入插件),编译QT的MySql驱动问题及解决方案(自己使用libmysql.lib进行编译mysql.pro,万不得已可以查看Makefile.Debug以解决问题)
- 每天一道算法题(13)——使用递归颠倒栈
- 在一个大型B2C项目中,如果存在大量重复查询,可以通过使用ThreadLocal来解决
- 使用.live()可以解决动态加载html,但js无效果的问题
- 解决 VS2005 SP1 时“无法使用此产品的安装源,请确认安装源存在,并且您可以访问它”的错误
- 再次打包IE64 可以使用的cab文件,关于inf文件写法和IE自动解压失败的解决过程
- 使用 STL 辅助解决算法问题
- 解决IE7一下版本的最小宽度。最大可以使用类似方法
- 解决SQL备份时,提示"不是多簇媒体集的一部分,可以使用BACKUP WITH FORMAT 来构造新的媒体集."
- 虚拟IP实验,遇到场景启用使用虚拟IP就报错,不启用可以正常运行的问题,解决方法
- 使用PHP描述顺序查找和二分查找(也叫做折半查找)算法,顺序查找必须考虑效率,对象可以是一个有序数组
- 使用xshell登录中文版CentOS时,在xshell下显示中文乱码,通过如下办法可以有效的解决乱码问题
- 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate 的解决办法