头文件、宏定义及串的基本操作
1 #include<string.h> 2 #include<ctype.h> 3 #include<malloc.h> 4 #include<stdio.h> 5 #include<stdlib.h> 6 #include<math.h> 7 8 #define TRUE 1 9 #define FALSE 0 10 #define OK 1 11 #define ERROR 0 12 #define INFEASIBLE -1 13 #define MAXSTRLEN 40 14 15 16 typedef int Status; 17 typedef int Boolean; 18 typedef char SString[MAXSTRLEN+1];//0号单元存放串的长度 19 20 //串的各种操作 21 Status StrAssign(SString T,char *chars) 22 {//生成一个其值等于chars的串T 23 int i; 24 if(strlen(chars)>MAXSTRLEN) 25 return ERROR; 26 else 27 { 28 T[0]=strlen(chars); 29 for(i=1;i<=T[0];i++) 30 T[i]=*(chars+i-1); 31 return OK; 32 } 33 } 34 35 36 Status StrCopy(SString T,SString S) 37 {//有串S复制得串T 38 int i; 39 for(i=0;i<=S[0];i++) 40 T[i]=S[i]; 41 return OK; 42 } 43 44 int StrEmpty(SString S) 45 {//若串S为空,则返回TRUE,否则返回FALSE 46 if(S[0]==0) 47 return TRUE; 48 else 49 return FALSE; 50 } 51 52 int StrCompare(SString S,SString T) 53 {//串S和T均存在,若S>T,则返回值>0;若S=T,则返回值=0,若S<T,则返回值<0 54 int i; 55 for(i=1;i<=S[0]&&i<=T[0];i++) 56 if(S[i]!=T[i]) 57 return S[i]-T[i]; 58 return S[0]-T[0]; 59 } 60 61 int StrLength(SString S) 62 {//返回串的元素个数 63 return S[0]; 64 } 65 66 Status ClearString(SString S) 67 {//将串S清为空串 68 S[0]=0; 69 return OK; 70 } 71 Status Concat(SString T,SString S1,SString S2) 72 { 73 int i; 74 if(S1[0]+S2[0]<=MAXSTRLEN) 75 {//未截断 76 for(i=1;i<=S1[0];i++) 77 T[i]=S1[i]; 78 for(i=1;i<=S2[0];i++) 79 T[S1[0]+i]=S2[i]; 80 T[0]=S1[0]+S2[0]; 81 return TRUE; 82 } 83 else 84 { 85 for(i=1;i<=S1[0];i++) 86 T[i]=S1[i]; 87 for(i=1;i<=MAXSTRLEN-S1[0];i++) 88 T[S1[0]+i]=S2[i]; 89 T[0]=MAXSTRLEN; 90 return FALSE; 91 } 92 } 93 94 95 Status SubString(SString Sub,SString S,int pos,int len) 96 {//用sub返回串S的第pos个字符起长度为len 的字串。 97 int i; 98 if(pos<1||pos>S[0]||len<0||len>S[0]-pos+1) 99 return ERROR; 100 for(i=1;i<=len;i++) 101 Sub[i]=S[pos+i-1]; 102 Sub[0]=len; 103 return OK; 104 } 105 106 107 int Index(SString S,SString T,int pos) 108 { 109 //返回字串T在主串中第pos个字符之后的位置,若不存在,则函数指为0 110 int i,j; 111 if(1<=pos&&pos<=S[0]) 112 { 113 i=pos; 114 j=1; 115 while(i<=S[0]&&j<=T[0]) 116 if(S[i]==T[j])//继续比较后继字符 117 { 118 ++i; 119 ++j; 120 } 121 else//指针后退重新开始匹配 122 { 123 i=i-j+2; 124 j=1; 125 126 } 127 if(j>T[0]) 128 return i-T[0]; 129 else 130 return 0; 131 } 132 } 133 134 Status StrInsert(SString S,int pos,SString T) 135 { /* 初始条件: 串S和T存在,1≤pos≤StrLength(S)+1 */ 136 /* 操作结果: 在串S的第pos个字符之前插入串T。完全插入返回TRUE,部分插入返回FALSE */ 137 int i; 138 if(pos<1||pos>S[0]+1) 139 return ERROR; 140 if(S[0]+T[0]<=MAXSTRLEN) 141 { /* 完全插入 */ 142 for(i=S[0];i>=pos;i--) 143 S[i+T[0]]=S[i]; 144 for(i=pos;i<pos+T[0];i++) 145 S[i]=T[i-pos+1]; 146 S[0]=S[0]+T[0]; 147 return TRUE; 148 } 149 else 150 { /* 部分插入 */ 151 for(i=MAXSTRLEN;i<=pos;i--) 152 S[i]=S[i-T[0]]; 153 for(i=pos;i<pos+T[0];i++) 154 S[i]=T[i-pos+1]; 155 S[0]=MAXSTRLEN; 156 return FALSE; 157 } 158 } 159 160 Status StrDelete(SString S,int pos,int len) 161 { /* 初始条件: 串S存在,1≤pos≤StrLength(S)-len+1 */ 162 /* 操作结果: 从串S中删除第pos个字符起长度为len的子串 */ 163 int i; 164 if(pos<1||pos>S[0]-len+1||len<0) 165 return ERROR; 166 for(i=pos+len;i<=S[0];i++) 167 S[i-len]=S[i]; 168 S[0]-=len; 169 return OK; 170 } 171 172 Status Replace(SString S,SString T,SString V) 173 { /* 初始条件: 串S,T和V存在,T是非空串(此函数与串的存储结构无关) */ 174 /* 操作结果: 用V替换主串S中出现的所有与T相等的不重叠的子串 */ 175 int i=1; /* 从串S的第一个字符起查找串T */ 176 if(StrEmpty(T)) /* T是空串 */ 177 return ERROR; 178 do 179 { 180 i=Index(S,T,i); /* 结果i为从上一个i之后找到的子串T的位置 */ 181 if(i) /* 串S中存在串T */ 182 { 183 StrDelete(S,i,StrLength(T)); /* 删除该串T */ 184 StrInsert(S,i,V); /* 在原串T的位置插入串V */ 185 i+=StrLength(V); /* 在插入的串V后面继续查找串T */ 186 } 187 }while(i); 188 return OK; 189 } 190 191 void DestroyString() 192 { /* 由于SString是定长类型,无法销毁 */ 193 } 194 195 void StrPrint(SString T) 196 { /* 输出字符串T。另加 */ 197 int i; 198 for(i=1;i<=T[0];i++) 199 printf("%c",T[i]); 200 printf("\n"); 201 }
模式匹配的主要代码,有关串模式匹配的理解说明:请点我
1 void get_next(SString T,int next[]) 2 { 3 //求模式串T的next函数值并存入数组next 4 int i=1,j=0; 5 while(i<T[0]) 6 if(j==0||T[i]==T[j]) 7 { 8 ++i; 9 ++j; 10 next[i]=j; 11 } 12 else 13 j=next[j]; 14 } 15 16 int Index_KMP(SString S,SString T,int pos,int next[]) 17 { 18 //利用模式串T的next函数求T在主串S中第Pos个字符之后的位置的KMP算法 19 int i=pos,j=1; 20 while(i<=S[0]&&j<=T[0]) 21 if(j==0||S[i]==T[j]) 22 { 23 ++i; 24 ++j; 25 } 26 else 27 j=next[j]; 28 if(j>T[0]) 29 return i-T[0]; 30 else 31 return 0; 32 33 34 } 35 36 37 void main() 38 { 39 int i,j,*p; 40 SString s1,s2; 41 StrAssign(s1,"acabaabaabcacaabc"); 42 printf("主串为: "); 43 StrPrint(s1); 44 StrAssign(s2,"abaabcac"); 45 printf("子串为: "); 46 StrPrint(s2); 47 i=StrLength(s2); 48 p=(int*)malloc((i+1)*sizeof(int)); /* 生成s2的next数组 */ 49 get_next(s2,p); 50 printf("子串的next函数为: "); 51 for(j=1;j<=i;j++) 52 printf("%d ",*(p+j)); 53 printf("\n"); 54 i=Index_KMP(s1,s2,1,p); 55 if(i) 56 printf("主串和子串在第%d个字符处首次匹配\n",i); 57 else 58 printf("主串和子串匹配不成功\n"); 59 }
浙公网安备 33010602011771号