做我的世界壁纸的网站,怎样上传图片到wordpress,短网址批量在线生成,企业年金有必要交吗6-2 堆串的基本操作StrReplace(S, T, V)
编写算法#xff0c;实现堆串的基本操作StrReplace(S, T, V)。
初始条件: 串S, T和 V 均已存在,且 V 是非空串。
操作结果: 用V替换主串S中出现的所有与(模式串)T相等的不重叠的子串。输入格式:
第一行#xff1a;S
第二行#…6-2 堆串的基本操作StrReplace(S, T, V)
编写算法实现堆串的基本操作StrReplace(S, T, V)。
初始条件: 串S, T和 V 均已存在,且 V 是非空串。
操作结果: 用V替换主串S中出现的所有与(模式串)T相等的不重叠的子串。输入格式:
第一行S
第二行T
第三行V
输出格式:
S 被替换后的结果
函数接口定义
void StrReplace(HString *S, HString T, HString V);
堆串类型定义如下
typedef struct
{char *ch;int len;
}HString;裁判测试程序样例
#includestdio.h
#include stdlib.h
#include string.htypedef struct{char *ch;int len;
}HString;void StrReplace(HString *S, HString T, HString V);/*串初始化函数*/
void StrInit(HString *s)
{s-chNULL;s-len0;
}/*串赋值函数将字符串常量tval的值赋给串s */
int StrAssign(HString *s, char *tval)
{ int len,i0;if (s-ch!NULL) free(s-ch);while (tval[i]!\0) i;leni;if (len){s-ch(char *)malloc(len);if (s-chNULL) return(0); for (i0;ilen;i)s-ch[i]tval[i];}else s-chNULL;s-lenlen;return(1);
}/*串插入函数在串s中下标为pos的字符之前插入串t */
int StrInsert(HString *s, int pos, HString t){int i; char *temp;if (pos0 || poss-len || s-len0)return(0);temp(char *)malloc(s-len t.len);if (tempNULL) return(0);for (i0;ipos;i)temp[i]s-ch[i];for (i0;it.len;i)temp[ipos]t.ch[i];for (ipos;is-len;i)temp[i t.len]s-ch[i];s-lent.len;free(s-ch);s-chtemp;return(1);
} /*串删除函数在串s中删除从下标pos起len个字符 */
int StrDelete(HString *s, int pos, int len) {int i; char *temp;if (pos0 || pos(s-len - len))return(0);temp(char *)malloc(s-len - len);if (tempNULL) return(0);for (i0;ipos;i)temp[i]s-ch[i];for (ipos;is-len - len;i)temp[i]s-ch[ilen];s-lens-len-len;free(s-ch);s-chtemp;return(1);
}/*串的简单模式匹配求串t在串s中的位置*/
int StrIndex(HString *s, int pos, HString t){int i,j,start;if (t.len0) return(0);startpos; istart; j0;while (is-len jt.len){if (s-ch[i]t.ch[j]) {i;j;}else {start; istart; j0;}}if (jt.len) return(start);else return(-1);
} void main()
{HString s, t, v;char str1[100],str2[100],str3[100];int i;gets(str1); StrInit(s); StrAssign(s, str1);gets(str2); StrInit(t); StrAssign(t, str2);gets(str3); StrInit(v); StrAssign(v, str3);StrReplace(s, t, v);printf(S );for(i0;is.len;i)printf(%c, s.ch[i]);printf(\n);
}/* 请在这里填写答案 */
输入样例
在这里给出一组输入。例如
abcaabcaaabca
bca
x输出样例
在这里给出相应的输出。例如
S axaxaax
void StrReplace(HString *S,HString T,HString V){int index0;while((indexStrIndex(S,index,T))!-1){StrDelete(S,index,T.len);StrInsert(S,index,V);indexV.len;}
}