-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExact_String_Search_SingleThread.cs
More file actions
257 lines (226 loc) · 12.1 KB
/
Exact_String_Search_SingleThread.cs
File metadata and controls
257 lines (226 loc) · 12.1 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace String_Search
{
class Program
{
struct P_Bad_Character_struct
{
public char P_Char;
public int Bad_Character_Shift;
public P_Bad_Character_struct(char input1, int input2)
{
P_Char = input1;
Bad_Character_Shift = input2;
}
}
static void Main(string[] args)
{
//string T = "GTTATAGCTGATCGCGGCGTAGCGGCGATAT"; //original
//string T = "GTTATAGCTGATCCCGGCGTAGCGGCGATATCTCCCCC";
//string T = "GTTAGAGCTGATCGCGGCGTAGCGGCGATATCGAGCGGCGCCTCATAGTAGATA";
string T = "TGCATGTTAGAGTGATGAAGCGATAAAAGGTAGGTAGCGGCGTAGGAAAACCGTGATAGTAGAAAAATATAGATAAGATACGCAATTACA"; //
//string T = "CGTGCCTACTTACTTACTTACTTACGCGAA"; //
//string P = "CTTACTTAC";
//string P = "GTAGCGGCG"; //original
//string T = "GTTATAGCTGATCGCGGCGTAGCGGCGAA"; //original
//string P = "GTTA";
//string P = "CGTAG";
//string P = "GCGG";
//string P = "CGTA";
//string P = "GCGA";
//string P = "TGATCGC";
//string P = "GCGATAT";
//string P = "ATAGTAGATA";
//string P = "TAGATAAGATA";
//string P = "AAA";
//string P = "CGCAATTACA"; //10 characters
//string P = "CC";
//string P = "A";
//string P = "TC";
//string P = "CCTC";
//string P = "CTCCCCC";
//string P = "ATC";
//string P = "AAAA";
string P = "AAAAA";
int P_position_in_T = string_search(P, T);
if (P_position_in_T == -1)
Console.WriteLine("P string not exist in string T");
else //P_position_in_T >= 0
Console.WriteLine("String P is found inside string T, between " + P_position_in_T + " to " + (P_position_in_T + P.Length - 1));
Console.Read();
}
private static int string_search(string P, string T)
{
string matched_string = "";
int start_P_in_T = 0;
int end_P_in_T = P.Length - 1;
int T_ptr;
//Check string 'P' & 'T' are not empty and T string is longer than P string, or else definitely will not be any matches and no reason to proceed the checking
if (!String.IsNullOrEmpty(P) && !String.IsNullOrEmpty(T) && T.Length >= P.Length)
{
if (P.Length == 1) //Special case: if string 'P' only consist of 1 character
{
T_ptr = 0;
while (T_ptr < T.Length && P[0] != T[T_ptr])
T_ptr++;
if (T_ptr != T.Length)
return T_ptr;
else
return -1;
}
int P_ptr; //Pointer showing position of target search string 'P' last character now
T_ptr = P.Length - 1; //Pointer showing position of target search string 'P' last character in string 'T' now
//To optimize quick shifting, contruct string 'P' number of shift array table
//First case: when only last character of string 'P' matching string 'T', how many characters shift string 'P' should be in string 'T'
P_ptr = P.Length - 2;
int only_last_character_of_P_matching_shift;
while (P_ptr >= 0 && P[P_ptr] != P.Last())
P_ptr--;
only_last_character_of_P_matching_shift = P.Length - 1 - P_ptr;
//Second case: when string 'P' suffix partially matching string 'T', 2 characters & more. How many characters shift string 'P' should be in string 'T'
matched_string = P.Substring(P.Length - 2, 2);
int matched_string_ptr = 1;
//P_ptr pointer continue from only_last_character_of_P_matching case
int[] P_Good_Suffix_shift_table = new int[P.Length - 2];
//P_Good_Suffix_Rule
finding_good_suffix_shift:
if (P_ptr >= 0)
{
finding_good_suffix_shift_2:
if (matched_string[matched_string_ptr] == P[P_ptr])
{
if (P_ptr == 0 || matched_string_ptr == 0)
goto found_good_suffix_shift;
matched_string_ptr--;
}
else
{
if (matched_string_ptr != matched_string.Count() - 1)
{
matched_string_ptr = matched_string.Count() - 1;
goto finding_good_suffix_shift_2;
}
}
P_ptr--;
goto finding_good_suffix_shift;
}
found_good_suffix_shift:
if (P_ptr > 0) //Match string are found in the mid of remaining P string
{
P_Good_Suffix_shift_table[matched_string.Length - 2] = P.Length - matched_string.Length - P_ptr;
P_ptr--;
//matched_string increase length by 1
matched_string = P.Substring(P.Length - matched_string.Length - 1, matched_string.Length + 1); //matched_string increase length by 1
matched_string_ptr = 0; //search suffix from previous shorter suffix position, 1 character to the left
goto finding_good_suffix_shift; //should goto finding_good_suffix_shift_2
}
else if (P_ptr == 0) //Match string are found in the start of remaining P string
{
int P_Good_Suffix_shift = P.Length - matched_string.Count() + matched_string_ptr;
for (int i = matched_string.Length - 2; i < P.Length - 2; i++)
P_Good_Suffix_shift_table[i] = P_Good_Suffix_shift;
//goto P_Bad_Character_Rule;
}
else //When P_ptr < 0, no match found in remaining P string
{
for (int i = matched_string.Length - 2; i < P.Length - 2; i++)
P_Good_Suffix_shift_table[i] = P.Length;
//goto P_Bad_Character_Rule;
}
//Third case: when string 'P' last character does not match string 'T'
//P_Bad_Character_Rule
P_ptr = P.Length - 2;
List<P_Bad_Character_struct> P_Bad_Character_shift_table = new List<P_Bad_Character_struct>();
while (P_ptr >= 0 && P[P_ptr] == P.Last())
P_ptr--;
//P_ptr == -1, special case when string 'P' only consist of only one duplicated character
if (P_ptr != -1) //string 'P' has character different from P.Last()
{
P_Bad_Character_shift_table.Add(new P_Bad_Character_struct(P[P_ptr], P.Length - 1 - P_ptr)); //Add P second last character into the table
P_ptr--;
while (P_ptr >= 0) //when string P.length >= 3
{
int i = 0;
while (i < P_Bad_Character_shift_table.Count() && P_Bad_Character_shift_table[i].P_Char != P[P_ptr])
i++;
if (i == P_Bad_Character_shift_table.Count() && P[P_ptr] != P.Last()) //P current character not occur inside P_Bad_Character_shift_table, add this new character & it position
P_Bad_Character_shift_table.Add(new P_Bad_Character_struct(P[P_ptr], P.Length - 1 - P_ptr));
P_ptr--;
}
for (int i = 0; i < P_Bad_Character_shift_table.Count(); i++)
Console.WriteLine("P_Bad_Character_shift_table.P_Char[" + i + "] = " + P_Bad_Character_shift_table[i].P_Char
+ ", Bad_Character_Shift = " + P_Bad_Character_shift_table[i].Bad_Character_Shift);
for (int i = 0; i < P_Good_Suffix_shift_table.Count(); i++)
Console.WriteLine("P_Good_Suffix_shift_table[" + i + "] = " + P_Good_Suffix_shift_table[i]);
}
//Start string 'P' against string 'T' comparison
int matched_string_length = 0;
P_ptr = P.Length - 1;
string_compare_P_against_T:
if (T_ptr < T.Length)
{
//String compare from string 'P' last character toward first character against string 'T'. Stop whenever there is a mismatch.
while (P_ptr >= 0 && P[P_ptr] == T[T_ptr])
{
matched_string_length++;
T_ptr--;
P_ptr--;
}
if (matched_string_length == 0) //Bad Character Rule
{
if (P_Bad_Character_shift_table.Count() == 0)
{
//special case when string 'P' only consist of only one duplicated character
start_P_in_T += P.Length;
end_P_in_T += P.Length;
}
else
{
int i = 0;
while (i < P_Bad_Character_shift_table.Count() && P_Bad_Character_shift_table[i].P_Char != T[end_P_in_T])
i++;
if (i == P_Bad_Character_shift_table.Count())
{
//string 'T' character correspond to string 'P' last character position cannot be found in string 'P'
start_P_in_T += P.Length;
end_P_in_T += P.Length;
}
else
{
start_P_in_T += P_Bad_Character_shift_table[i].Bad_Character_Shift;
end_P_in_T += P_Bad_Character_shift_table[i].Bad_Character_Shift;
}
}
Console.Write("Bad Character Rule, ");
}
else if (matched_string_length == 1)
{
//Only P last character match
start_P_in_T += only_last_character_of_P_matching_shift;
end_P_in_T += only_last_character_of_P_matching_shift;
Console.Write("Only P last character match, ");
}
else if (matched_string_length == P.Length) //found string 'P' matching string 'T' in this position
return start_P_in_T; //return the position of string 'P' in string 'T'
else //P against T matched string is 2 characters or more but not exactly matched, only partially match
{
//Good Suffix Rule
start_P_in_T += P_Good_Suffix_shift_table[matched_string_length - 2];
end_P_in_T += P_Good_Suffix_shift_table[matched_string_length - 2];
Console.Write("Good Suffix Rule, ");
}
T_ptr = end_P_in_T;
P_ptr = P.Length - 1;
matched_string_length = 0;
Console.WriteLine("start_P_in_T = " + start_P_in_T);
goto string_compare_P_against_T;
}
}
return -1; //found string 'P' not matching string 'T'
}
}
}