我自己写的,有错误,请帮忙修改一下
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();
        }
    }}

解决方案 »

  1.   

    http://blog.chinaitlab.com/user1/295958/archives/2005/30347.html
      

  2.   

    求到正确的next函数值
    断点调试怎么搞?
      

  3.   

    搞定了,大家帮忙测试一下可不可以
    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 i = 0;
                int j = 0;
                while (i < next.Length - 1)
                {
                    if (j == 0 || a[i] == a[j - 1])
                    {
                        ++i;
                        ++j;
                        if (a[i] != a[j - 1])
                            next[i] = j;
                        else
                            next[i] = next[j - 1];
                    }
                    else
                        j = next[j - 1];
                }            return next;
            }
            public static int KMP(string zc, string ppc)
            {
                int[] next = Program.GetNext(ppc);
                int i = 0; int j = 0;
                while (i < zc.Length && j < ppc.Length)
                {
                    if (j == 0 || zc[i] == ppc[j - 1])
                    {
                        ++i;
                        ++j;
                    }
                    else
                        j = next[j - 1];
                }
                if (j >= ppc.Length)
                    return i - ppc.Length + 1;//匹配的话返回startindex
                else
                    return -1;//不匹配的话返回-1
            }
            static void Main(string[] args)
            {
                string s = "六章护";
                string b = "第一百四十六章护花倾情";
                Console.WriteLine(Program.KMP(b, s).ToString());
                Console.WriteLine(b.Substring(Program.KMP(b, s), s.Length));
                Console.ReadLine();
            }
        }}