递归求最大子串序列长度
import java.util.Scanner;
/**
* Created by junyi.pc on 2017/1/25.
*/
//求两个字符串最长子序列的长度
// 比如abcde 的子序列是a或ab或ad或abcd不一定要连续
public class Main {
public static int f(String a,String b){
if(a.length()==0||b.length()==0)
return 0;
if(a.charAt(0)==b.charAt(0)){
return f(a.substring(1),b.substring(1))+1;
}
else{
return Math.max(f(a.substring(1),b),f(a,b.substring(1)));
}
}
public static void main(java.lang.String[] args){
String a = "abcd";
String b="xabcd";
System.out.print(f(a,b));
}
}
运行结果
C:\java\jdk1.8.0_25\bin\java -Didea.launcher.port=7535 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\java\jdk1.8.0_25\jre\lib\charsets.jar;C:\java\jdk1.8.0_25\jre\lib\deploy.jar;C:\java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\java\jdk1.8.0_25\jre\lib\javaws.jar;C:\java\jdk1.8.0_25\jre\lib\jce.jar;C:\java\jdk1.8.0_25\jre\lib\jfr.jar;C:\java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\java\jdk1.8.0_25\jre\lib\jsse.jar;C:\java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\java\jdk1.8.0_25\jre\lib\plugin.jar;C:\java\jdk1.8.0_25\jre\lib\resources.jar;C:\java\jdk1.8.0_25\jre\lib\rt.jar;C:\Users\junyi.pc\Desktop\client\out\production\client;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
4
Process finished with exit code 0
网友评论