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

推荐订阅源

I
Intezer
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
N
News | PayPal Newsroom
T
Tenable Blog
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
P
Privacy International News Feed
IT之家
IT之家
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
博客园_首页
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
Martin Fowler
Martin Fowler
NISL@THU
NISL@THU
I
InfoQ
D
DataBreaches.Net
有赞技术团队
有赞技术团队
K
Kaspersky official blog
Security Latest
Security Latest
The Register - Security
The Register - Security
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
P
Proofpoint News Feed
M
MIT News - Artificial intelligence
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic

博客园 - 飘渺峰

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

因项目需要使用字符串查询算法,在网上搜搜了半天,没有找到C#版的。

索性根据BM机制,用C#实现了一遍。现在贴出了,以备忘记。

 1         /// <summary>
 2         /// BM算法
 3         /// </summary>
 4         /// <param name="source"></param>
 5         /// <param name="subString"></param>
 6         /// <returns></returns>
 7         static int BoyerMooreSearch(string source, string subString)
 8         {
 9             if(string.IsNullOrWhiteSpace(source) || string.IsNullOrWhiteSpace(subString)) 
10                 return - 1;
11             int i = 0, j, sLen = subString.Length;
12             while (i < sLen)
13             {
14                 j = 0;
15                 while (j < sLen && source[i] == subString[j])
16                 {
17                     i++;
18                     j++;
19                 }
20                 if (j == sLen)
21                 {
22                     return i - sLen;
23                 }
24                 i = i - j + 1;
25             }
26             return -1;
27         }

C#

使用:

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

            int a = BoyerMooreSearch(source, subString);

            Console.WriteLine(a);


如大家发现实现有什么问题,欢迎指正。