Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

  • 双指针

  • 数组

  • 字符串

    • 匹配
      • KMP
      • 28. 找出字符串中第一个匹配项的下标
      • 459. 重复的子字符串
      • 686. 重复叠加字符串匹配
      • 214. 最短回文串
    • 字符串模拟
    • 数字与字符串
  • 链表

  • 树

  • 回溯

  • 动态规划

  • 图

  • 二分查找

  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

  • LeetCode周赛

  • ACM模式输入输出
  • 数学

  • 数据结构与算法
  • 字符串
Nreal
2023-12-26
目录

匹配

# KMP

双指针:i(不后退),j(在最长前缀匹配表中找到次大回退);

案例:

a b a b a b z a b a b a b a
0 0 1 2 3 4 0 1 2 3 4 5 7 5(4+1)
1
2

最长前缀匹配表:

参考:如何更好地理解和掌握 KMP 算法? - 知乎 (zhihu.com) (opens new window)

int[] calculateMaxMathLengths(String pattern){
    int[] next = new int[pattern.length()];
    int j = 0;
    for(int i=1;i<pattern.length();i++){
        while(j>0 && pattern.charAt(j)!=pattern.charAt(i)){
            j = next[j-1];
        }
        if(pattern.charAt(j) == pattern.charAt(i)){
            j++;
        }
        next[i] = j;
    }
    return next;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

寻找所有匹配字符串的开头:

最浅显易懂的 KMP 算法讲解_哔哩哔哩_bilibili (opens new window)

List<Integer> search(String text,String pattern){
    List<Integer> ret = new ArrayList<>();
    int[] next = calculateMaxMathLengths(pattern);
    int j = 0;
    for(int i=0;i<text.length();i++){
        while(j>0 && pattern.charAt(j)!=text.charAt(i)){
            j = next[j-1];
        }
        if(pattern.charAt(j) == text.charAt(i)){
            j++;
        }
        if(j == pattern.length()){
            ret.add(i-pattern.length()+1);
            j = next[j-1];
        }
    }
    return ret;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 28. 找出字符串中第一个匹配项的下标 (opens new window)

class Solution {
    public int strStr(String haystack, String needle) {
        int[] next = calculateMaxMathLengths(needle);
        int j = 0;
        for(int i=0;i<haystack.length();i++){
            while(j>0 && needle.charAt(j)!=haystack.charAt(i)){
                j = next[j-1];
            }
            if(haystack.charAt(i)==needle.charAt(j)){
                j++;
            }
            if(j==needle.length()){
                return i-needle.length()+1;
            }
        }
        return -1;
    }
    int[] calculateMaxMathLengths(String pattern){
        int[] next = new int[pattern.length()];
        int j = 0;
        for(int i=1;i<pattern.length();i++){
            while(j>0 && pattern.charAt(j)!=pattern.charAt(i)){
                j = next[j-1];
            }
            if(pattern.charAt(j) == pattern.charAt(i)){
                j++;
            }
            next[i] = j;
        }
        return next;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 459. 重复的子字符串 (opens new window)

方法一:KMP


1

方法二:API

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        String ss = s+s;
        return ss.substring(1,ss.length()-1).contains(s);
    }
}
1
2
3
4
5
6

# 686. 重复叠加字符串匹配 (opens new window)

# 214. 最短回文串 (opens new window)

摩尔投票法
字符串模拟

← 摩尔投票法 字符串模拟→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式