串基本操作的實現
阿新 • • 發佈:2018-12-24
BF演算法
#include<iostream> //輸入字串用gets函式,故i,j從0開始 #include<cstring> #define OK 1 #define ERROR -1 #define Status int #define MAXSIZE 255 using namespace std; typedef struct //定長儲存 { char ch[MAXSIZE+1]; //儲存串的一維陣列 int length; //串的當前長度 }SString; //BF演算法 int Index_BF(SString S,SString T,int pos) {//返回模式T在主串S中第pos個字元開始第一次出現的位置。若不存在,則返回值為0. //其中,T非空,0<=pos<S.length int i=pos; int j=0; //初始化 while(i<S.length && j<T.length ) //兩個串均未比較到串尾 { if( S.ch [i] == T.ch [j] ) //繼續比較後繼字元 { ++i; ++j; } else //指標後退開始重新匹配 { i=i-j+1; j=0; } } if(j+1>T.length ) //匹配成功 return i-T.length+1 ; else //匹配失敗 return 0; } int main() { int pos=-1; int n1,n2; SString S,T; cout<<"\t\t\t請輸入字串S: "; gets(S.ch ); cout<<"\t\t\t請輸入字串T: "; gets(T.ch ); cout<<endl; S.length =strlen(S.ch ); T.length =strlen(T.ch ); //cout<<S.length <<" and "<<T.length <<endl; for(int i=0; i<S.length ; i++) { if(S.ch [i] == T.ch [0]) { pos = i; break; } } cout<<"\t\t*——————————————————————————————————————————*"<<endl; cout<<"\t\t| 若不匹配輸出0,否則輸出和模式T中第pos(1<=pos<=T.length)個字元在主串S中的序號 |"<<endl; cout<<"\t\t*——————————————————————————————————————————*"<<endl<<endl; if(pos == -1) { cout<<"\t\t\t匹配結果:0"<<endl; } else { cout<<"\t\t\t匹配結果:"<<Index_BF( S, T, pos); } return 0; }
KMP演算法
#include<iostream> //輸入字串用gets函式,故i,j及next[]從0開始 #include<cstring> #define OK 1 #define ERROR -1 #define OVERFLOW -1 #define Status int #define MAXSIZE 255 using namespace std; int next[255]; int nextval[255]; typedef struct //定長儲存 { char ch[MAXSIZE+1]; //儲存串的一維陣列 int length; //串的當前長度 }SString; //KMP演算法 int Index_KMP(SString S, SString T, int pos) { int i = pos, j = 0; while (i < S.length && j < T.length) //兩個串均未比較到串尾 { if (j == -1 || S.ch[i] == T.ch[j]) //繼續比較後繼字元 { ++i; ++j; } else //模式串向右移動 j = next[j]; // j = nextval[j]; //next修正值 } if (j+1 > T.length) //匹配成功 return i - T.length+1 ; else return 0; //匹配失敗 } void get_next(SString T) {//求模式串T的next函式值並存入陣列next int i=0,j=0; next[0]=-1; j=next[i]; while(i<T.length ) { if(j==-1 || T.ch[i]==T.ch[j]) { ++i; ++j; next[i]=j; } else j=next[j]; } } // next修正值 /* void get_nextval(SString T) {//求模式串T的next函式值並存入陣列next int i=0,j=0; nextval[0]=-1; j=nextval[i]; while(i<T.length ) { if(j==-1 || T.ch[i]==T.ch[j]) { ++i; ++j; if(T.ch[i]!=T.ch[j]) nextval[i]=j; else nextval[i]=nextval[j]; } else j=nextval[j]; } } */ int main() { int pos=-1; int n1,n2; SString S,T; cout<<"\t\t\t請輸入字串S: "; gets(S.ch ); cout<<"\t\t\t請輸入字串T: "; gets(T.ch ); cout<<endl; S.length = strlen( S.ch ); T.length = strlen( T.ch ); //cout<<S.length <<" and "<<T.length <<endl; for(int i=0; i<S.length ; i++) { if(S.ch [i] == T.ch [0]) { pos = i; break; } } cout<<"\t\t*——————————————————————————————————————————*"<<endl; cout<<"\t\t| 若不匹配輸出0,否則輸出和模式T中第pos(1<=pos<=T.length)個字元在主串S中的序號 |"<<endl; cout<<"\t\t*——————————————————————————————————————————*"<<endl; cout<<endl; get_next( T ); // get_nextval( T ); //next修正值 if(pos == -1) { cout<<"\t\t\t匹配結果:0"<<endl; } else { cout<<"\t\t\t匹配結果:"<<Index_KMP( S, T, pos ); } return 0; }