-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNHAY.cs
96 lines (81 loc) · 3.53 KB
/
NHAY.cs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Text;
// https://www.spoj.com/problems/NHAY/ #strings
// Finds all occurrences of a given pattern in a string.
public static class NHAY
{
public static IEnumerable<int> Solve(string text, string pattern)
=> KmpStringMatcher.GetMatchIndices(text, pattern);
}
// This is taken from CLRS. It maintains the one-based indexing used in it (since it's most natural), but gets a
// little ugly as the strings have to be zero-based. Where relevant, the original indices are shown in parentheses.
public static class KmpStringMatcher
{
public static IEnumerable<int> GetMatchIndices(string text, string pattern)
{
IReadOnlyList<int> prefixesLengthOfLongestProperSuffixThatIsItselfAPrefix =
ComputePrefixesLengthOfLongestProperSuffixThatIsItselfAPrefix(pattern);
int matchedCharactersCount = 0;
for (int i = 1; i <= text.Length; ++i)
{
while (matchedCharactersCount > 0 && pattern[(matchedCharactersCount + 1) - 1] != text[(i) - 1])
{
matchedCharactersCount = prefixesLengthOfLongestProperSuffixThatIsItselfAPrefix[matchedCharactersCount];
}
if (pattern[(matchedCharactersCount + 1) - 1] == text[(i) - 1])
{
++matchedCharactersCount;
}
if (matchedCharactersCount == pattern.Length)
{
yield return i - pattern.Length;
matchedCharactersCount = prefixesLengthOfLongestProperSuffixThatIsItselfAPrefix[matchedCharactersCount];
}
}
}
public static IReadOnlyList<int> ComputePrefixesLengthOfLongestProperSuffixThatIsItselfAPrefix(string pattern)
{
int[] prefixesLengthOfLongestProperSuffixThatIsItselfAPrefix = new int[pattern.Length + 1];
int lengthOfLongestProperSuffixThatIsItselfAPrefix = prefixesLengthOfLongestProperSuffixThatIsItselfAPrefix[1] = 0;
for (int i = 2; i <= pattern.Length; ++i)
{
while (lengthOfLongestProperSuffixThatIsItselfAPrefix > 0
&& pattern[(lengthOfLongestProperSuffixThatIsItselfAPrefix + 1) - 1] != pattern[(i) - 1])
{
lengthOfLongestProperSuffixThatIsItselfAPrefix =
prefixesLengthOfLongestProperSuffixThatIsItselfAPrefix[lengthOfLongestProperSuffixThatIsItselfAPrefix];
}
if (pattern[(lengthOfLongestProperSuffixThatIsItselfAPrefix + 1) - 1] == pattern[(i) - 1])
{
++lengthOfLongestProperSuffixThatIsItselfAPrefix;
}
prefixesLengthOfLongestProperSuffixThatIsItselfAPrefix[i] = lengthOfLongestProperSuffixThatIsItselfAPrefix;
}
return Array.AsReadOnly(prefixesLengthOfLongestProperSuffixThatIsItselfAPrefix);
}
}
public static class Program
{
private static void Main()
{
var output = new StringBuilder();
int patternLength;
while (int.TryParse(Console.ReadLine(), out patternLength))
{
string pattern = Console.ReadLine();
string text = Console.ReadLine();
int outputLengthBefore = output.Length;
foreach (int matchIndex in NHAY.Solve(text, pattern))
{
output.Append(matchIndex);
output.AppendLine();
}
if (output.Length == outputLengthBefore)
{
output.AppendLine(); // Empty line indicates no matches found.
}
}
Console.Write(output);
}
}