[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 ...
随机推荐
- 用于string对象中字符截取的几种函数总结——语法、参数意义及用途举例
1. charAt():返回指定位置的字符. 语法:stringObject.charAt(index) 参数意义:index 必需,指字符在字符串中的下标.需要注意的是,字符串中第一个字符的下标是 ...
- 为什么Java中的字符串是不可变的?
原文链接:https://www.programcreek.com/2013/04/why-string-is-immutable-in-java/ java字符串是不可变的.不可变类只是一个不能修改 ...
- Extjs — Grid数据导出成Excel
最近因为项目问题,需要解决Extjs导出成Excel的问题. 下面简单描述这个问题解决的步骤如下: 1.先在js文件里写了一个button的handler事件,通过点击按钮,来实现调用ExportEx ...
- linux复制和移动
复制: -f 强制覆盖同名文件 -r 按递归方式保留原目录结构复制文件 cp -Rf /home/user1/* /root/temp/ 将/home/user1目录下的所有东西拷到/root ...
- callback机制之内核通知链表【转】
本文转载自:http://bbs.chinaunix.net/thread-2011776-1-1.html 1.通知链表简介 大多数内核子系统都是相互独立的,因此某个子系统可能对其它子系统产生 ...
- Codeforces Round #551 (Div. 2) A~E题解
突然发现上一场没有写,那就补补吧 本来这场应该5题的,结果一念之差E fail了 A. Serval and Bus 基本数学不解释,假如你没有+1 -1真的不好意思见人了 #include<c ...
- CodeForces - 540B School Marks —— 贪心
题目链接:https://vjudge.net/contest/226823#problem/B Little Vova studies programming in an elite school. ...
- ActiveMQ 了解
Active MQ 是JMS的一个具体实现,所以首先要对JMS有所了解. 1. JMS: 定义:Java消息服务(Java Message Service),是Sun是提出来的为J2EE提 ...
- DenseNet算法详解——思路就是highway,DneseNet在训练时十分消耗内存
论文笔记:Densely Connected Convolutional Networks(DenseNet模型详解) 2017年09月28日 11:58:49 阅读数:1814 [ 转载自http: ...
- 1131 Subway Map(30 分)
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...