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

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

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

namespace ConsoleApplication1
{
    public class KMPMatchString
    {
        ///


        /// 优化的KMP字符串匹配算法
        ///

        /// 要匹配的字符串
        /// 目标字符串
        /// 返回匹配成功百分率
        /// 返回一个目标字符串的索引 -1 表示失败
        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;
            }
        }
        ///


        /// 优化的KMP字符串匹配算法
        ///

        /// 要匹配的字符串
        /// 目标字符串
        /// 返回一个目标字符串的索引 -1 表示失败
        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;
            }
        }
    }
}

转载于:https://www.cnblogs.com/voidobject/archive/2011/10/14/3975518.html

你可能感兴趣的:(c#)