题目链接

【题解】

开个字典树记录下所有的单词。
然后注意题目的已知条件
每个单词的长度都是一样的。
这就说明不会出现某个字符串是另外一个字符串的前缀的情况(除非相同).
所以可以贪心地匹配(遇到什么字符就在字典树里面沿着边从根往下走就好).
假设给的单词的个数为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】串联所有单词的子串的更多相关文章

  1. Java实现 LeetCode 30 串联所有单词的子串

    30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...

  2. [LeetCode] 30. 串联所有单词的子串

    题目链接: https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/ 题目描述: 给定一个字符串 s 和一 ...

  3. Leetcode 30 串联所有单词的子串 滑动窗口+map

    见注释.滑动窗口还是好用. class Solution { public: vector<int> findSubstring(string s, vector<string> ...

  4. [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 ...

  5. [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 ...

  6. 【LeetCode-面试算法经典-Java实现】【030-Substring with Concatenation of All Words(串联全部单词的子串)】

    [030-Substring with Concatenation of All Words(串联全部单词的子串)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Yo ...

  7. Leetcode 30.与所有单词相关联的子串

    与所有单词相关联的字串 给定一个字符串 s 和一些长度相同的单词 words.在 s 中找出可以恰好串联 words 中所有单词的子串的起始位置. 注意子串要与 words 中的单词完全匹配,中间不能 ...

  8. [leetcode] 30. 与所有单词相关联的字串(cn第653位做出此题的人~)

    30. 与所有单词相关联的字串 这个题做了大概两个小时左右把...严重怀疑leetcode的judge机器有问题.同样的代码交出来不同的运行时长,能不能A题还得看运气? 大致思路是,给words生成一 ...

  9. Leetcode——30.与所有单词相关联的字串【##】

    @author: ZZQ @software: PyCharm @file: leetcode30_findSubstring.py @time: 2018/11/20 19:14 题目要求: 给定一 ...

随机推荐

  1. Apache Flink 进阶(六):Flink 作业执行深度解析

    本文根据 Apache Flink 系列直播课程整理而成,由 Apache Flink Contributor.网易云音乐实时计算平台研发工程师岳猛分享.主要分享内容为 Flink Job 执行作业的 ...

  2. Equivalent Prefixes

    题目链接 题意:给你两个数组a,b,大小为n,让你寻找一个数p (1<= p <= n) ,使之在 1~p 任意一个区间中a,b数组的最小值下标相同. 思路:看到用线段树去写的我也是服了. ...

  3. JS-for..of

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of 刚刚上网上看到<V8 ...

  4. 显示等待WebDriverWait+EC

    参考:https://www.cnblogs.com/yoyoketang/p/6538505.html 百度搜索关键字,等待搜索结果页面显示完成后,验证搜索结果的第一条记录 通过WebDriverW ...

  5. GHCi Prelude学习

    参考:http://www.cse.unsw.edu.au/~en1000/haskell/inbuilt.html http://www.cse.unsw.edu.au/~en1000/haskel ...

  6. Numpy基础之创建与属性

    import numpy as np ''' 1.数组的创建:np.array([]) 2.数组对象的类型:type() 3.数据类型:a.dtype 4.数组的型shape:(4,2,3) 5.定义 ...

  7. C语言|博客作业12

    一.我学到的内容(整理本课程所学,[用思维导图的方式] 二.我的收获(包括我完成的所有作业的链接+收获)不能只有作业链接,没有收获 作业链接 收获 https://edu.cnblogs.com/ca ...

  8. bootstrap3- 导航条 - 慕课笔记

    bootstrap中的导航条 一.和导航的区别 导航条比导航多了一个条字 直接上图 导航: 导航条: 简单文字描述: 由两张图看出,导航内容比较简单,而导航条可以包含导航及其他元素,如表单,搜索框等, ...

  9. Vue中src属性绑定的问题

    地址:https://blog.csdn.net/qq_25479327/article/details/80082520 地址:https://blog.csdn.net/sinat_3655513 ...

  10. c# Winform dev控件之ChartControl

    1.改变颜色 字体颜色 背景颜色 XYDiagram dia = chartControl1.Diagram as XYDiagram; dia.AxisX.Label.TextColor = Col ...