定义串的基本操作
#include<iostream>
#include<cstring>
#include<stdlib.h>
using namespace std;
#define MAXLEN 255
typedef struct{
char ch[MAXLEN];
int length;
}SString;
typedef struct {
char *ch;
int length;
}HString;
void InitSting(HString &S);
bool StrAssign(HString &T,char* chars);
bool StrCopy(HString &T,HString S);
bool StrEmpty(HString S);
int StrCompare(HString S,HString T);
int StrLength(HString S);
bool SubString(HString &Sub,HString S,HString pos,HString len);
bool Concat(HString &T,HString S1,HString S2);
int Index(HString S,HString T);
bool ClearString(HString &S);
bool DestoryString(HString &S);
void InitSting(HString &S){
S.ch = (char *)malloc(MAXLEN*sizeof(char));
S.length = 0;
}
bool StrAssign(HString &T,char* chars){
T.length = 0;
for(int i=1;chars[i];i++){
T.ch[i] = chars[i];
T.length++;
}
return true;
}
bool StrCopy(HString &T,HString S){
T = S;
return true;
}
bool StrEmpty(HString S){
if(S.length == 0) return true;
return false;
}
int StrCompare(HString S,HString T){
int i=1;
while(i<S.length&&i<T.length){
if(S.ch[i]!=T.ch[i]) return S.ch[i]-T.ch[i];
i++;
}
return S.length-T.length;
}
int StrLength(HString S){
return S.length;
}
bool SubString(HString &Sub,HString S,int pos,int len){
if(pos+len-1>S.length) return false;
for(int i=1;i<=len;i++){
Sub.ch[i] = S.ch[pos+i-1];
}
Sub.ch[len+1] = '\0';
Sub.length=len;
return true;
}
bool Concat(HString &T,HString S1,HString S2){
for(int i=1;i<=S1.length;i++){
T.ch[i] = S1.ch[i];
}
for(int i=1;i<=S2.length;i++){
T.ch[i+S1.length] = S2.ch[i];
}
T.length = S1.length+S2.length;
return true;
}
int Index(HString S,HString T){
int i=1,n=StrLength(S),m=StrLength(T);
HString sub;
InitSting(sub);
while(i<n-m+1){
SubString(sub,S,i,m);
if(StrCompare(T,sub)!=0) i++;
else return i;
}
return 0;
}
bool ClearString(HString &S){
S.length = 0;
return true;
}
bool DestoryString(HString &S){
free(S.ch);
S.length = 0;
}
void test(){
HString s,t;
InitSting(s);
InitSting(t);
char *sr =" 123456";
char *tr =" 345";
StrAssign(s,sr);
StrAssign(t,tr);
printf("%d\n",Index(s,t));
}
int main(){
HString s,t;
InitSting(s);
InitSting(t);
char *sr =" 123456";
char *tr =" 345";
StrAssign(s,sr);
StrAssign(t,tr);
printf("%d\n",Index(s,t));
system("pause");
return 0;
}
KMP算法实现(串的唯一重点)
#include<iostream>
#include<cstring>
#include<cstdio>
const int maxn = 1e3+10;
char t[maxn];
char s[maxn];
int next[maxn];
int nextval[maxn];
void get_next(char *t,int next[]){
int i=1,j=0;
int len = strlen(t+1);
next[1]=0;
while(i<=len){
if(j==0||t[i]==t[j]){
i++;
j++;
next[i] = j;
}else{
j = next[j];
}
}
}
void get_nextval(char *t,int next[]){
int i=1,j=0;
int len = strlen(t+1);
nextval[1]=0;
while(i<=len){
if(j==0||t[i]==t[j]){
i++;
j++;
if(t[i]!=t[j]){
next[i] = j;
}else{
next[i] = next[j];
}
}else{
j = next[j];
}
}
}
int KMP(char *s,char *t,int next[]){
int lens = strlen(s+1);
int lent = strlen(t+1);
int i=1,j=1;
while(i<=lens&&j<=lent){
if(j == 0||s[i] == t[j]){
i++;
j++;
}else{
j = next[j];
}
}
if(j>lent){
return i - j + 1;
}else{
return 0;
}
}
int main(){
char *s=" aaabaaaab";
char *t=" aaaab";
get_next(t,next);
printf("%d\n",KMP(s,t,next));
get_nextval(t,next);
printf("%d\n",KMP(s,t,next));
system("pause");
return 0;
}
|