求两个串的最大公共子序列的长度

 1 public class CommonSubsequence {
 2
 3     public static int f(String s1,String s2){
 4
 5         if(s1.length()==0||s2.length()==0) return 0;
 6
 7         if(s1.charAt(0)==s2.charAt(0))
 8             return f(s1.substring(1),s2.substring(1))+1;
 9         else
10             return Math.max(f(s1.substring(1),s2), f(s1,s2.substring(1)));
11
12     }
13
14
15     public static void main(String[] args) {
16         int k =f("abc","xbacd");
17         System.out.println(k);
18     }
19
20 }

Notes:

①String s.charAt(int index)返回字符串中下标为index的字符;

String s.subString(int beginIndex,int endIndex)返回字符串的子字符串,包括下标为beginIndex,不包括下标为endIndex;

Math类中的方法都被定义为static形式,Math类可以在主函数中直接调用;

Math.max(int a,int b)返回两个参数中的最大值,参数可以是float、byte、double类型;

②使用递归只能求出其长度,相比列举序列要简单许多(动态规划);递归步骤过于复杂;

③字符串a:xasfas    字符串b:as

  字符串a的第一个字符==字符串b的第一个字符 -->> 两遍都剩余的字符串求公共子序列长度+1

  字符串a的第一个字符!=字符串b的第一个字符 -->>a除第一个字符以外和b所求公共子序列长度

                       -->>a和b除第一个字符以外所求公共子序列长度

  a或b序列长度为0时-->>0(出口)

原文地址:https://www.cnblogs.com/foxobedient/p/8516904.html

时间: 2024-08-01 11:13:40

求两个串的最大公共子序列的长度的相关文章

数据结构——算法之(032)(求两个串中的第一个最长子串)

[申明:本文仅限于自我归纳总结和相互交流,有纰漏还望各位指出. 联系邮箱:[email protected]] 题目: 求两个串中的第一个最长子串(神州数码曾经试题).如"abractyeyt","dgdsaeactyey"的最大子串为"actyey". 题目分析: 1.这里仅仅是实现了简单的字符串算法(最大支持字符串长度64),主要是展示算法思想 2.思路是把2个字符串每一个字符的匹配关系,映射到一张二维数组表中,匹配写1,非匹配写0 算法实现

求两个串的最大子序列(非字串)

问题:求两个串的最大子序列(并非连接的) Java代码: import java.util.Set; import java.util.StringJoiner; public class Main { public static int getL(String a, String b) { if (a.isEmpty()||b.isEmpty()) return 0; if (a.charAt(0) == b.charAt(0)) return getL(a.substring(1), b.s

SPOJ 1811 Longest Common Substring(求两个串的最长公共子串)

http://www.spoj.com/problems/LCS/ 题目:求两个串的最长公共子串 分析: 以A建立SAM 让B在SAM上匹配可以类比于kmp思想,我们知道在Parent树上,fa是当前节点的子集,也就是说满足最大前缀,利用这个就可以做题了 #include <bits/stdc++.h> #define LL long long #define P pair<int, int> #define lowbit(x) (x & -x) #define mem(a

求两个串的最大公共子串

 给定一个query和一个text,均由小写字母组成.要求在text中找出以同样的顺序连续出现在query中的最长连续字母序列的长度.例如,query为 "acbac",text为"acaccbabb",那么text中的"cba"为最长的连续出现在query中的字母序列,因此,返回结果应该为其长度3. int getLongestSubString(char* query, char* text) { int imax = INT_MIN; in

UVA 题目760 DNA Sequencing (后缀数组求两个串最长公共子串,字典序输出)

 DNA Sequencing  A DNA molecule consists of two strands that wrap around each other to resemble a twisted ladder whose sides, made of sugar and phosphate molecules, are connected by rungs of nitrogen-containing chemicals called bases. Each strand is

POJ 2774 求两个串的最长公共前缀 | 后缀数组

#include<cstdio> #include<algorithm> #include<cstring> #define N 200005 using namespace std; int buf1[N],buf2[N],sa[N],rnk[N],buc[N],n,height[N],ans,belong[N]; char s[N]; void suffix_sort() { int *x=buf1,*y=buf2,m=1000; for (int i=0;i<

leetcode 159. Longest Substring with At Most Two Distinct Characters 求两个字母组成的最大子串长度 --------- java

Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example, Given s = "eceba", T is "ece" which its length is 3. 给一个字符串,求这个字符串中,由两个字母组成的,连续的最长子串的长度. 虽然是hard,但是感觉并没有什么难度. 用ch1和pre

hdu1159 最大公共子序列

题意就不用多说了       这道题就是求两个串的最大公共子序列  注意与最大公共子串的区别 最长公共子序列的结构 最长公共子序列的结构有如下表示: 设序列X=<x1, x2, -, xm>和Y=<y1, y2, -, yn>的一个最长公共子序列Z=<z1, z2, -, zk>,则: 1> 若 xm=yn,则 zk=xm=yn,且Zk-1是Xm-1和Yn-1的最长公共子序列: 2> 若 xm≠yn且 zk≠xm ,则 Z是 Xm-1和 Y的最长公共子序列:

ACM最大公共子序列&amp;&amp;回文串

---恢复内容开始--- Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be in