ksmk99

Substring(Main)

Jun 21st, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace substring
  6. {
  7.     public class Main
  8.     {
  9.         private int[,] matrix;
  10.         private List<string> substrings;
  11.         private List<Position> pos;
  12.  
  13.         public List<string> GetSubstring(string v1, string v2)
  14.         {
  15.             if (v1 == null || v2 == null) throw new ArgumentException();
  16.             if (v1.Length == 0 || v2.Length == 0) return new List<string>();
  17.             else if (v1.Equals(v2)) return new List<string>() { v1 };
  18.  
  19.             v1 = " " + v1;
  20.             v2 = " " + v2;
  21.  
  22.             pos = new List<Position>();
  23.             substrings = new List<string>();
  24.             matrix = new int[v1.Length, v2.Length];
  25.  
  26.             MatrixComplete(v1, v2);
  27.             Stringer(v1);
  28.  
  29.             return substrings;
  30.         }
  31.  
  32.         private void MatrixComplete(string v1, string v2)
  33.         {
  34.             var max = 0;
  35.             for (int i = 0; i < v1.Length; i++)
  36.             {
  37.                 for (int j = 0; j < v2.Length; j++)
  38.                 {
  39.                     if (i == 0 || j == 0) continue;
  40.                     if (v1[i] == v2[j])
  41.                     {
  42.                         if (matrix[i - 1, j - 1] == 0) matrix[i, j] = 1;
  43.                         else matrix[i, j] = matrix[i - 1, j - 1] + 1;
  44.  
  45.                         if (max < matrix[i, j])
  46.                         {
  47.                             max = matrix[i, j];
  48.                             pos.Clear();
  49.                             pos.Add(new Position(i, j, max));
  50.                         }
  51.                         else if (max == matrix[i, j]) pos.Add(new Position(i, j, max));
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.  
  57.  
  58.  
  59.         private void Stringer(string v1)
  60.         {
  61.             var substring = new StringBuilder();
  62.             for (int i = 0; i < pos.Count; i++)
  63.             {
  64.                 for (int j = pos[i].Lenght - 1; j > -1; j--) substring.Append(v1[pos[i].X - j]);
  65.                 substrings.Add(substring.ToString());
  66.                 substring.Clear();
  67.             }
  68.         }
  69.     }
  70.  
  71.     public class Position
  72.     {
  73.         public int X { get; }
  74.         public int Y { get; }
  75.         public int Lenght { get; }
  76.  
  77.         public Position(int x, int y, int lenght)
  78.         {
  79.             X = x;
  80.             Y = y;
  81.             Lenght = lenght;
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment