kmp算法
我自己写的,有错误,请帮忙修改一下
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication22
{
class Program
{
public static int[] GetNext(string a)
{
int[] next = new int[a.Length];
next[0] = 0;
int j = 0;
int i = 1;
while (i < a.Length - 1)
{
if (j == 0 || a[i] == a[j])
{
++i;
++j;
if (a[i] != a[j])
{
next[i] = j+1;
}
else
{
next[i] = next[j];
}
}
else
{
j = next[j];
}
}
return next;
}
static void Main(string[] args)
{
string s = "abaabc";
foreach (int i in Program.GetNext(s))
Console.WriteLine(i.ToString());
Console.ReadLine();
}
}
}