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 ...
随机推荐
- eclipse对Java程序的移植
有些Java项目可能不在同一台计算机上开发,所以程序需要平台间进行移植,方法很简单,首先有一个最简单的项目HelloJava 当我们开发完成或者要休息了,一般都会保存然后在项目上右击,选择Close ...
- HDU 2147 kiki's game(博弈)
kiki's game Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit S ...
- VS 高亮显示不带后缀的C++头文件
工具-选项-文本编辑器-文件扩展名-勾选“将无扩展名文件映射到(M)” Microsoft Visual C++
- css3学习总结6--CSS3字体
使用自己需要的字体 在新的 @font-face 规则中,您必须首先定义字体的名称(比如 myFirstFont),然后指向该字体文件. 如需为 HTML 元素使用字体,请通过 font-family ...
- 三种配置linux环境变量的方法(以java为例)
1.先确认是否为openjdk:参考 2. 修改/etc/profile文件 如果你的计算机仅仅作为开发使用时推荐使用这种方法,因为所有用户的shell都有权使用这些环境变量,可能会给系统带来安全性 ...
- opencv学习笔记(五)镜像对称
opencv学习笔记(五)镜像对称 设图像的宽度为width,长度为height.(x,y)为变换后的坐标,(x0,y0)为原图像的坐标. 水平镜像变换: 代码实现: #include <ios ...
- 什么是网络爬虫(Spider) 程序
Spider又叫WebCrawler或者Robot,是一个沿着链接漫游Web 文档集合的程序.它一般驻留在服务器上,通过给定的一些URL,利用HTTP等标准协议读取相应文档,然后以文档中包括的所有未访 ...
- js event 事件兼容浏览器 ie不需要 event参数 firefox 需要
js event 事件兼容浏览器 ie不需要 event参数 firefox 需要 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
- C# 常用正则表达式
// 匹配移动手机号 @"^1(3[4-9]|5[012789]|8[78])\d{8}$"; // 匹配电信手机号 @"^18[09]\d{8}$"; ...
- android.intent.action.MAIN 与 android.intent.category.LAUNCHER 的验证理解 (转)
原文地址:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 的验证理解 作者: 第一种情况:有MAIN,无LAUNCHER,程 ...