惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
博客园 - 【当耐特】
小众软件
小众软件
博客园 - Franky
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
雷峰网
雷峰网
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
S
Security @ Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
Y
Y Combinator Blog
O
OpenAI News
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
Kaspersky official blog
Cloudbric
Cloudbric

博客园 - 飘渺峰

一致性Hash算法 .net Parallel并行使用注意事项 析构函数和Dispose方法的区别 查看SQLServer的最大连接数 Hash算法-CityHash算法 Hash算法 Sunday算法--C#版 BoyerMoore(BM)算法--C# HostFileChangeMonitor [转]软件项目管理总体流程设计 全排列和组合算法 生活 负载均衡算法--C#版 结束进程的方法forceStopPackage 【转】OAUTH协议简介 SQL Server FOR XML PATH 语句的应用 nlog轻量级日志组件 反射加载程序集的几个方法的区别 windows 下TCP最大连接数
KMP算法--C#版
飘渺峰 · 2013-12-10 · via 博客园 - 飘渺峰
        static void BuildTable(string subString, ref int[] next)
        {
            if (string.IsNullOrWhiteSpace(subString)) return;

            int j = 0, k = -1;
            next[0] = -1;
            while (j < subString.Length - 1)
            {
                if (-1 == k || subString[j] == subString[k])
                {
                    j++;
                    k++;
                    next[j] = k;
                }
                else
                {
                    k = next[k];
                }
            }
        }

        /// <summary>
        /// 查找算法
        /// </summary>
        /// <param name="source">要搜索的字符串</param>
        /// <param name="subString">子串</param>
        /// <returns>子串在source字符串中的开始位置</returns>
        static int KmpSearch(string source, string subString)
        {
            if (string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(subString))
                return -1;
            var next = new int[subString.Length];
            for (int k = 0; k < next.Length; k++)
            {
                next[k] = -1;
            }
            int i = 0, j = 0,sLen = subString.Length;

            BuildTable(subString, ref next);

            while (i < sLen)
            {
                if (j == -1 || source[i] == subString[j])
                {
                    i++;
                    j++;
                }
                else
                {
                    j = next[j];
                }

                if (j == sLen)
                {
                    return i - sLen;
                }
            }
            return -1;
        }

使用

            string source = "我是中和敏式的服务二是到覅维尔维尔34山东富翁234斯蒂芬234234式的服务二";
            string subString = "山东富翁";

            int a = KmpSearch(source, subString);

            Console.WriteLine(a);

posted @ 2013-12-10 14:40  飘渺峰  阅读(1362)  评论()    收藏  举报