算法沒什么可說的,就是一段一段匹配唄。
運行效果:
?源代碼:
using System;
using System.Collections;
using System.Collections.Generic;
namespace Legalsoft.Truffer.Algorithm
{
?? ?/// <summary>
?? ?/// 字符串匹配(模式搜索)算法集錦
?? ?/// </summary>
?? ?public static partial class PatternSearch
?? ?{
?? ??? ?/// <summary>
?? ??? ?/// 字符串匹配的暴力算法(1)
?? ??? ?/// </summary>
?? ??? ?/// <param name="text"></param>
?? ??? ?/// <param name="pattern"></param>
?? ??? ?/// <returns></returns>
?? ??? ?public static List<int> NativeSearch_Original(string text, string pattern)
?? ??? ?{
?? ??? ??? ?int pt = pattern.Length;
?? ??? ??? ?List<int> matchs = new List<int>();
?? ??? ??? ?for (int i = 0; i < (text.Length - pt); i++)
?? ??? ??? ?{
?? ??? ??? ??? ?if (text.Substring(i, pt) == pattern)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?matchs.Add(i);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?return matchs;
?? ??? ?}
?? ??? ?/// <summary>
?? ??? ?/// 字符串匹配的暴力算法(2)
?? ??? ?/// </summary>
?? ??? ?/// <param name="text"></param>
?? ??? ?/// <param name="pattern"></param>
?? ??? ?/// <returns></returns>
?? ??? ?public static List<int> Native_Search(string text, string pattern)
?? ??? ?{
?? ??? ??? ?List<int> matchs = new List<int>();
?? ??? ??? ?int M = pattern.Length;
?? ??? ??? ?int N = text.Length;
?? ??? ??? ?int S = N - M;
?? ??? ??? ?if (S <= 0) return matchs;
?? ??? ??? ?for (int i = 0; i <= S; i++)
?? ??? ??? ?{
?? ??? ??? ??? ?int j = 0;
?? ??? ??? ??? ?while (j < M)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (text[i + j] != pattern[j])
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?j++;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if (j == M)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?matchs.Add(i);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?return matchs;
?? ??? ?}
?? ?}
}
--------------================--------------------文章來源:http://www.zghlxwxcb.cn/news/detail-790463.html
POWER BY TRUFFER.CN文章來源地址http://www.zghlxwxcb.cn/news/detail-790463.html
using System;
using System.Collections;
using System.Collections.Generic;
namespace Legalsoft.Truffer.Algorithm
{
?? ?/// <summary>
?? ?/// 字符串匹配(模式搜索)算法集錦
?? ?/// </summary>
?? ?public static partial class PatternSearch
?? ?{
?? ??? ?/// <summary>
?? ??? ?/// 字符串匹配的暴力算法(1)
?? ??? ?/// </summary>
?? ??? ?/// <param name="text"></param>
?? ??? ?/// <param name="pattern"></param>
?? ??? ?/// <returns></returns>
?? ??? ?public static List<int> NativeSearch_Original(string text, string pattern)
?? ??? ?{
?? ??? ??? ?int pt = pattern.Length;
?? ??? ??? ?List<int> matchs = new List<int>();
?? ??? ??? ?for (int i = 0; i < (text.Length - pt); i++)
?? ??? ??? ?{
?? ??? ??? ??? ?if (text.Substring(i, pt) == pattern)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?matchs.Add(i);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?return matchs;
?? ??? ?}
?? ??? ?/// <summary>
?? ??? ?/// 字符串匹配的暴力算法(2)
?? ??? ?/// </summary>
?? ??? ?/// <param name="text"></param>
?? ??? ?/// <param name="pattern"></param>
?? ??? ?/// <returns></returns>
?? ??? ?public static List<int> Native_Search(string text, string pattern)
?? ??? ?{
?? ??? ??? ?List<int> matchs = new List<int>();
?? ??? ??? ?int M = pattern.Length;
?? ??? ??? ?int N = text.Length;
?? ??? ??? ?int S = N - M;
?? ??? ??? ?if (S <= 0) return matchs;
?? ??? ??? ?for (int i = 0; i <= S; i++)
?? ??? ??? ?{
?? ??? ??? ??? ?int j = 0;
?? ??? ??? ??? ?while (j < M)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?if (text[i + j] != pattern[j])
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?break;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?j++;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if (j == M)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?matchs.Add(i);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?return matchs;
?? ??? ?}
?? ?}
}
到了這里,關于C#,字符串匹配(模式搜索)原生(Native)算法的源代碼的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!