Leetcode | substr()
求子串当然最经典的就是KMP算法了。brute force算法在leetcode上貌似也有一些技巧。
brute force:
char* StrStr(const char *str, const char *target) {
if (!*target) return str;
char *p1 = (char*)str, *p2 = (char*)target;
char *p1Adv = (char*)str;
while (*++p2)
p1Adv++; // 这里相当于用这个指针控制外循环为N-M+1次
while (*p1Adv) {
char *p1Begin = p1;
p2 = (char*)target;
while (*p1 && *p2 && *p1 == *p2) {
p1++;
p2++;
}
if (!*p2)
return p1Begin;
p1 = p1Begin + ;
p1Adv++;
}
return NULL;
}
Knuth–Morris–Pratt算法:
algorithm kmp_search:
input:
an array of characters, S (the text to be searched)
an array of characters, W (the word sought)
output:
an integer (the zero-based position in S at which W is found) define variables:
an integer, m ← (the beginning of the current match in S)
an integer, i ← (the position of the current character in W)
an array of integers, T (the table, computed elsewhere) while m + i < length(S) do
if W[i] = S[m + i] then
if i = length(W) - then
return m
let i ← i +
else
let m ← m + i - T[i]
if T[i] > - then
let i ← T[i]
else
let i ← (if we reach here, we have searched all of S unsuccessfully)
return the length of S
当然关键在于求T这个数组,T[i]就相当于S[0:T[i]] = W[i - T[i], i]。
algorithm kmp_table:
input:
an array of characters, W (the word to be analyzed)
an array of integers, T (the table to be filled)
output:
nothing (but during operation, it populates the table) define variables:
an integer, pos ← (the current position we are computing in T)
an integer, cnd ← (the zero-based index in W of the next
character of the current candidate substring) (the first few values are fixed but different from what the algorithm
might suggest)
let T[] ← -, T[] ← while pos < length(W) do
(first case: the substring continues)
if W[pos - ] = W[cnd] then
let cnd ← cnd + , T[pos] ← cnd, pos ← pos + (second case: it doesn't, but we can fall back)
else if cnd > 0 then
let cnd ← T[cnd] (third case: we have run out of candidates. Note cnd = 0)
else
let T[pos] ← 0, pos ← pos + 1
Leetcode | substr()的更多相关文章
- LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2
https://leetcode.com/problems/longest-palindromic-substring/ manacher算法相关:http://blog.csdn.net/ywhor ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- [LeetCode] Concatenated Words 连接的单词
Given a list of words (without duplicates), please write a program that returns all concatenated wor ...
- [LeetCode] Encode String with Shortest Length 最短长度编码字符串
Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...
- [LeetCode] Repeated Substring Pattern 重复子字符串模式
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- [LeetCode] Ternary Expression Parser 三元表达式解析器
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LeetCode] Longest Absolute File Path 最长的绝对文件路径
Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...
- [LeetCode] Mini Parser 迷你解析器
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
随机推荐
- HDU1130 卡特兰数
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Servlet过滤器和监听器
1,Servlet过滤器 <filter> <filter-name>charset</filter-name> <filter-class>org.g ...
- 如何手动修改XP系统属性中的技术支持信息
\windows\system32目录下有个oeminof.ini,里面是OEM显示的文字信息,把相应项目修改即可,OEM图片使用的是本目录下的OEMlogo.bmp(图片:创建一个图形文件,像素尺寸 ...
- 分类and分类延展
1.Category简介 Category,又称为类别&类目&分类,是OC特有语法,在不修改原有类的基础上增加新的方法,一个庞大的类可以多人来分模块开发,有助于团队合作,或者对当前类方 ...
- obj-m
转自:http://blog.sina.com.cn/s/blog_693301190100sxoi.html obj-m (转帖) 目标定义是Kbuild Makefile的主要部分,也是核心部分. ...
- git branch -D 大写的D 删除分支
今天删除本地分支 git branch -d XXX 提示: the branch XXX is not fully merged 原因:XXX分支有没有合并到当前分支的内容 解决方法:使用大写的 ...
- gitlab+TortoiseGit中使用SSH
1.在文件夹空白位置右键打开"Git Bash" 2.按 https://gitlab.yourhost.com/help/ssh/ssh.md 中的说明,输入命令 ssh-k ...
- Html的一点点收获
结束了牛腩,总结了自己的收获,我开始了征战HTML的计划,在看<提高班培养计划>的时候,我很诧异,因为<HTML孙鑫>这个项目竟然就只有一天的时间,怎么可以这样,但是,我还是决 ...
- 【HTML5】拖放(Drag 和 drop)
效果图: <!DOCTYPE HTML> <html> <head> <style type="text/css"> #div1 { ...
- 神奇的HTML5离线存储(应用程序缓存)
声明:本文为原创文章,如需转载,请注明来源并保留原文链接前端小尚,谢谢! 前言 使用 HTML5,通过创建 cache manifest 文件,可以轻松地创建 web 应用的离线版本. HTML5引入 ...