https://oj.leetcode.com/problems/substring-with-concatenation-of-all-words/

找S中子串,每个元素都在T中出现了,且所有T中元素都在S子串中出现了

class Solution {
public:
vector<int> findSubstring(string S, vector<string> &L) {
vector<int> ans;
if(S.empty() || S.size() == || L.empty() || L.size() == )
return ans; // L is longer
int len = L.size() * L[].size();
if(len > S.size())
return ans; unordered_map<string,int> dictL;
for(int i = ; i< L.size(); i++)
dictL[L[i]] += ; unordered_map<string,int> _dict;
for(int i = ; i + len <= S.size(); i++)
{
string subS = S.substr(i,len); _dict.clear();
bool flag = true; for(int j = ; j < len; j+= L[].size())
{
string subLittle = subS.substr(j,L[].size());
if(dictL.find(subLittle) == dictL.end()) //不属于目标里面的
{
flag = false;
break;
} _dict[subLittle] += ;
if(_dict[subLittle] > dictL[subLittle])
{
flag = false;
break;
}
}
if(flag)
ans.push_back(i); }
return ans;
}
};

超时、超时、超时、超时……

教训就是,有时候超时不是算法的问题,而是实现的不好。具体看下一篇博客

LeetCode OJ-- Substring with Concatenation of All Words ***的更多相关文章

  1. [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  2. leetCode 30.Substring with Concatenation of All Words (words中全部子串相连) 解题思路和方法

    Substring with Concatenation of All Words You are given a string, s, and a list of words, words, tha ...

  3. 【leetcode】Substring with Concatenation of All Words

    Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...

  4. LeetCode - 30. Substring with Concatenation of All Words

    30. Substring with Concatenation of All Words Problem's Link --------------------------------------- ...

  5. LeetCode 030 Substring with Concatenation of All Words

    题目要求:Substring with Concatenation of All Words You are given a string, S, and a list of words, L, th ...

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

  7. 【leetcode】Substring with Concatenation of All Words (hard) ★

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  8. Java for LeetCode 030 Substring with Concatenation of All Words【HARD】

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  9. Java [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 a ...

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

随机推荐

  1. 动态规划:HDU1712-ACboy needs your help(分组背包问题)

    ACboy needs your help Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Othe ...

  2. 后缀数组的使用心得——POJ2774 最长连续公共子串

    对于这道题,将两个字符串直接合并成为一个字符串,分别记录连个字符串结束的位置. 首先,应用黑暗圣典的模板,我们可以顺利得到height,rank,sa三个数组. 之后直接扫描1-n所有的位置,选出来一 ...

  3. 关于IDEA 单元测试时 【empty test suite】异常的分析!!

    IDEA功能很强大,配置很操蛋,自从用了之后掉了很多坑!!! 这几天要用单元测试,方法完好但是就是一直报empty test suite ,WTF,类找不到 在网上反复的找答案都没有合适 静下心想想, ...

  4. TCP/IP网络编程之套接字的多种可选项

    套接字可选项进而I/O缓冲大小 我们进行套接字编程时往往只关注数据通信,而忽略了套接字具有的不同特性.但是,理解这些特性并根据实际需要进行更改也十分重要.之前我们写的程序在创建好套接字后都是未经特别操 ...

  5. 洛谷P1553数字反转升级版

    题目链接:https://www.luogu.org/problemnew/show/P1553

  6. synchronized 基本用法

    常见三种使用方式 1)普通同步方法,锁是当前实例:2)静态同步方法,锁是当前类的Class实例,Class数据存在永久代中,是该类的一个全局锁:3)对于同步代码块,锁是synchronized括号里配 ...

  7. python 中单例模式

    1.什么是单例模式: 单例模式是指一个类有且只有一个实例对象,创建一个实例对象后,再创建实例是返回上一次的对象引用.(简单的讲就是两个实例对象的ID相同,节省了内存空间) 2.单例模式的创建: 举例创 ...

  8. Dos中查看mysql数据时 中文乱码

    使用jsp页面查看数据时可以正确显示中文,但是dos窗口查看数据时中文显示乱码. 上网查了一下原因:之所以会显示乱码,就是因为MySQL客户端输出窗口显示中文时使用的字符编码不对造成的,可以使用如下的 ...

  9. 【转】lightmap

    Shader "Diffuse Lightmap" { Properties { _MainTex ("Texture 1", 2D) = "whit ...

  10. 静态分析:IDA逆向代码段说明 text、idata、rdata、data

    静态分析:IDA逆向代码段说明 text.idata.rdata.data 通常IDA对一个PE文件逆向出来的代码中, 存在四个最基本的段text.idata.rdata.data, 四个段为PE文件 ...