Click here to Skip to main content
15,912,977 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Substring Matching (Harder than it sounds) Pin
Russell'29-Aug-07 8:06
Russell'29-Aug-07 8:06 
GeneralRe: Substring Matching (Harder than it sounds) Pin
Skippums29-Aug-07 8:28
Skippums29-Aug-07 8:28 
GeneralRe: Substring Matching (Harder than it sounds) Pin
Russell'29-Aug-07 9:02
Russell'29-Aug-07 9:02 
AnswerRe: Substring Matching (Harder than it sounds) Pin
PIEBALDconsult29-Aug-07 18:03
mvePIEBALDconsult29-Aug-07 18:03 
AnswerRe: Substring Matching (Harder than it sounds) Pin
Mark Churchill29-Aug-07 18:26
Mark Churchill29-Aug-07 18:26 
AnswerRe: Substring Matching (Harder than it sounds) Pin
cp987629-Aug-07 23:55
cp987629-Aug-07 23:55 
GeneralRe: Substring Matching (Harder than it sounds) Pin
Skippums30-Aug-07 4:04
Skippums30-Aug-07 4:04 
AnswerRe: Substring Matching (Harder than it sounds) Pin
Skippums4-Sep-07 11:23
Skippums4-Sep-07 11:23 
Here is the code (in c#) that I finally ended up implementing. As I said before, I think it is O(n*log(n))...

public static string LongestMatchingSubstring(string s1, string s2, int minMatchLength, bool caseSensitive) {
if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s2))
return string.Empty;
string shorter, longer;
if (s1.Length > s2.Length) {
shorter = s2;
longer = s1;
} else {
shorter = s1;
longer = s2;
}
if (shorter.Length < minMatchLength)
return string.Empty;
Dictionary<char, IList<int>> hashTable = new Dictionary<char, IList<int>>(
Math.Min(caseSensitive ? 96 : 64, longer.Length));
if (caseSensitive) {
for (int i = 0; i < longer.Length; ++i) {
char c = longer[i];
if (!hashTable.ContainsKey(c))
hashTable.Add(c, new List<int>());
hashTable[c].Add(i);
}
} else {
for (int i = 0; i < longer.Length; ++i) {
char c = char.ToLower(longer[i]);
if (!hashTable.ContainsKey(c))
hashTable.Add(c, new List<int>());
hashTable[c].Add(i);
}
}
string result = string.Empty;
--minMatchLength;
FindLongestMatchingSubstring(shorter, longer,
hashTable, ref result, ref minMatchLength, caseSensitive);
return result;
}

private static void FindLongestMatchingSubstring(string str1, string str2,
Dictionary<char, IList<int>> ht, ref string result, ref int minMatchLength, bool caseSensitive) {
if (str1.Length <= minMatchLength)
return;
int midpoint = str1.Length / 2;
char key = caseSensitive ? str1[midpoint] : char.ToLower(str1[midpoint]);
if (ht.ContainsKey(key)) {
foreach (int index in ht[key]) {
string temp = GetFullMatch(str1, midpoint, str2, index, caseSensitive);
if (temp.Length > minMatchLength) {
minMatchLength = temp.Length;
result = temp;
}
}
}
FindLongestMatchingSubstring(str1.Substring(0, midpoint),
str2, ht, ref result, ref minMatchLength, caseSensitive);
FindLongestMatchingSubstring(str1.Substring(midpoint + 1),
str2, ht, ref result, ref minMatchLength, caseSensitive);
}

private static string GetFullMatch(string str1, int str1Idx, string str2, int str2Idx, bool caseSensitive) {
int minOffset = -Math.Min(str1Idx, str2Idx);
int maxOffset = Math.Min(str1.Length - str1Idx, str2.Length - str2Idx) - 1;
int lowOffset = -1, highOffset = 1;
if (caseSensitive) {
for (; lowOffset >= minOffset && str1[str1Idx + lowOffset] ==
str2[str2Idx + lowOffset]; --lowOffset)
;
for (; highOffset <= maxOffset && str1[str1Idx + highOffset] ==
str2[str2Idx + highOffset]; ++highOffset)
;
} else {
for (; lowOffset >= minOffset && char.ToLower(str1[str1Idx + lowOffset]) ==
char.ToLower(str2[str2Idx + lowOffset]); --lowOffset)
;
for (; highOffset <= maxOffset && char.ToLower(str1[str1Idx + highOffset]) ==
char.ToLower(str2[str2Idx + highOffset]); ++highOffset)
;
}
++lowOffset;
--highOffset;
return str2.Substring(str2Idx + lowOffset, highOffset - lowOffset + 1);
}

Hope this helps some people!

Jeff
GeneralRe: Substring Matching (Harder than it sounds) Pin
Luc Pattyn4-Sep-07 13:06
sitebuilderLuc Pattyn4-Sep-07 13:06 
GeneralRe: Substring Matching (Harder than it sounds) Pin
Skippums4-Sep-07 13:26
Skippums4-Sep-07 13:26 
GeneralRe: Substring Matching (Harder than it sounds) Pin
Luc Pattyn5-Sep-07 9:00
sitebuilderLuc Pattyn5-Sep-07 9:00 
GeneralRe: Substring Matching (Harder than it sounds) Pin
Skippums5-Sep-07 10:46
Skippums5-Sep-07 10:46 
GeneralRe: Substring Matching (Harder than it sounds) Pin
Member 419459329-Mar-08 9:55
Member 419459329-Mar-08 9:55 
QuestionNeed some intelligent people... Pin
Paddy Boyd29-Aug-07 2:20
Paddy Boyd29-Aug-07 2:20 
AnswerRe: Need some intelligent people... Pin
Luc Pattyn29-Aug-07 2:56
sitebuilderLuc Pattyn29-Aug-07 2:56 
GeneralRe: Need some intelligent people... Pin
Paddy Boyd29-Aug-07 3:42
Paddy Boyd29-Aug-07 3:42 
GeneralRe: Need some intelligent people... Pin
Paddy Boyd29-Aug-07 3:55
Paddy Boyd29-Aug-07 3:55 
GeneralRe: Need some intelligent people... Pin
Luc Pattyn29-Aug-07 4:42
sitebuilderLuc Pattyn29-Aug-07 4:42 
GeneralRe: Need some intelligent people... Pin
Paddy Boyd29-Aug-07 4:44
Paddy Boyd29-Aug-07 4:44 
AnswerRe: Need some intelligent people... Pin
Russell'29-Aug-07 4:54
Russell'29-Aug-07 4:54 
AnswerRe: Need some intelligent people... Pin
cp987629-Aug-07 16:18
cp987629-Aug-07 16:18 
GeneralRe: Need some intelligent people... Pin
Luc Pattyn29-Aug-07 23:41
sitebuilderLuc Pattyn29-Aug-07 23:41 
QuestionGenetic Algorithm Pin
starist26-Aug-07 4:58
starist26-Aug-07 4:58 
AnswerRe: Genetic Algorithm Pin
Russell'26-Aug-07 22:08
Russell'26-Aug-07 22:08 
QuestionCOMBINATION Pin
Heljeeve22-Aug-07 23:36
Heljeeve22-Aug-07 23:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.