Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace longestCommonSubstringDP
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. string test = longestCommonSubstring("abc123def", "hij123klmn");
  14. string testEdgecaseRow0 = longestCommonSubstring("1abc","def1ghi"); // edge case: line 50-58
  15. string testEdgecaseCol0 = longestCommonSubstring("abc1def","1ghijkl");
  16. }
  17.  
  18. /*
  19. * Lintcode #79 - longest common substring
  20. *
  21. * Practice using Dynamic programming solution
  22. *
  23. * Time complexity: O(nm), space complexity: O(nm), n is string s1's length,
  24. * m is string s2's length
  25. *
  26. * Using Dynamic programming, bottom up
  27. * Based on the recurrence formula
  28. * T(i+1, j+1) = T(i,j) + 1, if s[i+1] = s[j+1],
  29. * = 0, if s[i+1] != s[j+1]
  30. * T(i,j) stands for length of longest common substring from s1, s2,
  31. * s1's substring ends at postion i,
  32. * and s2's substring ends at position j
  33. *
  34. * Design issues:
  35. *
  36. * 4 variables - memo, longest, end1, searchFirstRowCol
  37. * 1. memorization using two dimension array - memo
  38. * 2. variable int longest - get maximum length
  39. * 3. variable int end1 - string s1 - end position - s1's substring end position
  40. * 4. vraiable bool searchFirstRowCol - check first row and first col to update maximum length
  41. */
  42. public static string longestCommonSubstring(string s1, string s2)
  43. {
  44. if (s1 == null || s1.Length == 0 || s2 == null || s2.Length == 0)
  45. return string.Empty;
  46.  
  47. int len1 = s1.Length;
  48. int len2 = s2.Length;
  49.  
  50. int[,] memo = new int[len1, len2];
  51.  
  52. // first column of 2-dimension matrix
  53. int end1 = 0; // end position of string 1
  54. int longest = 0;
  55. bool searchFirstRowCol = true;
  56.  
  57. for (int i = 0; i < len1; i++)
  58. {
  59. memo[i, 0] = (s1[i] == s2[0]) ? 1 : 0;
  60. if (searchFirstRowCol && memo[i, 0] > 0) // static analysis - added
  61. {
  62. end1 = i; // found by test case!
  63. longest = 1;
  64. searchFirstRowCol = false;
  65. }
  66. }
  67.  
  68. for (int j = 0; j < len2; j++)
  69. {
  70. memo[0, j] = (s1[0] == s2[j]) ? 1 : 0;
  71. if (searchFirstRowCol && memo[0, j] > 0)
  72. {
  73. end1 = 0;
  74. longest = 1;
  75. searchFirstRowCol = false;
  76. }
  77. }
  78.  
  79. for(int i=1; i< len1; i++)
  80. for (int j = 1; j < len2; j++)
  81. {
  82. memo[i, j] = (s1[i] == s2[j]) ? (memo[i - 1, j - 1] + 1) : 0;
  83. if (memo[i, j] > longest)
  84. {
  85. longest = memo[i, j];
  86. end1 = i;
  87. }
  88. }
  89.  
  90. return longest == 0 ? string.Empty : s1.Substring(end1 - longest + 1, longest); // start position check, length check.
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement