数据结构 --- 串的匹配

  1 using System;
  2 using System.Text;
  3 using System.Collections.Generic;
  4 using System.Collections;
  5 using System.Text.RegularExpressions;
  6 using System.Timers;
  7 
  8 namespace ConsoleApplication1
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             string s = "abcde123sadas";
 15             string p = "ababc";
 16 
 17             int[] array = GetNextArray(p);
 18             for (int i = 0; i < array.Length; ++i)
 19             {
 20                 Console.WriteLine(array[i]);
 21             }
 22 
 23             Console.ReadKey();
 24         }
 25 
 26         #region 字符串
 27 
 28         //字符串朴素匹配算法
 29         static int StringMatch(string s, string p)
 30         {
 31             int sLen = s.Length;
 32             int pLen = p.Length;
 33 
 34             int i = 0, j = 0;
 35             while (i < sLen && j < pLen)
 36             {
 37                 if (s[i] == p[j])
 38                 {
 39                     //如果当前字符匹配成功,继续比较
 40                     ++i;
 41                     ++j;
 42                 }
 43                 else
 44                 {
 45                     //如果失配,则令 i = i-j+1, j = 0
 46                     i = i - j + 1;
 47                     j = 0;
 48                 }
 49             }
 50 
 51             //匹配成功,返回p在s中的位置,否则返回 -1
 52             return j == pLen ? i - j : -1;
 53         }
 54 
 55         #region KMP匹配
 56         //计算字符串 s 的 Next 数组
 57         static int[] GetNextArray(string s)
 58         {
 59             //next数组值的含义 --- 当前位置匹配失败的时候,下标应该改变到的位置。
 60             int[] next = new int[s.Length];
 61             int pLen = s.Length;
 62             int k = -1;
 63             int j = 0;
 64 
 65             next[j] = k;
 66 
 67             while (j < pLen - 1)
 68             {
 69                 //s[k] --- 前缀的各个字符  s[j] --- 后缀的各个字符
 70                 if (k == -1 || s[k] == s[j])
 71                 {
 72                     ++k;
 73                     ++j;
 74                     next[j] = k;
 75                 }
 76                 else
 77                 {
 78                     k = next[k];
 79                 }
 80             }
 81 
 82             return next;
 83         }
 84 
 85         static int KMPMatch(string s, string p)
 86         {
 87             int sLen = s.Length;
 88             int pLen = p.Length;
 89 
 90             int i = 0, j = 0;
 91 
 92             int[] next = GetNextArray(p);
 93 
 94             while (i < sLen && j < pLen)
 95             {
 96                 if (j == -1 || s[i] == p[j])
 97                 {
 98                     //如果当前字符匹配成功,继续比较
 99                     ++i;
100                     ++j;
101                 }
102                 else
103                 {
104                     j = next[j];
105                 }
106             }
107 
108             //匹配成功,返回p在s中的位置,否则返回 -1
109             return j == pLen ? i - j : -1;
110         }
111 
112         #endregion
113 
114         #endregion
115     }
116 }

猜你喜欢

转载自www.cnblogs.com/luguoshuai/p/9951361.html