zoukankan      html  css  js  c++  java
  • 最长公共子序列LCS

     1 public class Zuichangzixulie {
     2     //X[n] 取哪些
     3     //Y[m] 取哪些
     4 //    int search(int xi, int yi){
     5 //        int n ,m;
     6 //        if(xi>=n || yi>=m) return -1;
     7 //        if(xi == n && yi == m) return 0;
     8 //        return Math.max(
     9 //        if (X[xi] == Y[yi] search(xi+1, yi+1) + 1,
    10 //        search(xi, yi+1),
    11 //        search(xi+1, yi)
    12 //        );
    13 //        
    14 //    }
    15     public static int lcs(String a,String b){
    16         int len1 = a.length();
    17         int len2 = b.length();
    18         int c[][] = new int[len1+1][len2+1];
    19         for(int i = 1; i <= len1;i++){
    20             for(int j = 1; j <= len2;j++){
    21                 if(i == 0 || j == 0){c[i][j] = 0;}
    22                  if(a.charAt(i-1) == b.charAt(j-1)){
    23                     c[i][j] = c[i-1][j-1]+1;
    24                     //System.out.println(c[i][j]);
    25                 }else{
    26                     c[i][j] = Math.max(c[i-1][j],c[i][j-1]);
    27                 }
    28             }
    29         }
    30         return c[len1][len2];
    31         
    32         
    33     }
    34     public static void main(String[] args) {
    35         // TODO Auto-generated method stub
    36         String a = "1A2C3D4B56";
    37         String b = "B1D23CA45B6A";
    38     
    39         System.out.println(lcs(a, b));
    40     }
    41 
    42 }

    例题:

    1、对于两个字符串,请设计一个高效算法,求他们的最长公共子序列的长度,这里的最长公共子序列定义为有两个序列U1,U2,U3...Un和V1,V2,V3...Vn,其中Ui&ltUi+1,Vi&ltVi+1。且A[Ui] == B[Vi]。

    给定两个字符串A和B,同时给定两个串的长度n和m,请返回最长公共子序列的长度。保证两串长度均小于等于300。

    测试样例:
    "1A2C3D4B56",10,"B1D23CA45B6A",12
    返回:6

    2、

    我们有两个字符串m和n,如果它们的子串a和b内容相同,则称a和b是m和n的公共子序列。子串中的字符不一定在原字符串中连续。
    例如字符串“abcfbc”和“abfcab”,其中“abc”同时出现在两个字符串中,因此“abc”是它们的公共子序列。此外,“ab”、“af”等都是它们的字串。
    现在给你两个任意字符串(不包含空格),请帮忙计算它们的最长公共子序列的长度。

    输入描述:

    输入包含多组数据。

    每组数据包含两个字符串m和n,它们仅包含字母,并且长度不超过1024。

    输出描述:

    对应每组输入,输出最长公共子序列的长度。
    示例1

    输入

    abcfbc abfcab
    programming contest
    abcd mnp

    输出

    4
    2
    0
     1 import java.util.Scanner;
     2 public class ZuichangLCS {
     3     public static int lcs(String a,int n,String b, int m){
     4         n = a.length();
     5         m = b.length();
     6         int c[][] = new int[n+1][m+1];
     7         for(int i = 1; i <= n;i++){
     8             for(int j = 1; j <= m;j++){
     9                 if(i == 0 || j == 0){c[i][j] = 0;}
    10                  if(a.charAt(i-1) == b.charAt(j-1)){
    11                     c[i][j] = c[i-1][j-1]+1;
    12                     //System.out.println(c[i][j]);
    13                 }else{
    14                     c[i][j] = Math.max(c[i-1][j],c[i][j-1]);
    15                 }
    16             }
    17         }
    18         return c[n][m];
    19         
    20         
    21     }
    22     public static void main(String[] args) {
    23         // TODO Auto-generated method stub
    24         Scanner sc = new Scanner(System.in);
    25         while(sc.hasNext()){
    26             String a = sc.next();
    27             String b = sc.next();
    28             System.out.println(lcs(a,a.length(),b,b.length()));
    29         }
    30     }
    31 
    32 }
    
    
  • 相关阅读:
    jQuery EasyUI API 中文文档 可调整尺寸
    jQuery EasyUI API 中文文档 链接按钮(LinkButton)
    jQuery EasyUI API 中文文档 手风琴(Accordion)
    jQuery EasyUI API 中文文档 表单(Form)
    jQuery EasyUI API 中文文档 组合(Combo)
    jQuery EasyUI API 中文文档 布局(Layout)
    jQuery EasyUI API 中文文档 拆分按钮(SplitButton)
    jQuery EasyUI API 中文文档 菜单按钮(MenuButton)
    jQuery EasyUI API 中文文档 搜索框
    jQuery EasyUI API 中文文档 验证框(ValidateBox)
  • 原文地址:https://www.cnblogs.com/zlz099/p/8796615.html
Copyright © 2011-2022 走看看