汕头网站制作网页,英文网站接单做翻译,做网站的怎么学,0511网站Z算法也是模式搜索#xff08;Pattern Search Algorithm#xff09;的常用算法。
本文代码的运算效果#xff1a; 一、Z 算法
线性时间模式搜索算法的Z算法#xff0c;在线性时间内查找文本中模式的所有出现。
假设文本长度为 n#xff0c;模式长度为 m#xff0c;那么… Z算法也是模式搜索Pattern Search Algorithm的常用算法。
本文代码的运算效果 一、Z 算法
线性时间模式搜索算法的Z算法在线性时间内查找文本中模式的所有出现。
假设文本长度为 n模式长度为 m那么所用的总时间为 O(m n)空间复杂度为线性。现在我们可以看到时间和空间复杂度都和 KMP 算法一样但是这个算法更容易理解。
在这个算法中我们构造了一个 Z 数组。
什么是 Z 数组 为字符串[0..n-1]Z 数组与字符串长度相同。Z 数组的元素 Z[i]存储从字符串[i]开始的最长子串的长度字符串[i]也是字符串[0]的前缀..n-1]。Z 数组的第一个条目意义不大因为完整的字符串总是它自己的前缀。 二、核心代码
#define ANIMATE using System; using System.Text; using System.Collections; using System.Collections.Generic;
namespace Legalsoft.Algorithm.PatternSearch { /// summary /// Z算法模式搜索 /// /summary public class Z_Algorithm { #if ANIMATE public Liststring slides { get; set; } new Liststring(); private string[] stringArray { get; set; } null; #endif /// summary /// 构建Z数组并进行Z算法模式搜索 /// /summary /// param nametext/param /// param namepattern/param /// returns/returns public string Search(string text, string pattern) { // 构造模式字串与原始字符串的合并串 P$T string concat pattern $ text; // 以合并串构建Z数组 int[] Z Z_Array_Build(concat); for (int i 0; i concat.Length; i) { #if ANIMATE slides.Add(ToHtml(Z, i)); #endif if (Z[i] pattern.Length) { return 模式位于 (i - pattern.Length - 1); } } return 未找到匹配模式; } /// summary /// 依据 字符串构建 Z 数组 /// /summary /// param namestr/param private int[] Z_Array_Build(string str) { int n str.Length; int[] Z new int[n]; int L 0; int R 0; #if ANIMATE stringArray new string[n]; stringArray[0] str.Substring(0, 1); #endif for (int i 1; i n; i) { #if ANIMATE stringArray[i] str.Substring(i, 1); #endif if (i R) { L R i; while (R n str[R - L] str[R]) { R; } Z[i] R - L; R--; } else { int k i - L; if (Z[k] R - i 1) { Z[i] Z[k]; } else { L i; while (R n str[R - L] str[R]) { R; } Z[i] R - L; R--; } } } return Z; } #if ANIMATE private string ToHtml(int[] Z, int index) { StringBuilder sb new StringBuilder(); sb.Append(styletd { padding:5px;text-align:center; }/style); sb.Append(table border1 styleborder-collapse:collapse;); sb.Append(tr); sb.Append(tdstring/td); for (int i 0; i stringArray.Length; i) { sb.Append(td stringArray[i] /td); } sb.Append(/tr); sb.Append(tr); sb.Append(tdz array/td); for (int i 0; i Z.Length; i) { if (i index) { sb.Append(td stylebackground-color:#FF6701;color:#FFFFFF; Z[i] /td); } else { sb.Append(td stylebackground-color:#FFDD99; Z[i] /td); } } sb.Append(/tr); sb.Append(/table); return sb.ToString(); } #endif } } 三、数据可视化代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
using Legalsoft.Algorithm.PatternSearch;
namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.Text C#模式搜索Z算法线性时间模式搜索算法——北京联高软件开发有限公司; button1.Text Z算法; button1.Cursor Cursors.Hand; panel1.Dock DockStyle.Top; panel2.Dock DockStyle.Fill; webBrowser1.Navigate(http://www.315soft.com); textBox1.Text C#模式搜索Z算法线性时间模式搜索算法,联高软件; textBox2.Text Z算法; } private void button1_Click(object sender, EventArgs e) { Z_Algorithm z new Z_Algorithm(); z.Search(textBox1.Text.Trim(), textBox2.Text.Trim()); slides.AddRange(z.slides); loop 0; timer1.Interval 1000; timer1.Enabled true; } int loop 0; Liststring slides new Liststring(); private void timer1_Tick(object sender, EventArgs e) { if (loop slides.Count (3000 / timer1.Interval)) { if (loop slides.Count) { StringBuilder sb new StringBuilder(); sb.AppendLine(style* { font-size:21px; } /style); sb.AppendLine(Source String: font colorred textBox1.Text.Trim() /fontbr); sb.AppendLine(Pattern String: font colorblue textBox2.Text.Trim() /fontbr); sb.AppendLine(br); sb.Append(slides[loop]); webBrowser1.DocumentText sb.ToString(); loop; return; } loop; return; } loop 0; } } }
--------------------------------------------- POWER BY TRUFFER.CN 文章转载自: http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.kqpq.cn.gov.cn.kqpq.cn http://www.morning.hpggl.cn.gov.cn.hpggl.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.kntbk.cn.gov.cn.kntbk.cn http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.ysskn.cn.gov.cn.ysskn.cn http://www.morning.rykx.cn.gov.cn.rykx.cn http://www.morning.bfbl.cn.gov.cn.bfbl.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.jwxmn.cn.gov.cn.jwxmn.cn http://www.morning.playmi.cn.gov.cn.playmi.cn http://www.morning.tdhxp.cn.gov.cn.tdhxp.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.qnypp.cn.gov.cn.qnypp.cn http://www.morning.srkqs.cn.gov.cn.srkqs.cn http://www.morning.zxhhy.cn.gov.cn.zxhhy.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.fmznd.cn.gov.cn.fmznd.cn http://www.morning.dhpjq.cn.gov.cn.dhpjq.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.lzqtn.cn.gov.cn.lzqtn.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.kyzja.com.gov.cn.kyzja.com http://www.morning.kwyq.cn.gov.cn.kwyq.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.prysb.cn.gov.cn.prysb.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.nkjnr.cn.gov.cn.nkjnr.cn http://www.morning.sjsks.cn.gov.cn.sjsks.cn http://www.morning.tdfyj.cn.gov.cn.tdfyj.cn http://www.morning.lcdtb.cn.gov.cn.lcdtb.cn http://www.morning.wgkz.cn.gov.cn.wgkz.cn http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.tkjh.cn.gov.cn.tkjh.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.pbzgj.cn.gov.cn.pbzgj.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.wiitw.com.gov.cn.wiitw.com http://www.morning.ksjmt.cn.gov.cn.ksjmt.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.haolipu.com.gov.cn.haolipu.com http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.bhznl.cn.gov.cn.bhznl.cn http://www.morning.rqnml.cn.gov.cn.rqnml.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.khtjn.cn.gov.cn.khtjn.cn http://www.morning.fcxt.cn.gov.cn.fcxt.cn http://www.morning.rswfj.cn.gov.cn.rswfj.cn http://www.morning.qphcq.cn.gov.cn.qphcq.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.pshtf.cn.gov.cn.pshtf.cn http://www.morning.lxlzm.cn.gov.cn.lxlzm.cn http://www.morning.chmkt.cn.gov.cn.chmkt.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.sdktr.com.gov.cn.sdktr.com http://www.morning.brwp.cn.gov.cn.brwp.cn http://www.morning.qtqjx.cn.gov.cn.qtqjx.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.xqgtd.cn.gov.cn.xqgtd.cn