網(wǎng)站首頁 編程語言 正文
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 20
typedef struct String{
char ch[MAXSIZE];
int length;
}String;
void InitData(String str){
str.ch[MAXSIZE];
}
//KMP 未優(yōu)化
int Index(String str,String s){
int i=0,j=0;
while(i<str.length && j<s.length){
if(str.ch[i] == s.ch[j]){
++i,++j; //相等則前進
}else{
i=i-j+1;//i跟上次的多1
j=0;//j回到原點
}
if(j==s.length) return i - s.length;
}
return 0;
}
//-------------start----------
//獲取未優(yōu)化的next
void get_next(String s,int next[]){
int i=0,j=-1;
next[0]=-1;
while(i < s.length-1) {
if(j == -1 || s.ch[i] == s.ch[j]){
++i,++j;
next[i]=j;
}
else j=next[j];
}
}
//KMP 優(yōu)化1
int Index_KMP(String str,String s,int next[]){
int i=0,j=0;
while(i<str.length && j<s.length){
if(j == -1 || str.ch[i] == s.ch[j]){
++i,++j;
}else j=next[j];
if(j==s.length) return i - s.length;
}
return 0;
}
//-------------end----------
//-------------start---------
//獲取優(yōu)化的next --> nextval
void get_nextval(String s,int nextval[]){
int i=0,j=-1;
nextval[0]=-1;
while(i < s.length-1) {
if(j == -1 || s.ch[i] == s.ch[j]){
++i,++j;
if(s.ch[i]!=s.ch[j]) nextval[i]=j;
else nextval[i]=nextval[j];
}
else j=nextval[j];
}
}
//KMP 優(yōu)化2
int Index_KMP2(String str,String s,int nextval[]){
int i=0,j=0;
while(i<str.length && j<s.length){
if(j == -1 || str.ch[i] == s.ch[j]){
++i,++j;
}else j=nextval[j];
if(j==s.length) return i - s.length;
}
return 0;
}
//-------------end---------
int main()
{
String str;
InitData(str);
strcpy(str.ch,"googlcgoogleloogob");
str.length = strlen(str.ch);
String s;
InitData(s);
strcpy(s.ch,"google");
s.length = strlen(s.ch);
int idx=Index(str,s);
printf("【未優(yōu)化的結(jié)果,開始的下標為:】%d\n",idx);
//優(yōu)化1
int next[s.length]={0};
get_next(s,next);
// for(int i=0;i<s.length;i++){
// printf("%d\n",next[i]);
// }
int idx2=Index_KMP(str,s,next);
printf("【next未優(yōu)化的結(jié)果,開始的下標為:】%d\n",idx2);
//優(yōu)化2
int nextval[s.length]={0};
get_nextval(s,nextval);
// for(int i=0;i<s.length;i++){
// printf("%d\n",nextval[i]);
// }
int idx3=Index_KMP2(str,s,nextval);
printf("【next優(yōu)化的結(jié)果,開始的下標為:】%d\n",idx3);
return 0;
}
原文鏈接:https://blog.csdn.net/qq_44223394/article/details/125839987
相關(guān)推薦
- 2023-05-19 Flutter?枚舉值enum和int互相轉(zhuǎn)化總結(jié)_Android
- 2022-08-02 flask后端request獲取參數(shù)的幾種方式整理_python
- 2022-08-29 Python神器之Pampy模式匹配庫的用法詳解_python
- 2022-06-28 ASP.NET一次性對GridView批量更新多行數(shù)據(jù)_實用技巧
- 2022-06-04 Python?os和os.path模塊詳情_python
- 2022-05-07 Python關(guān)鍵字之global與nonlocal_python
- 2022-06-21 C語言超全面講解函數(shù)的使用方法上_C 語言
- 2023-11-17 在python中按不同的條件在列表list、字典dict和集合set中篩選想要的數(shù)據(jù)(以實際案例進行
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支