子串判断

题目描述

现有一个小写英文字母组成的字符串s和一个包含较短小写英文字符串的数组p,请设计一个高效算法,对于p中的每一个较短字符串,判断其是否为s的子串。

给定一个string数组p和它的大小n,同时给定string s,为母串,请返回一个bool数组,每个元素代表p中的对应字符串是否为s的子串。保证p中的串长度小于等于8,且p中的串的个数小于等于500,同时保证s的长度小于等于1000。

测试样例:
["a","b","c","d"],4,"abc"
返回:[true,true,true,false]


后缀数组?二分查找!

 class Substr {
public:
bool matched(string a, string b) {
if (b.length() < a.length()) return false;
for (int i = ; i < a.length(); ++i) if (a[i] != b[i]) return false;
return true;
}
vector<bool> chkSubStr(vector<string> p, int n, string s) {
// write code here
set<string> st;
vector<bool> res;
for (int i = ; i < s.length(); ++i) {
st.insert(s.substr(i));
}
for (int i = ; i < n; ++i) {
auto it = st.lower_bound(p[i]);
if (it == st.end()) {
res.push_back(false);
} else {
if (matched(p[i], *it)) res.push_back(true);
else res.push_back(false);
}
}
return res;
}
};

[CTCI] 子串判断的更多相关文章

  1. [google面试CTCI] 1-8.判断子字符串

    [字符串与数组] Q:Assume you have a method isSubstring which checks if one word is a substring of another G ...

  2. HDU 5510 Bazinga (2015沈阳现场赛,子串判断)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. [google面试CTCI] 1-4.判断两个字符串是否由相同字符组成

    [字符串与数组] Q:Write a method to decide if two strings are anagrams or not 题目:写一个算法来判断两个字符串是否为换位字符串.(换位字 ...

  4. Python判断一个字符串中是否存在多个子串中的一个

    在使用python的开发过程中,常常需要判断,字符串中是否存在子串的问题, 但判断一个字符串中是否存在多个字串中的一个时,如if (a or b) in c或者if x contains a|b|c| ...

  5. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  6. LOJ103 子串查找

    题意 这是一道模板题. 给定一个字符串 A 和一个字符串 B ,求 B 在 A 中的出现次数.A 和 B 中的字符均为英语大写字母或小写字母. A 中不同位置出现的 B 可重叠. 分析 参照jklov ...

  7. 转载:LeetCode:5Longest Palindromic Substring 最长回文子串

    本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...

  8. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  9. CodeForces 628B New Skateboard

    New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. excel文件批量重命名

    1.创建bat文件 2.在文件内输入以下格式的内容并保存,注意期间有空格 ren 1.txt 0011.txt     ren 2.txt 0021.txt     ren 3.txt 0031.tx ...

  2. linux常用命令全拼

    命令缩写: pwd:print work directory 打印当前目录 显示出当前工作目录的绝对路径 ps: process status(进程状态,类似于windows的任务管理器) 常用参数: ...

  3. JavaScript的NaN-唯一 一个自己不等于自己的对象!!

    JavaScript的NaN为什么不等于NaN 在JS中 Object === Object 感觉没有任何问题 这两个都代表的一个东西 但是如果你试过 NaN === NaN 是返回false为什么呢 ...

  4. iOS12 Network框架 自签名证书认证

    发布时间:2018-09-21   技术:iOS12 xcode10 golang1.11   概述 iOS12 苹果发布了新的网络框架Network,可以更方便地操作底层网络通信了.使用TLS也很方 ...

  5. 使用std::find_if提取序列容器的子串

    一个需求是这样的,一个vector容器中,我需要提取满足一定条件的元素的序列.就比如,一个树形结构,我把该接口拍扁成vector容器,每个节点都有一个惟一ID. 以下就是根据特定的ID查找节点下的子节 ...

  6. Docker运行python容器

    容器是镜像运行的实例,而镜像保存在仓库里,测试或者发布生产环境只需要pull下来即可,相对传统的应用部署,能很好的保持环境的一致,节省运维时间.最近公司内部的java和.net服务也已经全部容器化,实 ...

  7. Ubuntu 13.04下安装WPS for Linux

    [日期:2013-06-03]   有人说Linux下不是有open office 和libre office么?是啊,可是将windows下的doc文档或者ppt放到Libreoffice上打开的时 ...

  8. iOS 常用的几个math函数

    1.取整数 double ceil (double); 取上整 double floor (double); 取下整 2.绝对值 double fabs (double);求绝对值 double ca ...

  9. Xshell连接Ubuntu失败问题

    转自:https://www.linuxidc.com/Linux/2017-08/146222.htm Xshell是一个安全终端模拟软件,可以进行远程登录.我使用XShell的主要目的是在Wind ...

  10. numpy二分查找

    a = np.array([1, 2, 2, 3]) print(np.searchsorted(a, 0)) # 0 print(np.searchsorted(a, 1)) # 0 print(n ...