Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CzescWspolna;
- public class CzescWspolna {
- public static void main(String[] args) {
- String text1 = "AABABB";
- String text2 = "BAABAB";
- int len1 = text1.length();
- int len2 = text2.length();
- int max = 0;
- int position_w1 = -1;
- int position_w2 = -1;
- for (int i = 0; i < len1 - max; i++)
- {
- for (int k = len2 - 1; k >= 0; k--)
- {
- int counter = 0;
- 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
- for (int j = k; j < limit; j++)
- {
- if (text1.charAt(i+j-k) == text2.charAt(j))
- {
- counter++;
- if (max < counter)
- {
- max = counter;
- position_w1 = i + j - k - max + 1;
- position_w2 = j - max + 1;
- }
- }
- else counter = 0;
- }
- }
- }
- System.out.println("Position of text1: " + position_w1 + ", position of text2: " + position_w2 + ", length: " + max);
- System.out.println(text1.substring(0, position_w1) + "\u001B[31m"
- + text1.substring(position_w1, max + position_w1) + "\u001B[0m" + text1.substring(max + position_w1) );
- System.out.println(text2.substring(0, position_w2) + "\u001B[31m"
- + text2.substring(position_w2, max + position_w2) + "\u001B[0m" + text2.substring(max + position_w2) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement