[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 ...
随机推荐
- Django项目高频使用文件
数据库配置: MySQL数据库 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': 'localhost' ...
- Redis之java增删改查
jedis是java的redis客户端实现,要使用jedis须要加入jedis的maven依赖: <dependency> <groupId>redis.clients< ...
- AndroidUI组件之ImageSwitcher
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/gc_gongchao/article/details/25594669 今天继续AndroidUI组 ...
- nvl()与regexp_replace()
NVL(字段,0):将空值转化为0 regexp_replace(字段, ‘[1-9]‘ ,'') is not null; 将数字转化为0
- 常见C C++问题(转)
这一部分是C/C++程序员在面试的时候会被问到的一些题目的汇总.来源于基本笔试面试书籍,可能有一部分题比较老,但是这也算是基础中的基础,就归纳归纳放上来了.大牛们看到一笑而过就好,普通人看看要是能补上 ...
- 在linux 中卸载Mysql
一.通用的mysql卸载方式 1.查看系统中是否已经安装了mysql 命令:rpm -qa|grep -i mysql如果有显示msql的安装列表,代表已经安装了. 2.停止mysql服务.删除之前安 ...
- Java for LeetCode 101 Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 20145239 《Java程序设计》第9周学习总结
20145239 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC入门 JDBC简介 1.JDBC是java联机数据库的标准规范,它定义了一组标准类与接口,应用程序需要联机数 ...
- 一篇文章了解相见恨晚的 Android Binder 进程间通讯机制【转】
本文转载自:https://blog.csdn.net/freekiteyu/article/details/70082302 Android-Binder进程间通讯机制 概述 最近在学习Binder ...
- .net 常用的插件列表
1,.net 分布式Session 解决方案RedisSessionStateProvider 2,c# 表达式树查看工具 Expression Tree Visualizer 3,sqlbuilde ...