char[] dataas = new char[1000000]; for (int t = 0; t < 1000000; t++) { if (t == 999999) { dataas[t] = '1'; } else { dataas[t] = '0'; } } char[] searchhs = new char[100]; for (int t = 0; t < 100; t++) { if (t == 99) { searchhs[t] = '1'; } else { searchhs[t] = '0'; } } StringList datas = new StringList(dataas); StringList search = new StringList(searchhs); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); int res = datas.Index(search); stopwatch.Stop(); Console.WriteLine("结果:" + res + " KMP:" + stopwatch.ElapsedMilliseconds); stopwatch.Reset(); stopwatch.Start(); res = datas.SimpleIndex(search); stopwatch.Stop(); Console.WriteLine("结果:" + res + " Normal:" + stopwatch.ElapsedMilliseconds);
主串是999999个0加个1,匹配串是99个0和一个1,也就是要匹配到最后才能匹配到
单位ms
如果匹配串的0再长点,普通匹配时间会大幅增长。而KMP不会。
KMP的时间复杂度为O(i+j)