【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 题目要求: 给定一 ...
随机推荐
- 安装ThinkPHP
ThinkPHP5的环境要求如下: PHP >= 5.4.0 PDO PHP Extension MBstring PHP Extension CURL PHP Extension 严格来说,T ...
- 2017 ACM-ICPC乌鲁木齐网络赛 B. Out-out-control cars(计算几何 直线相交)
题目描述 Two out-of-control cars crashed within about a half-hour Wednesday afternoon on Deer Park Avenu ...
- 【LeetCode 42】接雨水
题目链接 [题解] 考虑每个位置它最后能接多少单位的水. 显然就是这个min(位置左边最高的位置,位置右边最高的位置)-当前这个位置的高度. 这就是这个位置最后水上涨的高度. 两个边界注意是不会储水的 ...
- Delphi读取和写入utf-8编码格式的文件
读取UTF-8格式的文件内容 function LoadUTF8File(AFileName: string): string; var ffileStream:TFileStream; fAnsiB ...
- ACM中java的使用 (转)
ACM中java的使用 这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入: 格式为:Scanner ...
- 如何设置Windows操作系统打印机与xlpd连接
Xlpd是Xmanager中负责远程打印的软件,除了打印远程文件,它还具备很多功能,本集将具体讲解Xlpd的主要功能. 主要功能如下: 1. 支持LPD协议(RFC1179) 在RFC1179中定义 ...
- 封装token
let baseApiUrl = 'xxxx' var app = getApp(); let defaultHeaders = { 'Content-Type': 'application/json ...
- 【读书笔记】:MIT线性代数(1):Linear Combinations
1. Linear Combination Two linear operations of vectors: Linear combination: 2.Geometric Explaination ...
- WireShark 自带工具 editcap 和 text2pcap 配合完成改包操作
一.拆包 首先声明这种方法比较复杂而且需要点技术水平,不建议菜鸟尝试(可以使用WireEdit编辑pcap包,不过要联网)其实在熟练这种方法后也可以很快的,但这种方法主要还是方便吧,不用下载其他什么软 ...
- subprocess 模块 与 re 模块
sub :子 process:进程 用法: import subprocess while True: cmd_str = inport('请输入终端命令:') obj = subprocrss.Po ...