数据结构 串(顺序存储)的基本操作

举报
悦来客栈的老板 发表于 2020/12/30 01:47:49 2020/12/30
【摘要】 #include <stdio.h> #define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSTRLEN 255 typedef unsigned char String[MAXSTRLEN+1]; int StrAssign(String &S, char *str){ i...

  
  1. #include <stdio.h>
  2. #define OK 1
  3. #define ERROR 0
  4. #define TRUE 1
  5. #define FALSE 0
  6. #define MAXSTRLEN 255
  7. typedef unsigned char String[MAXSTRLEN+1];
  8. int StrAssign(String &S, char *str)
  9. {
  10. int i,len;
  11. char *p;
  12. for (len = 0, p = str; *p != '\0'; len++,p++);
  13. if (len == 0)
  14. {
  15. S[0] = 0;
  16. }
  17. else
  18. {
  19. for (i=0; i<len;i++)
  20. {
  21. S[i+1] = str[i];
  22. }
  23. S[0] = len;
  24. }
  25. return OK;
  26. }
  27. void StrCopy(String &S,String T)
  28. {
  29. if (T[0] == 0)
  30. {
  31. S[0] = 0;
  32. }
  33. else
  34. {
  35. int i;
  36. for (i=1;i<=T[0]; i++)
  37. {
  38. S[i] = T[i];
  39. }
  40. S[0] = T[0];
  41. }
  42. }
  43. int StrEmpty(String T)
  44. {
  45. if (T[0] == 0)
  46. {
  47. return OK;
  48. }
  49. return ERROR;
  50. }
  51. int StrLength(String T)
  52. {
  53. return T[0];
  54. }
  55. void ClearString(String &T)
  56. {
  57. T[0] = 0;
  58. }
  59. int StrCompare(String S,String T)
  60. {
  61. int i;
  62. for (i=1; i<=S[0] && i<=T[0]; ++i)
  63. {
  64. if (S[i] != T[i])
  65. {
  66. return S[i] - T[i];
  67. }
  68. }
  69. return S[0] - T[0];
  70. }
  71. int Concat(String &T,String S1,String S2)
  72. {
  73. int uncut;
  74. int i,j;
  75. if (S1[0] + S2[0] <= MAXSTRLEN)
  76. {
  77. for (i=1; i<=S1[0]; i++)
  78. {
  79. T[i] = S1[i];
  80. }
  81. for (j=1;j<=S2[0];j++)
  82. {
  83. T[i+j-1] = S2[j];
  84. }
  85. T[0] = S1[0] + S2[0];
  86. uncut = TRUE;
  87. }
  88. else if (S1[0] < MAXSTRLEN)
  89. {
  90. for (i=1; i<=S1[0]; i++)
  91. {
  92. T[i] = S1[i];
  93. }
  94. for (j=1; j<=MAXSTRLEN - S1[0]; j++)
  95. {
  96. T[i+j-1] = S2[j];
  97. }
  98. T[0] = MAXSTRLEN;
  99. uncut = FALSE;
  100. }
  101. else
  102. {
  103. for (i=0; i<=MAXSTRLEN; i++)
  104. {
  105. T[i] = S1[i];
  106. }
  107. uncut = FALSE;
  108. }
  109. return uncut;
  110. }
  111. void Print_String(String S)
  112. {
  113. if (S[0] == 0)
  114. {
  115. return;
  116. }
  117. else
  118. {
  119. int i;
  120. for (i=1; i<=S[0]; i++)
  121. {
  122. printf("%c",S[i]);
  123. }
  124. printf("\n");
  125. }
  126. }
  127. int SubString(String &Sub, String S, int pos, int len)
  128. {
  129. int i;
  130. if (pos < 1 || pos > S[0] || len < 0 || len > S[0] - pos + 1)
  131. {
  132. return ERROR;
  133. }
  134. if (!len)
  135. {
  136. Sub[0] = 0;
  137. }
  138. else
  139. {
  140. for (i=1; i<=len;i++)
  141. {
  142. Sub[i] = S[pos+i-1];
  143. }
  144. Sub[0] = len;
  145. }
  146. return OK;
  147. }
  148. int Index(String S,String T, int pos)
  149. {
  150. if (pos > 0 && pos <= S[0] - T[0] + 1)
  151. {
  152. int n = S[0];
  153. int m = T[0];
  154. int i = pos;
  155. String sub;
  156. while (i<= n-m+1)
  157. {
  158. SubString(sub,S, i,m);
  159. if (StrCompare(sub, T) != 0)
  160. {
  161. ++i;
  162. }
  163. else
  164. {
  165. return i;
  166. }
  167. }
  168. }
  169. return 0;
  170. }
  171. void get_next(String T, int next[])
  172. {
  173. int i = 1;
  174. int j = 0;
  175. next[1] = 0;
  176. while (i<T[0])
  177. {
  178. if (j == 0 || T[i] == T[j])
  179. {
  180. ++i;
  181. ++j;
  182. next[i] = j;
  183. }
  184. else
  185. {
  186. j = next[j];
  187. }
  188. }
  189. }
  190. void get_nextval(String T, int nextval[])
  191. {
  192. int i = 1;
  193. int j = 0;
  194. nextval[1] = 0;
  195. while (i<T[0])
  196. {
  197. if (j == 0 || T[i] == T[j])
  198. {
  199. ++i;
  200. ++j;
  201. if (T[i] != T[j])
  202. {
  203. nextval[i] = j;
  204. }
  205. else
  206. {
  207. nextval[i] = nextval[j];
  208. }
  209. }
  210. else
  211. {
  212. j = nextval[j];
  213. }
  214. }
  215. }
  216. int Index_KMP(String S, String T, int pos)
  217. {
  218. int i = pos;
  219. int j = 1;
  220. int next[255];
  221. get_next(T,next);
  222. while (i<=S[0] && j <= T[0])
  223. {
  224. if (j == 0 || S[i] == T[j])
  225. {
  226. ++i;
  227. ++j;
  228. }
  229. else
  230. {
  231. j = next[j];
  232. }
  233. }
  234. if (j>T[0])
  235. {
  236. return i-T[0];
  237. }
  238. else
  239. return 0;
  240. }
  241. int Index_KMP2(String S, String T, int pos)
  242. {
  243. int i = pos;
  244. int j = 1;
  245. int nextval[255];
  246. get_nextval(T,nextval);
  247. while (i<=S[0] && j <= T[0])
  248. {
  249. if (j == 0 || S[i] == T[j])
  250. {
  251. ++i;
  252. ++j;
  253. }
  254. else
  255. {
  256. j = nextval[j];
  257. }
  258. }
  259. if (j>T[0])
  260. {
  261. return i-T[0];
  262. }
  263. else
  264. return 0;
  265. }
  266. int main()
  267. {
  268. String T,S,H;
  269. char str[1000];
  270. gets(str);
  271. StrAssign(T,str);
  272. Print_String(T);
  273. gets(str);
  274. StrAssign(S,str);
  275. Print_String(S);
  276. StrCopy(T,S);
  277. Print_String(T);
  278. Concat(H,S,T);
  279. Print_String(H);
  280. SubString(S,T,5,3);
  281. Print_String(S);
  282. printf("%d\n",Index(T,S,1));
  283. printf("%d\n",Index_KMP(T,S,1));
  284. printf("%d\n",Index_KMP2(T,S,1));
  285. return 0;
  286. }

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/17249169

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。