使用C# 优化KMP字符串匹配算法

字符串匹配在现实生活中有着广泛的应用,DNA匹配,情报检索,信息查找等.在字符串匹配算法中BM 算法,经过事实验证是最高效算法.不过它也是最抽象的算法.由于本人水平有限,只能写一些KMP的优化,希望大家海涵.具体示例代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class KMPMatchString
    {
        /// <summary>
        /// 优化的KMP字符串匹配算法
        /// </summary>
        /// <param name=”pad”>要匹配的字符串</param>
        /// <param name=”patd”>目标字符串</param>
        /// <param name=”present”>返回匹配成功百分率</param>
        /// <returns> 返回一个目标字符串的索引 -1 表示失败</returns>
        static int YouHua_KMP(string pad, string patd, ref double present)
        {
            present = 0.0;
            pad = pad.Trim();
            char[] C_pad = pad.ToCharArray();
            char[] C_patd = patd.ToCharArray();

            //分别得到匹配字符串的长度
            int a = patd.Length;
            int b = pad.Length;
            if (patd.Contains(pad))
            {
                int offset = 0;
                int sum = 0;   //匹配计数
                int limit = 0;  //界限
                int i = 0;  //控制C-pad的数组下标
                int j = 0;  //控制C_patd的数组下标
                int s = 0;  //控制C-pad开始搜索的起始索引
                do
                {     //匹配上
                    if (C_pad[i] == C_patd[j])
                    {
                        i++;
                        j++;
                        sum++;
                        limit++;
                    }
                    else
                    {
                        j++;
                        i = 0;
                    }
                    offset++;
                    if (i == b)
                    {
                        s = offset – b;
                        break;
                    }
                } while (true);
                if (i == b)
                {
                    present = i * 100 / sum;
                }
                return s;
            }
            else
            {
                return -1;
            }
        }
        /// <summary>
        /// 优化的KMP字符串匹配算法
        /// </summary>
        /// <param name=”pad”>要匹配的字符串</param>
        /// <param name=”patd”>目标字符串</param>
        /// <returns> 返回一个目标字符串的索引 -1 表示失败</returns>
        static int YouHua_KMP(string pad, string patd)
        {
            pad = pad.Trim();
            char[] C_pad = pad.ToCharArray();
            char[] C_patd = patd.ToCharArray();

            //分别得到匹配字符串的长度
            int a = patd.Length;
            int b = pad.Length;
            if (patd.Contains(pad))
            {
                int offset = 0;
                int sum = 0;   //匹配计数
                int limit = 0;  //界限
                int i = 0;  //控制C-pad的数组下标
                int j = 0;  //控制C_patd的数组下标
                int s = 0;  //控制C-pad开始搜索的起始索引
                do
                {     //匹配上
                    if (C_pad[i] == C_patd[j])
                    {
                        i++;
                        j++;
                        sum++;
                        limit++;
                    }
                    else
                    {
                        j++;
                        i = 0;
                    }
                    offset++;
                    if (i == b)
                    {
                        s = offset – b;
                        break;
                    }
                } while (true);
                return s;
            }
            else
            {
                return -1;
            }
        }
    }
}

    原文作者:KMP算法
    原文地址: https://blog.csdn.net/daowzq/article/details/6872037
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞