String-related algorithms (editing distance, maximum public string) C # implementation

Source: Internet
Author: User

Address: http://hi.baidu.com/tangguoshequ/blog/item/d587dc170878c8946538dbd1.html

1. levenshtein distance

The Algorithm It is also called "edit distance", used to calculate the similarity between two strings. The basic principle is to return the number of edits that convert the first string (delete, insert, and replace) to the second string. The less the number of times, the higher the string similarity.

Algorithm principle: Wikipedia-levenshtein distance This article from: http://www.rainsts.net/article.asp? Id = 767

Step 1:

People-to-People and time-based
0, 0, 0, 0, 0, 0, 0
Medium 1, 0, 0, 0, 0, 0, 0
China 2, 0, 0, 0, 0, 0, 0
3, 0, 0, 0, 0, 0, 0
Min 4, 0, 0, 0, 0, 0, 0
Total 5, 0, 0, 0, 0, 0, 0
And 6, 0, 0, 0, 0, 0, 0
Country 7, 0, 0, 0, 0, 0, 0

Step 2:

People-to-People and time-based
0, 1, 2, 3, 4, 5, 6
Medium 1, 0, 0, 0, 0, 0, 0
China 2, 0, 0, 0, 0, 0, 0
3, 0, 0, 0, 0, 0, 0
Min 4, 0, 0, 0, 0, 0, 0
Total 5, 0, 0, 0, 0, 0, 0
And 6, 0, 0, 0, 0, 0, 0
Country 7, 0, 0, 0, 0, 0, 0

Step 3:

People-to-People and time-based
0, 1, 2, 3, 4, 5, 6
Medium 1, 1, 2, 3, 4, 5, 6
China 2, 2, 2, 3, 4, 5, 6
3, 2, 3, 3, 4, 5, 6
Min 4, 3, 2, 3, 4, 5, 6
5, 4, 3, 2, 3, 4, 5
And 6, 5, 4, 3, 2, 3, 4
Country 7, 6, 5, 4, 3, 3, 4

Algorithm Implementation:

Public static int levenshteindistance (string S1, string S2)
{
If (S1 = S2)
Return 0;
Else if (string. isnullorempty (S1 ))
Return s2.length;
Else if (string. isnullorempty (S2 ))
Return s1.length;

VaR M = s1.length + 1;
VaR n = s2.length + 1;
VaR d = new int [M, N];

// Step 1
For (VAR I = 0; I <m; I ++) d [I, 0] = I;

// Step 2
For (var j = 0; j <n; j ++) d [0, J] = J;

// Step 3
For (VAR I = 1; I <m; I ++)
{
For (VAR j = 1; j <n; j ++)
{
VaR cost = S1 [I-1] = S2 [J-1]? 0: 1;

VaR deletion = d [I-1, J] + 1;
VaR insertion = d [I, j-1] + 1;
VaR substitution = d [I-1, J-1] + cost;

D [I, j] = math. Min (math. Min (deletion, insertion), substitution );
}
}

Return d [m-1, n-1];
}


2. LCS

The LCS (longest common subsequence) algorithm is used to identify the longest common substring of two strings.

Algorithm principle:

(1) The two strings form a matrix of rows and columns respectively.
(2) check whether the row and column characters of each node are the same. If the characters are the same, the value is 1.
(3) You can obtain the longest common substring by finding the longest diagonal line with the value of 1.

People-to-People and time-based
0, 0, 0, 0, 0, 0
China 0, 0, 0, 0, 0, 0
Person 1 , 0, 0, 0, 0, 0
Min 0, 1 , 0, 0, 0, 0
0 in total, 0,1 , 0, 0, 0
And 0, 0, 0, 1 , 0, 0
Country 0, 0, 0, 0, 0, 0

To further improve the algorithm, we can add the value of the same character node (1) to the value of the upper left corner (d [I-1, J-1]) to get the maximum length of the Public substring. In this way, the maximum substring can be intercepted only by row number and maximum value.

People-to-People and time-based
0, 0, 0, 0, 0, 0
China 0, 0, 0, 0, 0, 0
Person1 , 0, 0, 0, 0, 0
Min 0, 2 , 0, 0, 0, 0
0 in total, 0, 3 , 0, 0, 0
And 0, 0, 0, 4 , 0, 0
Country 0, 0, 0, 0, 0, 0

Algorithm Implementation:

Public static string LCS (string S1, string S2)
{
If (S1 = S2)
Return S1;
Else if (string. isnullorempty (S1) | string. isnullorempty (S2 ))
Return NULL;

VaR d = new int [s1.length, s2.length];

VaR Index = 0;
VaR length = 0;

For (INT I = 0; I <s1.length; I ++)
{
For (Int J = 0; j <s2.length; j ++)
{
// Value in the upper left corner
VaR n = I-1> = 0 & J-1> = 0? D [I-1, J-1]: 0;

// Current node value = "1 + upper left corner value": "0"
D [I, j] = S1 [I] = S2 [J]? 1 + N: 0;

// If it is the maximum value, this value and the row number are recorded.
If (d [I, j]> length)
{
Length = d [I, j];
Index = I;
}
}
}

Return s1.substring (index-Length + 1, length );
}

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.