Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class al8b {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- while(in.hasNext()) {
- String str1 = in.nextLine();
- String str2 = in.nextLine();
- int tam1 = str1.length();
- int tam2 = str2.length();
- String nstr;
- int cont = 0;
- System.out.println(lcs(str1, tam1, str2, tam2, cont));
- /*for(int i = str1.length() - 1; i >= 0; i--) {
- char c = str1.charAt(i);
- String n = "" + c;
- for(int j = tam2 - 1; j >= 0; j--) {
- if(str2.charAt(j) == c) {
- str2 = str2.replaceFirst(n, "");
- cont++;
- tam2--;
- j = -1;
- } else if(j == 0) {
- str2 = str2.substring(0, tam2 - 1);
- tam2--;
- }
- }
- }*/
- //System.out.println(cont);
- }
- }
- public static int lcs(String str1, int tam1, String str2, int tam2, int res) {
- if(tam1 == 0 || tam2 == 0) {
- return res;
- } else if(str1.charAt(tam1 - 1) == str2.charAt(tam2 - 1)) {
- str1 = str1.substring(0, tam1 - 1);
- str2 = str2.substring(0, tam2 - 1);
- tam1--;
- tam2--;
- res = res + 1 + lcs(str1, tam1, str2, tam2, res);
- } else {
- if(lcs(str1.substring(0, tam1 - 1), tam1 - 1, str2, tam2, res) > lcs(str1, tam1, str2.substring(0, tam2 - 1), tam2 - 1, res)) {
- res = res + lcs(str1.substring(0, tam1 - 1), tam1 - 1, str2, tam2, res);
- } else {
- res = res + lcs(str1, tam1, str2.substring(0, tam2 - 1), tam2 - 1, res);
- }
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement