Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Zad3
- {
- class Program
- {
- static int[,] LongestCommonSubsequence(string s1, string s2)
- {
- int[,] common = new int[s1.Length + 1, s1.Length + 1];
- for (int i = 1; i < s1.Length; i++)
- {
- for (int j = 1; j < s2.Length; j++)
- {
- if (s1[i - 1] == s2[j - 1]) common[i, j] = common[i - 1, j - 1] + 1;
- else common[i, j] = common[i - 1, j] > common[i, j - 1] ? common[i - 1, j] : common[i, j - 1];
- }
- }
- return common;
- }
- static string WriteLCS(int[,] c, string s1, string s2, int i, int j)
- {
- if (i == 0 || j == 0)
- return "";
- else if (s1[i - 1] == s2[j - 1])
- return WriteLCS(c, s1, s2, i - 1, j - 1) + s1[i - 1];
- else if (c[i, j - 1] > c[i - 1, j])
- return WriteLCS(c, s1, s2, i, j - 1);
- else
- return WriteLCS(c, s1, s2, i - 1, j);
- }
- static void Main(string[] args)
- {
- string s1 = "ABCBDAB";
- string s2 = "BDCABA";
- int[,] c = LongestCommonSubsequence(s1, s2);
- Console.WriteLine(WriteLCS(c,s1,s2,s1.Length,s2.Length));
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement