From 462c7551af2fd5fc754be8e41b0942880a6e39e8 Mon Sep 17 00:00:00 2001 From: jie65535 <840465812@qq.com> Date: Wed, 20 Jan 2021 00:43:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=B9=E9=85=8D=E8=A7=84=E5=88=99=EF=BC=8C?= =?UTF-8?q?=E5=8A=A0=E5=85=A5KMP=E7=AE=97=E6=B3=95=E6=80=9D=E6=83=B3=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E9=BB=98=E8=AE=A4=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E7=9A=84=E7=9F=A9=E9=98=B5=E5=A4=A7=E5=B0=8F=E4=B8=8E=E7=BC=93?= =?UTF-8?q?=E5=86=B2=E5=8C=BA=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CodeMatrix/UCCodeMatrix.cs | 4 ++-- CodeMatrix/UCCodeQueue.cs | 2 +- CodeMatrix/UCCodeTarget.cs | 46 +++++++++++++++++++++++++++++++++----- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/CodeMatrix/UCCodeMatrix.cs b/CodeMatrix/UCCodeMatrix.cs index cc7d487..fa00a18 100644 --- a/CodeMatrix/UCCodeMatrix.cs +++ b/CodeMatrix/UCCodeMatrix.cs @@ -69,8 +69,8 @@ namespace CodeMatrix private void InitData() { - Rows = 5; - Columns = 5; + Rows = 7; + Columns = 7; _currDir = Directions.Horizontal; HoverPoint = new Point(-1, -1); } diff --git a/CodeMatrix/UCCodeQueue.cs b/CodeMatrix/UCCodeQueue.cs index 75233da..bfe9027 100644 --- a/CodeMatrix/UCCodeQueue.cs +++ b/CodeMatrix/UCCodeQueue.cs @@ -34,7 +34,7 @@ namespace CodeMatrix private void InitData() { - BufferSize = 4; + BufferSize = 9; CurrIndex = 0; HoverCode = 0; } diff --git a/CodeMatrix/UCCodeTarget.cs b/CodeMatrix/UCCodeTarget.cs index 38715bb..01ed319 100644 --- a/CodeMatrix/UCCodeTarget.cs +++ b/CodeMatrix/UCCodeTarget.cs @@ -31,6 +31,7 @@ namespace CodeMatrix public int OffsetIndex { get; private set; } public int CurrIndex { get; private set; } public byte[] TargetCodes { get; } = new byte[32]; + private int[] _Next = new int[32]; public enum State { @@ -49,14 +50,32 @@ namespace CodeMatrix private void InitData() { - BufferSize = 4; - TargetLength = Common.Random.Next(2) + 2; + BufferSize = 9; + TargetLength = Common.Random.Next(3, 6); OffsetIndex = 0; CurrIndex = 0; HoverCode = 0; CurrState = State.Input; for (int i = 0; i < TargetLength; i++) TargetCodes[i] = Common.Codes[Common.Random.Next(Common.Codes.Length)]; + _Next[0] = -1; + _Next[1] = 0; + for (int i = 1, j = 0; i < TargetLength;) + { + if (j == -1 || TargetCodes[i] == TargetCodes[j]) + { + i++; + j++; + if (TargetCodes[i] != TargetCodes[j]) + _Next[i] = j; + else + _Next[i] = _Next[j]; + } + else + { + j = _Next[j]; + } + } } private void InitComponent() @@ -177,10 +196,25 @@ namespace CodeMatrix { if (CurrState == State.Input) { - if (code == TargetCodes[CurrIndex]) - CurrIndex++; - else - OffsetIndex++; + while (true) + { + if (CurrIndex == -1) + { + CurrIndex = 0; + break; + } + if (code == TargetCodes[CurrIndex]) + { + CurrIndex++; + break; + } + else + { + var next = _Next[CurrIndex]; + OffsetIndex += CurrIndex - next; + CurrIndex = next; + } + } if (OffsetIndex + TargetLength > BufferSize) CurrState = State.Reject;