-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (73 loc) · 1.95 KB
/
Copy pathProgram.cs
File metadata and controls
74 lines (73 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KMPAlgorithm
{
class Program
{
static void Main(string[] args)
{
int result = KMP("FSAQWadASDCXE","ASD");
Console.WriteLine(result.ToString());
Console.ReadLine();
}
public static int KMP(string ts,string ps)
{
char[] t = ts.ToCharArray();
char[] p = ps.ToCharArray();
int i = 0;
int j = 0;
int[] next = getNext(ps);
while(i < t.Length && j< p.Length)
{
if(j == -1 || t[i] == p[j]) //如果j = -1或者当前字符匹配成功
{
i++;
j++;
}
else
{
j = next[j]; //如果j != -1且当前字符匹配失败
}
}
if(j == p.Length)
{
return i - j;
}
else
{
return -1;
}
}
public static int[] getNext(string ps)
{
char[] p = ps.ToCharArray();
int[] next = new int[p.Length];
next[0] = -1;
int j = 0;
int k = -1;
while(j<p.Length - 1)
{
//p[k]表示前缀,p[j]表示后缀
if(k == -1 || p[j] == p[k])
{
if(p[++j] == p[++k])//当两个字符相等时要继续递归,k = next[k] = next[next[k]]
{
next[j] = next[k];
}
else
{
next[j] = k;
}
}
else
{
k = next[k];
}
}
return next;
}
}
}