Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _02.LongestCommonSeq
- {
- using System;
- using System.Linq;
- public class StartUp
- {
- private static int[][] lcs;
- private static char[] firstWord;
- private static char[] secondWord;
- public static void Main()
- {
- ReadInputInitialize();
- CountLCS();
- PrintResult();
- }
- private static void ReadInputInitialize()
- {
- firstWord = Console.ReadLine().ToCharArray();
- secondWord = Console.ReadLine().ToCharArray();
- lcs = new int[firstWord.Length + 1][];
- for (int row = 0; row < lcs.Length; row++)
- {
- lcs[row] = new int[secondWord.Length + 1];
- }
- }
- private static void PrintResult()
- {
- int max = 0;
- for (int row = 0; row < lcs.Length; row++)
- {
- int currentMax = lcs[row].Max();
- if(currentMax > max)
- {
- max = currentMax;
- }
- }
- Console.WriteLine(max);
- }
- private static void CountLCS()
- {
- for (int row = 1; row < lcs.Length; row++)
- {
- for (int col = 1; col < lcs[row].Length; col++)
- {
- char leftLetter = firstWord[row - 1];
- char rightLetter = secondWord[col - 1];
- int leftTopElement = lcs[row - 1][col - 1];
- int topElement = lcs[row - 1][col];
- int leftElement = lcs[row][col - 1];
- if (leftLetter == rightLetter)
- {
- lcs[row][col] = leftTopElement + 1;
- }
- else if(topElement > leftElement)
- {
- lcs[row][col] = topElement;
- }
- else
- {
- lcs[row][col] = leftElement;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment