《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40
题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典。请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中。
解法:O(n^3)级别的时间和空间进行动态规划。这道题目和第17章的最后一题很像,由于这题的时间复杂度实在是高,我动手写了字典树进行加速。如果单纯用哈希表来作为词典,查询效率实际会达到O(n)级别,导致最终的算法复杂度为O(n^4)。用字典树则可以加速到O(n^3),因为对于一个字符串“abcd”,只需要从字典树的根节点出发往下走,就可以一次性判断“a”、“ab”、“abc”、“abcd”是否包含在字典里,而用哈希表无法做到这样的效率。动态规划过程仍然是O(n^4)级别,字典树只是将查单词的过程中进行了加速,从O(n^4)加速到了O(n^3)。空间复杂度为O(n^3),用于标记横向和纵向的每一个子段是否包含在字典里。
代码:
// 18.13 There is a matrix of lower case letters. Given a dictionary of words, you have to find the maximum subrectangle, such that every row reading from left to right, and every column reading from top to bottom is a word in the dictionary. Return the area as the result.
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std; const int MAX_NODE = ; struct TrieNode {
bool is_word;
char ch;
vector<int> child;
TrieNode(char _ch = ): is_word(false), ch(_ch), child(vector<int>(, -)) {};
};
int node_count;
TrieNode nodes[MAX_NODE]; void insertWordIntoTrie(int root, const string &s)
{
int len = s.length(); for (int i = ; i < len; ++i) {
if (nodes[root].child[s[i] - 'a'] < ) {
nodes[root].child[s[i] - 'a'] = node_count++;
root = nodes[root].child[s[i] - 'a'];
nodes[root].ch = s[i];
} else {
root = nodes[root].child[s[i] - 'a'];
}
if (i == len - ) {
nodes[root].is_word = true;
}
}
} int constructTrie(const unordered_set<string> &dict)
{
int root = node_count++; unordered_set<string>::const_iterator usit;
for (usit = dict.begin(); usit != dict.end(); ++usit) {
insertWordIntoTrie(root, *usit);
} return root;
} int main()
{
int i, j, k;
int i1, i2;
string s;
int n, m;
vector<vector<char> > matrix;
vector<vector<vector<bool> > > is_row_word;
vector<vector<vector<bool> > > is_col_word;
vector<int> dp;
unordered_set<string> dict;
int root;
int ptr;
int max_area; while (cin >> n && n > ) {
node_count = ;
for (i = ; i < n; ++i) {
cin >> s;
dict.insert(s);
}
root = constructTrie(dict); cin >> n >> m; matrix.resize(n);
for (i = ; i < n; ++i) {
matrix[i].resize(m);
}
for (i = ; i < n; ++i) {
cin >> s;
for (j = ; j < m; ++j) {
matrix[i][j] = s[j];
}
} is_row_word.resize(n);
for (i = ; i < n; ++i) {
is_row_word[i].resize(m);
for (j = ; j < m; ++j) {
is_row_word[i][j].resize(m);
}
} is_col_word.resize(m);
for (i = ; i < m; ++i) {
is_col_word[i].resize(n);
for (j = ; j < n; ++j) {
is_col_word[i][j].resize(n);
}
} for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
ptr = root;
for (k = j; k < m; ++k) {
if (ptr < ) {
is_row_word[i][j][k] = false;
continue;
} ptr = nodes[ptr].child[matrix[i][k] - 'a'];
if (ptr < ) {
is_row_word[i][j][k] = false;
continue;
} is_row_word[i][j][k] = nodes[ptr].is_word;
}
}
} for (i = ; i < m; ++i) {
for (j = ; j < n; ++j) {
ptr = root;
for (k = j; k < n; ++k) {
if (ptr < ) {
is_col_word[i][j][k] = false;
continue;
} ptr = nodes[ptr].child[matrix[k][i] - 'a'];
if (ptr < ) {
is_col_word[i][j][k] = false;
continue;
} is_col_word[i][j][k] = nodes[ptr].is_word;
}
}
} max_area = ;
dp.resize(m);
for (i1 = ; i1 < n; ++i1) {
for (i2 = i1; i2 < n; ++i2) {
k = ;
for (j = ; j < m; ++j) {
dp[j] = is_col_word[j][i1][i2] ? (++k) : (k = );
if (!dp[j]) {
continue;
} for (i = i1; i <= i2; ++i) {
if (!is_row_word[i][j - dp[j] + ][j]) {
break;
}
}
if (i > i2) {
max_area = max(max_area, (i2 - i1 + ) * dp[j]);
}
}
}
} cout << max_area << endl; // clear up data
dict.clear();
for (i = ; i < n; ++i) {
matrix[i].clear();
}
matrix.clear(); for (i = ; i < n; ++i) {
for (j = ; j < m; ++j) {
is_row_word[i][j].clear();
}
is_row_word[i].clear();
}
is_row_word.clear(); for (i = ; i < m; ++i) {
for (j = ; j < n; ++j) {
is_col_word[i][j].clear();
}
is_col_word[i].clear();
}
is_col_word.clear();
} return ;
}
《Cracking the Coding Interview》——第18章:难题——题目13的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第18章:难题——题目12
2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- 同步软件UltraCompare 64位 软件及注册机
软件及注册机下载: https://share.weiyun.com/f09e6243887e374ead1b3a3ab8f611a9 软件官方下载地址: https://www.ultraedit ...
- java集合框架——List
一.List接口概述 List有个很大的特点就是可以操作角标. 下面开始介绍List接口中相对于Collection接口比较特别的方法.在Collection接口中已经介绍的方法此处就不再赘述. 1. ...
- leetcode_No.1 Two Sum
原题: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- 系统运行时间悬浮框(demo)
此项目基于.net framework 4.0 思路: 拖一个定时器控件,每秒执行一次,调用函数查询当前运行时间并更新到label控件. private void Form1_Load(object ...
- python非字符串与字符产链连接
第一种办法: "hello" +' '+str(110) 输出结果: 'hello 110' 第二种办法: import numpy x = 110 print 'hello(%d ...
- YSlow的安装与说明文档
yslow官网 http://yslow.org/ 很明显起这个名字是说why slow 为什么这么慢,理所当然是为当前网页进行检测 借百度的 什么是YSlow? YSlow是yahoo发布的一款基于 ...
- js 实现链表
我们通常会在c++这类语言中学习到链表的概念,但是在js中由于我们可以动态的扩充数组,加之有丰富的原生api.我们通常并不需要实现链表结构.由于突发奇想,我打算用js实现一下: 首先我们要创建链表: ...
- js循环读取json数据,将读取到的数据用js写成表格
①js循环读取json数据的方式: var data=[{"uid":"2688","uname":"*江苏省南菁高级中学 022 ...
- dicom和dicomdir
转载http://blog.sina.com.cn/s/blog_4bce5f4b01019ix5.html DICOM 文件内容在 Part 3 DICOM IOD 里定义.CT, MR, CR, ...
- 【学时总结】◆学时·VI◆ SPLAY伸展树
◆学时·VI◆ SPLAY伸展树 平衡树之多,学之不尽也…… ◇算法概述 二叉排序树的一种,自动平衡,由 Tarjan 提出并实现.得名于特有的 Splay 操作. Splay操作:将节点u通过单旋. ...