《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’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- 利用C语言编辑画图程序的实现方法
不知道大家在进行开发县级电网调度自动化系统的时候,是否都会遇到一个问题就是:要绘制一个电力系统一次接线图.大家都应该知道其实电力系统的一次接线图是较为复杂的,如果想要使用一般的编程方法来进行绘制的话, ...
- 笨办法学Python(九)
习题 9: 打印,打印,打印 # Here's some new strange stuff, remember type it exactly. days = "Mon Tue Wed T ...
- 笨办法学Python(七)
习题 7: 更多打印 现在我们将做一批练习,在练习的过程中你需要键入代码,并且让它们运行起来.我不会解释太多,因为这节的内容都是以前熟悉过的.这节练习的目的是巩固你学到的东西.我们几个练习后再见.不要 ...
- 如何使用事务码SMICM分析ABAP代码发起的HTTP请求的错误ICM_HTTP_SSL_PEER_CERT_UNTRUSTED
当我用CL_HTTP_CLIENT往一个外网的url发请求时,遇到错误:ICM_HTTP_SSL_PEER_CERT_UNTRUSTED 错误是从这段ABAP代码里抛出来的: CALL METHOD ...
- 141. Linked List Cycle (amazon)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- vsftpd 启动 vsftpd:500 OOPS: bad bool value in config file for: guest_enable
不然启动时会涌现毛病,举个例子 guest_enable=YES 后面出现空格,就会出现 为 vsftpd 启动 vsftpd:500 OOPS: bad bool value in config ...
- 阿里云主机ss
https://promotion.aliyun.com/ntms/act/vm/aliyun-group/buy.html?group=HdcwGIaf6i
- veritas.com常用资源汇总
NetBackup 8.1.2文档(合集) https://www.veritas.com/support/en_US/article.100044086 NetBackup产品组停止支持生命周期 ...
- EF 连接数据库 Mysql (database first ) 一个表对应一个模型
准备工作 1.下载vs2015 2.下载mysql2017 3.安装 1.创建数据库 2. 将数据库映射成模型 3创建aspx 文件. 写下窗体内容的代码 hello_worldEntities en ...
- 第3章 如何用DAP仿真器下载程序—零死角玩转STM32-F429系列
第3章 如何用DAP仿真器下载程序 集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/firege ...