求子串当然最经典的就是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()的更多相关文章

  1. LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2

    https://leetcode.com/problems/longest-palindromic-substring/ manacher算法相关:http://blog.csdn.net/ywhor ...

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

  3. [LeetCode] Concatenated Words 连接的单词

    Given a list of words (without duplicates), please write a program that returns all concatenated wor ...

  4. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  5. [LeetCode] Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  6. [LeetCode] Ternary Expression Parser 三元表达式解析器

    Given a string representing arbitrarily nested ternary expressions, calculate the result of the expr ...

  7. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  8. [LeetCode] Longest Absolute File Path 最长的绝对文件路径

    Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsub ...

  9. [LeetCode] Mini Parser 迷你解析器

    Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...

随机推荐

  1. php开发网站编码统一问题

    一个良好的网站代码整洁,注释适当是最基本的,也是好的习惯,这可以避免以后的非常乱了自己感觉都乱,一旦重构麻烦就大了耗时耗力,其中网站整个体系的编码是最重要的一个方面,为了网站的稳定性建议php程序,H ...

  2. 6.python模块(导入,内置,自定义,开源)

    一.模块 1.模块简介 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用python标准库的方法. 类似于函数式编程和面向过 ...

  3. 记VS2013并行编译导致出错的解决过程

    接前一篇,电脑换了新的,系统是64bit的win8系统,先安装了SQLServer2012,再安装VS2010旗舰版,Stop!为什么还是2010?因为2010太经典了,以至于公司的项目还在用它写项目 ...

  4. Java Hour 50 日期类型

    Plan List: 1 Java 中的日期类型 2 mysql 相关 3 java code style 鉴于本问题太过普通,所以参考文章满大街都是,因此本文内容基本为转载和验证. java.sql ...

  5. 在getView方法产生给用户item的视图以及数据

    在getView方法产生给用户item的视图以及数据

  6. OD hit跟踪 run跟踪使用问题

    刚学习OD不久,现在使用HIT跟踪 run跟踪功能,在我的程序里碰到点问题,还请赐教 选了一部分代码添加到HIT跟踪,在选的代码处设置断点,程序运行到断点,按单步跟踪,当执行到第二个PUSH时,程序就 ...

  7. Android 编程下短信监听在小米手机中失效的解决办法

    相信很多人写的短信监听应用在小米手机上是拦截不到短信的,这是因为小米对短信的处置权优先分给了系统.我们可以在短信的[设置]→[高级设置]→[系统短信优先]中发现短信的优先处理权默认是分给系统的,只要关 ...

  8. js:数据结构笔记12--排序算法(2)

    高级排序算法:(处理大数据:百万以上) 希尔排序:是插入排序的优化版: 首先设置间隔数组,然后按照每个间隔,分别进行排序: 如第一个间隔为5,首先a[5]与a[0]进行插入排序;然后a[6]和a[0] ...

  9. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  10. ZOJ2930 The Worst Schedule(最小割)

    题目大概说有n个任务,每个任务可以提前或推迟,提前或推迟各有一定的费用,有的任务一旦推迟另一个任务也必须推迟,问怎么安排任务使花费最少,且最少花费的条件下提前的任务数最多能多少. 问题就是要把各个任务 ...