【LeetCode 30】串联所有单词的子串
【题解】
开个字典树记录下所有的单词。
然后注意题目的已知条件
每个单词的长度都是一样的。
这就说明不会出现某个字符串是另外一个字符串的前缀的情况(除非相同).
所以可以贪心地匹配(遇到什么字符就在字典树里面沿着边从根往下走就好).
假设给的单词的个数为len.(每个单词的长度都是L)
显然从每个位置开始都要匹配len次。而且每次都要匹配L个字符。
然后因为可能会出现一个单词出现多次的情况。
那么你得记录一下之前某个单词用了多少次。
【代码】
class Solution {
public:
int tree[100000][26+5],tot;
int tag[100000];
int used[100000];
vector<int> visits;
vector<int> findSubstring(string s, vector<string>& words) {
vector<int> ans;ans.clear();
if (s=="" || words.empty()) return ans;
memset(tree,0,sizeof tree);
memset(tag,0,sizeof tag);
tot = 1;
int lendic = words.size();
for (int i = 0;i < lendic;i++){
if (i>0 && ((int)words[i].size()!=(int)words[0].size())) return ans;
string ts = words[i];
int len2 = ts.size();
int p = 1;
for (int j = 0;j < len2;j++){
if (tree[p][ts[j]-'a']==0){
tree[p][ts[j]-'a'] = ++tot;
}
p = tree[p][ts[j]-'a'];
}
tag[p]++;
}
int L = words[0].size();
int len = s.size();
for (int i = 0;i < len;i++){
bool ok = true;
visits.clear();
for (int j = 0;j < lendic;j++){//要匹配字典里面的单词个数次
int p = 1;
for (int k = i+j*L;k < i+(j+1)*L;k++){//每次匹配的长度是固定的
if (k>=len){
ok = false;
break;
}
if (tree[p][s[k]-'a']==0){
ok = false;
break;
}
p = tree[p][s[k]-'a'];
}
if (!ok) break;
if (tag[p]==0) { ok=false;break;}//如果字典里没有这个单词就停止
if (used[p]==0) visits.push_back(p);//记录一下哪些位置用到了,之后清0用
used[p]++;
if (used[p]>tag[p]){//如果用的次数超过单词的个数了,则不行。
ok = false;
break;
}
}
for (int x:visits) used[x]=0;
if (ok) ans.push_back(i);
}
return ans;
}
};
【LeetCode 30】串联所有单词的子串的更多相关文章
- Java实现 LeetCode 30 串联所有单词的子串
30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...
- [LeetCode] 30. 串联所有单词的子串
题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...
- Leetcode 30 串联所有单词的子串 滑动窗口+map
见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...
- [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- [LeetCode] Substring with Concatenation of All Words 串联所有单词的子串
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】
[030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...
- Leetcode 30.与所有单词相关联的子串
与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...
- [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)
30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...
- Leetcode——30.与所有单词相关联的字串【##】
@author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一 ...
随机推荐
- Juint test Case 的2种使用方式
通常情况下,我们去测试一个类中的方法,首先是建立一个包,包中建立一个测试类,在建立测试类文件时,选择JUnit Test Case,如下: 建好之后写测试用例: 但是如果偏就想在编写方法的那个java ...
- html插入链接的实现方法
下面就为大家带来一篇html插入链接的实现方法.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 每个网页都已一个地址,用URL()标识,通常在网站内部创建链接时,通常 ...
- JS中算法之检索算法(查找算法)
顺序查找 查找指定值 function seqSearch(arr, data) { for (var i = 0; i < arr.length; ++i) { if (arr[i] == d ...
- React学习笔记-生命周期函数
定义: 生命周期函数指在某一个时刻组件会自动调用执行的函数
- Delphi 自带了 Base64 编解码的单元
Delphi 自带了 Base64 编解码的单元,叫 EncdDecd,这名字很拗口而且不直观,估计这是一直很少人关注和知道的原因. 这个单元提供两套四个公开函数: 对流的编解码: procedu ...
- Docker问题方案收集
1.问题: Unable to connect to unix:///var/run/docker.sock (Permission denied) from PHP code 解决方法: Make ...
- Nuget-Doc:Nuget 简介
ylbtech-Nuget-Doc:Nuget 简介 1.返回顶部 1. NuGet 简介 2019/05/24 适用于任何现代开发平台的基本工具可充当一种机制,通过这种机制,开发人员可以创建.共享和 ...
- 10. Jmeter-后置处理器一
jmeter-后置处理器介绍与使用一 今天我们先讲 CSS/JQuery Extractor JSON Extractor Boundary Extractor 正则表达式提取器 CSS/JQuery ...
- Unity中文本AssetBundle的解析
Unity在其安装目录下提供了许多实用的小工具,就存放在unity安装目录下的:Editor/Data/Tools, 其中该路径下的WebExtract.ext和binary2text.exe可以用 ...
- Cocos2d-x之MessageBox
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 在cocos2d-x中使用的对话框为MessageBox,因为cocos2d-x已经包装好了:但是在一般的游戏设置中,我们不会使用coco ...