《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’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- 梦织未来Windows驱动编程 第03课 驱动的编程规范
最近根据梦织未来论坛的驱动教程学习了一下Windows下的驱动编程,做个笔记备忘.这是第03课<驱动的编程规范>. 驱动部分包括基本的驱动卸载函数.驱动打开关闭读取写入操作最简单的分发例程 ...
- Altium_Designer-各种布线总结
1.常规布线:不详细说了,是个人就知道怎么弄.需要说明的是在布线过程中,可按小键盘的*键或大键盘的数字2键添加一个过孔:按L键可以切换布线层:按数字3可设定最小线宽.典型线宽.最大线宽的值进行切换. ...
- 搭建FTP服务
(一)FTP服务概述 FTP服务概述:名称.功能.特点.端口 VSFTP:very secure FTP 端口:21 服务安装#yum install vsftpd lftp -y ##lftp ...
- 【[USACO17DEC]Standing Out from the Herd】
题目 不会广义\(SAM\)啊 但信仰插入特殊字符就可以搞定一切了 我们先把所有的串搞在一起建出一个\(SAM\),记得在中间插入特殊字符 对于\(parent\)树上的一个节点,只有当其\(endp ...
- 在Centos上面用yum不能安装redis的朋友看过来
我得是centos 6.3,如果直接用yum安装redis,报错,如下:[root@CentOS6 etc]# yum install redisLoaded plugins: fastestmirr ...
- python导入其他文件夹下的.py文件
想在globalpararm中导入read_config中的类 import sys sys.path.append('..') from common.read_config import Read ...
- java基础必备单词讲解 day five
Rectangle width high height area employee tool param version author math guess resources 之前单词复习 path ...
- 使用phpExcel将数据批量导出
if(isset($_POST['export']) && $_POST['export'] == '导出所选数据') { //此处为多选框已勾选的数据 $export_id=$_PO ...
- link链接外部样式表
一个完整的link标签: <link href="[css adress]" rel="stylesheet" type="text/css&q ...
- vscode + leetcode +github 同步
1.用VScode打开本地leetcode文件夹 C:\Users\Administrator\.leetcode 2.上传到本地git库 3.打开github桌面,上传到远程库