BF算法与KMP算法

using System;

namespace kmp
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static int[] next = new int[20];
        static int count = 0;
        static int kmp(char[] a, char[] b, int pos)
        {
            int i = pos, j = 0;
            int lena = a.Length, lenb = b.Length;
            count = 0;
            while ((i <= lena - 1) && (j <= lenb - 1))
            {
                if (a[i] == b[j])
                { ++i; ++j; }
                else
                { i = i - j + 1; j = 0; }
                count++;
            }
            if (j > lenb - 1)
                return i - lenb;
            else
                return 0;
        }
        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
            int pos = 0, reval;
            Console.WriteLine("input string1");
            string s1 = Console.ReadLine();
            Console.WriteLine("input string2");
            string s2 = Console.ReadLine();
            char[] p1 = s1.ToCharArray();
            char[] p2 = s2.ToCharArray();
            Console.WriteLine("lena={0},lenb={1}", p1.Length, p2.Length);
            reval = kmp(p1, p2, pos);
            Console.WriteLine("====not kmp ====");
            Console.WriteLine("value={0},count={1}", reval, count);
            get_next(p2, next);
            reval = index_kmp(p1, p2, pos);
            Console.WriteLine("====this is kmp ====");
            Console.WriteLine("value={0},count={1}", reval, count);
            get_goodnext(p2, next);
            reval = index_kmp(p1, p2, pos);
            Console.WriteLine("====this is good kmp ====");
            Console.WriteLine("value={0},count={1}", reval, count);
            Console.ReadLine();
        }
        static int index_kmp(char[] a, char[] b, int pos)
        {
            int i = pos, j = 0;
            int lena = a.Length, lenb = b.Length;
            count = 0;
            while ((i <= lena - 1) && (j <= lenb - 1))
            {
                if (j == 0 || a[i] == b[j])
                { ++i; ++j; }
                else
                { j = next[j]; }
                count++;
            }
            if (j > lenb - 1)
                return i - lenb;
            else
                return 0;
        }
        static void get_next(char[] a, int[] next)
        {
            int i = 1, j = 0;
            next[0] = 0;
            while (i <= a.Length - 2)
            {
                if (j == 0 || a[i] == a[j])
                { ++i; ++j; next[i] = j; }
                else
                { j = next[j]; }
            }
        }
        static void get_goodnext(char[] a, int[] next)
        {
            int i = 1, j = 0;
            next[0] = 0;
            while (i <= a.Length - 2)
            {
                ++i; ++j;
                if (j == 0 || a[i] == a[j])
                {
                    if (a[i] != a[j])
                    {
                        next[i] = j;
                    }
                    else
                    {
                        next[i] = next[j];
                    }
                }
                else
                { j = next[j]; }
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值