一、串
![image-20210727005353732](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005353.png)
定义
![image-20210727005406740](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005406.png)
串V.S线性表
![image-20210727005523561](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005523.png)
基本操作
![image-20210727005607107](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005607.png)
比较操作
![image-20210727005635750](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005635.png)
字符集编码
![image-20210727005701467](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005701.png)
总结
![image-20210727005742561](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005742.png)
二、存储结构
![image-20210727005758794](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005758.png)
顺序存储与堆分配存储
![image-20210727005855765](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005855.png)
![image-20210727005924608](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005924.png)
方案二的字符串长度不可以超过255
块链存储
![image-20210727005953465](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727005953.png)
基本操作的实现
![image-20210727010016555](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010016.png)
![image-20210727010029230](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010029.png)
![image-20210727010038986](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010039.png)
![image-20210727010051797](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010051.png)
![image-20210727010105881](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010105.png)
三、模式匹配
![image-20200803220247955](https://cdn.jsdelivr.net/gh/KimYangOfCat/MyPicStorage/2021-CSPostgraduate-408/20200810012517.jpg)
朴素模式匹配算法
![image-20200803221038452](https://cdn.jsdelivr.net/gh/KimYangOfCat/MyPicStorage/2021-CSPostgraduate-408/20200810012522.jpg)
![image-20200803224601338](https://cdn.jsdelivr.net/gh/KimYangOfCat/MyPicStorage/2021-CSPostgraduate-408/20200810012527.jpg)
![image-20200803231428235](https://cdn.jsdelivr.net/gh/KimYangOfCat/MyPicStorage/2021-CSPostgraduate-408/20200810012623.jpg)
性能分析
最好时间复杂度
![image-20200803231735244](https://cdn.jsdelivr.net/gh/KimYangOfCat/MyPicStorage/2021-CSPostgraduate-408/20200810012632.jpg)
最坏时间复杂度
![image-20200803232029485](https://cdn.jsdelivr.net/gh/KimYangOfCat/MyPicStorage/2021-CSPostgraduate-408/20200810012636.jpg)
思想总结
四、KMP 算法
简单模式匹配的缺点
![image-20210727010646197](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010646.png)
改进思想
![image-20210727010709758](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010709.png)
情况一
![image-20210727010730593](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010730.png)
![image-20210727010746569](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010746.png)
![image-20210727010759979](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010800.png)
情况二
![image-20200804224917143](https://cdn.jsdelivr.net/gh/KimYangOfCat/MyPicStorage/2021-CSPostgraduate-408/20200810012945.jpg)
情况三:
![image-20210727010926790](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010926.png)
4这里有可能多比较一次:因为4和6已经不等,但是1和6相同,这里仍会期待1和6相同,再进行一次比较,所以这里它并不是最优的算法,但是相比于朴素模式匹配,它还是少对比了两次
![image-20210727010937811](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010937.png)
同理j指向3时少对比1次
总结:
![image-20210727010955129](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010955.png)
具体代码实现
![image-20210727011012022](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011012.png)
求next数组
![image-20210727011116270](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011116.png)
![image-20210727011214643](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011214.png)
![image-20210727011236014](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011236.png)
代码实现求next数组
![image-20210727011246482](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011246.png)
回顾总结
![image-20210727011258409](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011258.png)
四、KMP 算法
简单模式匹配的缺点
![image-20210727010646197](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010646.png)
改进思想
![image-20210727010709758](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010709.png)
情况一
![image-20210727010730593](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010730.png)
![image-20210727010746569](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010746.png)
![image-20210727010759979](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010800.png)
情况二
![image-20200804224917143](https://cdn.jsdelivr.net/gh/KimYangOfCat/MyPicStorage/2021-CSPostgraduate-408/20200810012945.jpg)
情况三:
![image-20210727010926790](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010926.png)
4这里有可能多比较一次:因为4和6已经不等,但是1和6相同,这里仍会期待1和6相同,再进行一次比较,所以这里它并不是最优的算法,但是相比于朴素模式匹配,它还是少对比了两次
![image-20210727010937811](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010937.png)
同理j指向3时少对比1次
总结:
![image-20210727010955129](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727010955.png)
具体代码实现
![image-20210727011012022](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011012.png)
求next数组
![image-20210727011116270](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011116.png)
![image-20210727011214643](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011214.png)
![image-20210727011236014](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011236.png)
代码实现求next数组
![image-20210727011246482](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011246.png)
回顾总结
![image-20210727011258409](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011258.png)
五、KMP 算法的优化
KMP算法存在的问题
![image-20210727011317097](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011317.png)
例子2:
![image-20210727011359659](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011359.png)
总结
![image-20210727011418148](https://raw.githubusercontent.com/wushuai2000/PicGo/main/img/20210727011418.png)