一个串的子串是指该串的一个连续的局部。如果不要求连续,则可称为它的子序列。
比如对串: “abcdefg” 而言,”ab”,”abd”,”bdef” 等都是它的子序列。
特别地,一个串本身,以及空串也是它的子序列。
对两个串而言,可以有许多的共同的子序列,我们关心的是:它们所共同拥有的长度最大的子序列是多长。以下代码实现了这个问题的求解。请填写划线部分缺失的代码。
注意:只填写缺少的代码,不要写任何多余的内容,比如注释或说明文字。
public class Zixulie
{
public static int f(String x, String y)
{
if(x.length()==0) return 0;
if(y.length()==0) return 0;
String x1 = x.substring(1);
String y1 = y.substring(1);
if(x.charAt(0)==y.charAt(0))
return f(x1,y1)+1;
return f(x1,y1);
}
public static void main(String[] args)
{
System.out.println(f("ac","abcd")); //2
System.out.println(f("acebbcde1133","xya33bc11de")); //5
}
}
答案:0
charAt:
charAt(int index)方法是一个能够用来检索特定索引下的字符的String实例的方法,charAt()方法返回指定索引位置的字符值。索引范围为0~length()-1,如:str.charAt(0)检索str中的第一个字符,str.charAt(str.length()-1)检索最后一个字符。
public String substring(int beginIndex),一般用于返回一个新的字符串,它是此字符串的一个子字符串。该子字符串始于指定索引处的字符,一直到此字符串末尾。