[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 ...
随机推荐
- python——进程池
from concurrent.futures import ProcessPoolExecutor import os,random def func(name): print("%s吃了 ...
- systemclock sleep 睡眠
- netstat参数记录
可以使用man netstat查看TCP的各种状态信息描述 ESTABLISHED socket已经建立连接 CLOSED socket没有被使用,无连接 CL ...
- css(4)
类选择器和id选择器都有父子选择器. 在css文件中国,有时候为了简化样式,可以把相同的样式拿出来放在一起. display:inline display:block 行内元素里只能放行内元素,而块内 ...
- 分布式锁的实现方式——ACID数据库、缓存或者是zk
针对分布式锁的实现,目前比较常用的有以下几种方案: 基于数据库实现分布式锁 基于缓存(redis,memcached,tair)实现分布式锁 基于Zookeeper实现分布式锁 在分析这几种实现方案之 ...
- openfire性能测试
使用TSung对Jabber服务器openfire进行压力测试 http://blog.csdn.net/spider_zhcl/article/details/6073920 Tsung负载测试Ti ...
- 清除float浮动三种方式
Float的作用? w3c的官方解释: Content flows down the right side of a left-floated box and down the left side o ...
- mongdb启动问题
问题:Detected unclean shutdown - /data/db/mongod.lock is not empty. old lock file:/data/db/mongod.lock ...
- 最短路径 一 Dijkstra 模板(O(n^2))
题目传送:http://hihocoder.com/problemset/problem/1081 #include<iostream> #include<cstdio> #i ...
- idea 调试技巧1
1 多线程调试 开发过多线程应用的朋友应该有体会,有些时候,为了观察多个线程间变量的不同状态,以及锁的获取等,就会想到在代码里加个断点debug一下. 在IDE里断点停下来的时候,可以切换到另外的线程 ...