Advertisement
Guest User

Algorytmika

a guest
May 27th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package CzescWspolna;
  2.  
  3. public class CzescWspolna {
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.         String text1 = "AABABB";
  8.         String text2 = "BAABAB";
  9.          
  10.         int len1 = text1.length();
  11.         int len2 = text2.length();
  12.        
  13.         int max = 0;
  14.         int position_w1 = -1;
  15.         int position_w2 = -1;
  16.        
  17.         for (int i = 0; i < len1 - max; i++)
  18.         {
  19.  
  20.             for (int k = len2 - 1; k >= 0; k--)
  21.             {
  22.                 int counter = 0;
  23.                 int limit = Math.min(len2, (len1 -i + k) );  // okresla ile maksymalnie znakow mozemy sprawdzic w zaleznosci od przesuniecia, mozna zastapic instrukcja IF [if( (i+j-k) >= dl2) break;] wewnatrz kolejnej petli
  24.                 for (int j = k; j < limit; j++)
  25.                 {
  26.                     if (text1.charAt(i+j-k) == text2.charAt(j))
  27.                     {                        
  28.                         counter++;
  29.                        
  30.                         if (max < counter)
  31.                         {
  32.                             max = counter;
  33.                             position_w1 = i + j - k - max + 1;
  34.                             position_w2 = j - max + 1;
  35.                         }
  36.                     }
  37.                     else counter = 0;
  38.                 }
  39.             }
  40.         }
  41.        
  42.         System.out.println("Position of text1: " + position_w1 + ", position of text2: " + position_w2 + ", length: " + max);
  43.        
  44.  
  45.                        
  46.         System.out.println(text1.substring(0, position_w1) + "\u001B[31m"
  47.                 + text1.substring(position_w1, max + position_w1) + "\u001B[0m" + text1.substring(max + position_w1) );
  48.         System.out.println(text2.substring(0, position_w2) + "\u001B[31m"
  49.                 + text2.substring(position_w2, max + position_w2) + "\u001B[0m" + text2.substring(max + position_w2) );
  50.     }
  51.    
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement