網站首頁 編程語言 正文
#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 未優化
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----------
//獲取未優化的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 優化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---------
//獲取優化的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 優化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("【未優化的結果,開始的下標為:】%d\n",idx);
//優化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未優化的結果,開始的下標為:】%d\n",idx2);
//優化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優化的結果,開始的下標為:】%d\n",idx3);
return 0;
}
原文鏈接:https://blog.csdn.net/qq_44223394/article/details/125839987
相關推薦
- 2022-02-13 使用filter過濾器計算數組中符合條件的長度
- 2022-11-17 C++11中異常處理機制詳解_C 語言
- 2022-03-15 spark-submit hive SQL standards based authorizati
- 2023-04-13 react 打包優化,配置生產環境不輸出console.log
- 2021-12-02 Spring?Boot?分層打包?Docker?鏡像實踐及分析(推薦)_docker
- 2022-04-20 Xamarin渲染器移植到.NET?MAUI項目中_實用技巧
- 2022-07-01 Python查詢缺失值的4種方法總結_python
- 2022-10-03 python接口自動化使用requests庫發送http請求_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支