Given two strings, find the longest common subsequence (LCS).

Your code should return the length of LCS.

Example

For "ABCD" and "EDCA", the LCS is "A" (or "D""C"), return 1.

For "ABCD" and "EACB", the LCS is "AC", return 2.

分析:

典型的DP。

  1. public class Solution {
  2. /**
  3. * @param A, B: Two strings.
  4. * @return: The length of longest common subsequence of A and B.
  5. */
  6. public int longestCommonSubsequence(String str1, String str2) {
  7. if (str1 == null || str2 == null) return ;
  8. int[][] opt = new int[str2.length() + ][str1.length() + ];
  9.  
  10. for (int j = ; j <= str1.length(); j++) {
  11. for (int i = ; i <= str2.length(); i++) {
  12. if (i == || j == ) {
  13. opt[i][j] = ;
  14. } else if (str2.charAt(i-) == str1.charAt(j-)) {
  15. opt[i][j] = opt[i-][j-] + ;
  16. } else {
  17. opt[i][j] = Math.max(opt[i-][j], opt[i][j-]);
  18. }
  19. }
  20. }
  21. return opt[str2.length()][str1.length()];
  22. }
  23. }

Longest Common Substring

Given two strings, find the longest common substring.

Return the length of it.

Notice

The characters in substring should occur continuously in original string. This is different with subsequence.

Example

Given A = "ABCD", B = "CBCE", return 2.

分析:

从头比到尾呗。

  1. public class Solution {
  2. /**
  3. * @param A, B: Two string.
  4. * @return: the length of the longest common substring.
  5. */
  6. public int longestCommonSubstring(String A, String B) {
  7. if (A == null || B == null || A.length() == || B.length() == ) return ;
  8. int max = ;
  9. for (int i = ; i < B.length(); i++) {
  10. for (int j = ; j < A.length(); j++) {
  11. int incr = ;
  12. while (i + incr < B.length() && j + incr < A.length() && (B.charAt(i + incr) == A.charAt(j + incr))) {
  13. incr++;
  14. max = Math.max(max, incr);
  15. }
  16. }
  17. }
  18. return max;
  19. }
  20. }

Longest Common Prefix

Given k strings, find the longest common prefix (LCP).

Example

For strings "ABCD""ABEF" and "ACEF", the LCP is "A"

For strings "ABCDEFG""ABCEFG" and "ABCEFA", the LCP is "ABC"

分析:

取出第一个string和剩余的string相比即可。

  1. public class Solution {
  2. /**
  3. * @param strs: A list of strings
  4. * @return: The longest common prefix
  5. */
  6. public String longestCommonPrefix(String[] strs) {
  7. if (strs == null || strs.length == ) return "";
  8. if (strs.length == ) return strs[];
  9.  
  10. StringBuilder sb = new StringBuilder();
  11.  
  12. for (int j = ; j < strs[].length(); j++) {
  13. for (int i = ; i < strs.length; i++) {
  14. if (j >= strs[i].length() || strs[i].charAt(j) != strs[].charAt(j)) {
  15. return sb.toString();
  16. }
  17. }
  18. sb.append(strs[].charAt(j));
  19. }
  20. return sb.toString();
  21. }
  22. }

Longest Common Subsequence & Substring & prefix的更多相关文章

  1. lintcode 77.Longest Common Subsequence(最长公共子序列)、79. Longest Common Substring(最长公共子串)

    Longest Common Subsequence最长公共子序列: 每个dp位置表示的是第i.j个字母的最长公共子序列 class Solution { public: int findLength ...

  2. Longest common subsequence(LCS)

    问题 说明该问题在生物学中的实际意义 Biological applications often need to compare the DNA of two (or more) different ...

  3. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  4. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  5. [Algorithms] Longest Common Subsequence

    The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the le ...

  6. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  7. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  8. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  9. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

随机推荐

  1. nginx web加密访问

    有时我们会有这么一种需求,就是你的网站并不想提供一个公共的访问或者某些页面不希望公开, 我们希望的是某些特定的客户端可以访问.那么我们可以在访问时要求进行身份认证,就如给你自己的家门加一把锁,以拒绝那 ...

  2. Freemarker-标签使用

     FreeMarker标签使用  FreeMarker模板文件主要有4个部分组成 1.文本,直接输出的部分 2.注释,即<#--...-->格式不会输出 3.插值(Interpolatio ...

  3. C#给文件夹添加权限

    //==== //添加权限 private void SetAttributes(string folder) { if (folder == "" || !Directory.E ...

  4. 【kAri OJ620】winoros的树

    时间限制 1000 ms 内存限制 65536 KB 题目描述 winoros 是一个热爱大自然的萌妹子,在植树节的时候,她打算带着集训的朋友们一起去种树. 到了种树的地方,学校给了她们四个不可弯曲. ...

  5. BZOJ-1880 Elaxia的路线 SPFA+枚举

    1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 921 Solved: 354 [Submit][Sta ...

  6. TYVJ1939 玉蟾宫

    背景 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. 描述 这片土地被分成N*M个格子,每个格子里写着'R'或者'F',R代 ...

  7. MySQL------如何安装mysql-connector-java-5.1.38.zip

    下载地址:http://dev.mysql.com/downloads/connector/j/ 安装mysql-connector-java-5.1.38.zip:1.解压文件->把里面的my ...

  8. BurpSuite实例教程

    很久以前就看到了Burp suite这个工具了,当时感觉好NB,但全英文的用起来很是蛋疼,网上也没找到什么教程,就把这事给忘了.今天准备开始好好学习这个渗透神器,也正好给大家分享下.(注:内容大部分是 ...

  9. WPF 组合快捷键

    if(e.KeyStates == Keyboard.GetKeyStates(Key.F4) && Keyboard.Modifiers == ModifierKeys.Alt) { ...

  10. Swift翻译之-Swift语法入门 Swift语法介绍

    目录[-] Hello world - Swift 简单赋值 控制流 函数与闭包 对象和类 枚举与结构 协议和扩展 泛型 2014.6.3日,苹果公布最新编程语言Swift,Swift是一种新的编程语 ...