学习了几天数据结构,一直有种冲动想上机试试,可惜教程是C语言版的,对于我这个对C语言不是很熟悉的初学者就有点困难,于是就用C#模拟: using System;namespace EasyIndex{ class Index { static void Main(string[] args) { string s="acabaabcaabaabcac"; string m="abaabcac"; int i=0; int j=0; while(i<s.Length && j<m.Length) { if(s[i]==m[j]) { i++; j++; } else { i=i-j+2;//从主串的下一个位置开始重新匹配; j=1;//子串从头开始重新匹配; } } if(j>=m.Length)//说明m是s的一个子串,而且pos是i-m.Lengh+1,因为数组从0开始计数; { Console.WriteLine("m与s中第"+Convert.ToString(i-m.Length+1)+"个元素开始的子串相匹配!"); } else { Console.WriteLine("m不是s的子串!"); } } }} 转载于:https://www.cnblogs.com/gmq/archive/2006/09/26/515577.html