Boyer-Moore算法的C#实现

using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace Test
{
    public class BM
    {
        public BM() { }

        StringBuilder contextCache = new StringBuilder();
        List<int> startIndex = new List<int>();
        List<int> endIndex = new List<int>();
        public void SetCache(string[] context)
        {
            if (contextCache.Length == 0)
            {
                for (int i = 0; i < context.Length; i++)
                {
                    startIndex.Add(contextCache.Length);
                    setCache(context[i]);
                    endIndex.Add(contextCache.Length - 1);
                }
            }
        }
        public void SetCache(string context)
        {
            if (contextCache.Length == 0)
            {
                setCache(context);
            }
        }
        void setCache(string context)
        {
            contextCache.Append(context);
        }
        public void ClearCache()
        {
            contextCache.Clear();
            startIndex.Clear();
            endIndex.Clear();
        }

        /// <summary>
        /// 搜索字符串索引
        /// </summary>
        /// <param name="targetMsg">要搜索的字符串</param>
        /// <returns>根据缓存数据类型确定返回数据类型,缓存的数组则返回数组索引,缓存字符串则返回字符索引</returns>
        public int[] GetIndex(string targetMsg)
        {
            return getIndex(targetMsg);
        }
        /// <summary>
        /// 搜索字符串索引
        /// </summary>
        /// <param name="targetMsg">要搜索的字符串数组</param>
        /// <returns>根据缓存数据类型确定返回数据类型,缓存的数组则返回数组索引,缓存字符串则返回字符索引</returns>
        public int[][] GetIndex(string[] targetMsg)
        {
            int[][] result = new int[targetMsg.Length][];
            for (int i = 0; i < targetMsg.Length; i++)
            {
                result[i] = getIndex(targetMsg[i]);
            }
            return result;
        }

        List<char> temparray = new List<char>();
        List<int> result = new List<int>();
        int[] getIndex(string targetMsg)
        {
            result.Clear();
            temparray.Clear();
            temparray.AddRange(targetMsg);
            for (int i = temparray.Count - 1; i < contextCache.Length;)
            {
                int moveCount = temparray.Count-1;
                if (temparray[temparray.Count - 1] == contextCache[i])
                {
                    for (int j = temparray.Count - 2, k = 1; j >= 0; j--, k++)
                    {
                        if (temparray[j] != contextCache[i - k])
                        {
                            moveCount -= temparray.FindIndex(0, temparray.Count - k, a => a == contextCache[i]);
                            break;
                        }
                    }
                    if (moveCount == temparray.Count - 1)
                    {
                        int charIndex = i - moveCount;
                        if (startIndex.Count != 0)
                        {
                            for (int j = 0; j < startIndex.Count; j++)
                            {
                                if (startIndex[j] <= charIndex && charIndex <= endIndex[j])
                                {
                                    charIndex = j;
                                    break;
                                }
                                if (j == startIndex.Count)//过滤掉由拼接字符组出现的拼接答案
                                {
                                    charIndex = -1;
                                }
                            }
                        }
                        if (charIndex != -1)
                            result.Add(charIndex);
                        moveCount = 1;
                    }
                }
                else
                {
                    for (int j = temparray.Count - 1; j >= 0; j--)
                    {
                        if (temparray[j] == contextCache[i])
                        {
                            moveCount -= j;
                            break;
                        }
                        if (j == 0)
                        {
                            moveCount -= -1;
                        }
                    }

                }
                i += moveCount;
            }
            return result.ToArray();
        }
    }

}
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值