求子串当然最经典的就是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. 2.python基础深入(元组、字符串、列表、字典)

    一,对象与类 对象: python中一切皆为对象,所谓对象:我自己就是一个对象,我玩的电脑就是对象,玩的手机就是对象. 我们通过描述属性(特征)和行为来描述一个对象的. 在python中,一个对象的特 ...

  2. hdu 1879 继续畅通工程 解题报告

    题目链接:http://code.hdu.edu.cn/showproblem.php?pid=1879 这条题目我的做法与解决Constructing Roads的解法是相同的. 0 表示没有连通: ...

  3. Android 如何让EditText不自动获取焦点

    解决之道:在EditText的父级控件中找一个,设置成 android:focusable="true"     android:focusableInTouchMode=&quo ...

  4. 修改iptables防火墙规则解决vsftp登录后不显示文件目录的问题

    如果设置防火墙开端口可能只是常用的几个端口,这样很可能导vsftpd在被动模式时无法启动随机端口,从而造成客户端的FTP无法列出目录这样胡问题.解决方式很简单,给 vsftpd增加随机端口范围,然后把 ...

  5. 在Android中将子View的坐标转换为父View的坐标

    在Android中,我们有时候可能会将子View的坐标转换为父View中的坐标.感觉很有用,分享给大家. 在Launcher中有这么一段代码可以完成这项工作.  public float getDes ...

  6. Oracle 11g 卸载

    1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlS ...

  7. linux下php增加curl扩展,生成curl.so文件

    进入php源代码目录 cd /php5.6.9/ext/curl 执行生成so文件编译模式 /usr/local/php/bin/phpize 编译curl扩展 ./configure --with- ...

  8. Android VLC播放器二次开发1——程序结构分析

    最近因为一个新项目需要一个多媒体播放器,所以需要做个视频.音频.图片方面的播放器.也查阅了不少这方面的资料,如果要从头做一个播放器工作量太大了,而且难度也很大.所以最后选择了VLC作为基础,进行二次开 ...

  9. ORA-00824:cannot set SGA_TARGET or MEMORY_TARGET due to existing internal settings

    练习时执行一条修改数据库连接数的语句: alter system set processes=1 scope=spfile; 然后关闭数据库: shutdown 再启动数据库时,出现异常,报错信息如下 ...

  10. poj 2486( 树形dp)

    题目链接:http://poj.org/problem?id=2486 思路:经典的树形dp,想了好久的状态转移.dp[i][j][0]表示从i出发走了j步最后没有回到i,dp[i][j][1]表示从 ...