【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 题目要求: 给定一 ...
随机推荐
- Ext js-01 -helloworld
一.下载ext: 登陆这个网址 https://www.sencha.com/products/evaluate/ 下载下来解压后如下:安装cmd程序 二.开始helloworld 新建一个idea ...
- 【HDOJ6578】Blank(DP)
题意:一个长为n的序列,每个位置上的值是0,1,2,3中的一个,有m个限制条件,限制位置[l[i],r[i]]中不同的数值有x[i]个,问方案数MOD 998244353 n<=100,m< ...
- 前端每日实战:99# 视频演示如何用纯 CSS 创作一个过山车 loader
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/KBxYZg/ 可交互视频 此视频是 ...
- LintCode之合并排序数组
题目描述: 我的代码: public class Solution { /* * @param A: sorted integer array A * @param B: sorted integer ...
- mysql联合查询sql优化
我们在使用mysql数据库时,经常会使用到mysql的联合查询,联合查询分为内连接和外连接,内连接查询结果是联合的表都存在匹配才会有结果,外连接则根据驱动表是否存在匹配来生成结果集. 这里使用mysq ...
- jenkins实现手动选择分支构建项目-Git Paramater
先下载插件: Git Paramater 参照: jenkins-参数化构建(三)插件:Git Parameter https://www.cnblogs.com/zhaojingyu/p/98624 ...
- HBuilder的一些常用快捷键
Alt + [ 匹配括号 Alt + ↓跳转到下一个可编辑区Ctrl + Alt + j 合并下一行Ctrl + Alt + ←选择助手Ctrl + 回车 换行Ctrl + Shift + 回车 向上 ...
- 《JavaScript DOM 编程艺术》学习成果
(在线演示地址)[http://thqy39.github.io/works/03.Js%20DOM%20website/index.html]
- JQuery判断radio(单选框)是否选中和获取选中值
一.设置选中方法 代码如下: $("input[name='名字']").get(0).checked=true; $("input[name='名字']"). ...
- python 装饰器 第十一步:多层装饰器的嵌套
#第十一步:多层装饰器的嵌套 #装饰器1 def kuozhan1(func): #定义装饰之后的函数 def neweat1(): # 扩展功能1 print('1-----饭前洗手') # 调用基 ...