[Lintcode]Word Squares(DFS|字符串)
题意
略
分析
0.如果直接暴力1000^5会TLE,因此考虑剪枝
1.如果当前需要插入第i个单词,其剪枝如下
1.1 其前缀(0~i-1)已经知道,必定在前缀对应的集合中找
– 第一个词填了ball 后,第二个词必须以a开头
– 第二个词填了area后,第三个词必须以le开头
– 以其他开头的就没必要搜下去了
1.2 第i+1n-1的单词,必定是以对应位置的0i-1的前缀+nextWord[k]作为前缀
— 第一个词填了ball
– 第二个词想填area的话
– 字典中必须有以le la开头的单词,否则没有的话就不能填area
1.3 如何实现?
利用hash或Trie
代码
class Solution {
public:
/*
* @param words: a set of words without duplicates
* @return: all word squares
*/
unordered_map<string,vector<string> >prefix;
vector<string>square;//存储字符串
vector<vector<string> >result;
vector<vector<string>> wordSquares(vector<string> &words) {
// write your code here
if(words.size()==0) return result;
initPrefix(words);
dfs(0);
return result;
}
void initPrefix(vector<string>&words)
{
for(int i=0;i<words.size();++i)
{
string str=words[i];
prefix[""].push_back(str);
for(int j=0;j<str.size();++j)//将每个字符串放入对应的前缀
prefix[str.substr(0,j+1)].push_back(str);
}
}
void dfs(int len)//当前放的行数
{
if(len==words[0].size())
{
result.push_back(square);
return ;
}
string pre;
//先将所放字符串前缀求出,竖向计算
for(int i=0;i<len;++i)
{
pre+=square[i][len];
}
vector<string>w=prefix[pre];//取出前缀有的字符串
for(int i=0;i<w.size();++i)
{
if(!check(len,w[i])) continue;
}
square.push_back(w[i]);
dfs(len+1);
square.pop_back();
}
/*
check的原则,检查未来插入的字符串是否有相同的前缀
*/
bool check(int len,string nextWord)
{
for(int j=len+1;j<words[0].size();++j)
{
string str;
for(int i=0;i<len;++i) str+=square[i][j];//第j列0~j-1前缀
str+=nextWord[j];//并加上第j列的字符
if(!prefix[str].size()) return false;
}
return true;
}
};
[Lintcode]Word Squares(DFS|字符串)的更多相关文章
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- Word Squares
Description Given a set of words without duplicates, find all word squares you can build from them. ...
- LC 425. Word Squares 【lock,hard】
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- C# 利用占位符替换word中的字符串和添加图片
利用占位符替换word中的字符串和添加图片 ///<summary> /// 替换word模板文件内容,包括表格中内容 /// 调用如下:WordStr ...
- LintCode 面试题 旋转字符串
1.题目描述 题目链接:http://www.lintcode.com/zh-cn/problem/rotate-string/ 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 2. ...
- LintCode笔记 - 8. 旋转字符串
这一题相对简单,但是代码质量可能不是很好,我分享一下我的做题笔记以及做题过程给各位欣赏,有什么不足望各位大佬指出来 原题目,各位小伙伴也可以试着做一下 . 旋转字符串 中文English 给定一个字符 ...
- 【LintCode】判断一个字符串是否包含另一个字符串的所有字符
问题描述: 比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母. 样例 给出 A = "ABCD" B = "ACD",返 ...
- [LintCode] Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
随机推荐
- imagecopyresampled()改变图片大小后质量要比imagecopyresized()高。
php程序中改变图片大小的函数大多数人都想到用imagecopyresized(),不过经过测试比较发现,使用imagecopyresampled()改变的图片质量更高. 下面我们来看看两者的比较结果 ...
- FI模块与SD、MM的接口配置方法
[转自 http://blog.itpub.net/195776/viewspace-1023910/] 1 FI/SD 借口配置FI/SD通过tcode VKOA为billing设置过帐科目,用户可 ...
- (转)浅析三层架构与MVC模式的区别
MVC模式介绍: MVC全名是Model ViewController,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用于组织代码用一种业务逻辑和数据 ...
- python之学习
------------------------------------------ 基本语句解析 import:导入某些模块或者文件 import random: 导入生成随机数模块 import ...
- 不使用 spring-boot-starter-parent 构建 spring boot 应用
创建 spring-boot 应用通用方法是配置 pom.xml,定义 为 spring-boot-start-parent.如下: <parent> <groupId>org ...
- zabbix 报表
摘自: https://www.w3cschool.cn/zabbix_manager/zabbix_manager-z45f1zie.html
- html中css的三种样式
在html中定义CSS样式的方法有三种,新建CSS文件 使用link 关联 这种是最常用的外部引用样式,第二种讲样式写在 head 头部里面 这种是页面样式 ,第三中样式直接写在行内 style里面 ...
- 使用mutt+msmtp做linux邮件客户端
下载MSMTP wget http://nchc.dl.sourceforge.net/sourceforge/msmtp/msmtp-1.4.17.tar.bz2 tar xvf msmtp-1 ...
- POJ-3680:Intervals (费用流)
You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task i ...
- 「HNOI2008」「LuoguP3197」越狱(数论
题目描述 原题来自:HNOI 2008 监狱有连续编号为 111 到 nnn 的 nnn 个房间,每个房间关押一个犯人.有 mmm 种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人信仰的宗教相同, ...