一个串的子串是指该串的一个连续的局部。如果不要求连续,则可称为它的子序列。 比如对串: "abcdefg" 而言,"ab","abd","bdef" 等都是它的子序列。 特别地,一个串本身,以及空串也是它的子序列。 对两个串而言,可以有许多的共同的子序列,我们关心的是:它们所共同拥有的长度最大的子序列是多长。
1 package lj; 2 public class Zixulie { 3 public static int f(String x,String y) 4 { 5 if(x.length()==0) return 0; 6 if(y.length()==0) return 0; 7 String x1 = x.substring(1); 8 String y1 = y.substring(1); 9 if(x.charAt(0)==y.charAt(0)) return f(x1,y1)+1; 10 return f(x,y1)>f(x1,y)?f(x,y1):f(x1,y); 11 } 12 public static void main(String[] args) 13 { 14 System.out.println(f("ac","abcd")); //2 15 System.out.println(f("acebbcde1133","xya33bc11de")); //5 16 } 17 }
浙公网安备 33010602011771号