博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA算法:最长重复子序列(JAVA)
阅读量:4039 次
发布时间:2019-05-24

本文共 2520 字,大约阅读时间需要 8 分钟。

JAVA算法:最长重复子序列(JAVA)

Longest Repeating Subsequence

Given a string, find length of the longest repeating subseequence such that the two subsequence don’t have same string character at same position, i.e., any i’th character in the two subsequences shouldn’t have the same index in the original string.


算法设计

package com.bean.algorithm.basic;public class LongestRepeatingSubsequence {	// Function to find the longest repeating subsequence	static int findLongestRepeatingSubSeq(String str) {		int n = str.length();		// Create and initialize DP table		int[][] dp = new int[n + 1][n + 1];		// Fill dp table (similar to LCS loops)		for (int i = 1; i <= n; i++) {			for (int j = 1; j <= n; j++) {				// If characters match and indexes are not same				if (str.charAt(i - 1) == str.charAt(j - 1) && i != j)					dp[i][j] = 1 + dp[i - 1][j - 1];				// If characters do not match				else					dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);			}		}		return dp[n][n];	}	// driver program to check above function	public static void main(String[] args) {		String str = "aabb";		System.out.println("The length of the largest subsequence that" + " repeats itself is : "				+ findLongestRepeatingSubSeq(str));	}}

程序运行结果:

The length of the largest subsequence that repeats itself is : 2

另外一种算法设计——递归算法 Recursion

package com.bean.algorithm.basic;import java.util.Arrays;public class LongestRepeatingSubsequence2 {	static int dp[][] = new int[1000][1000];	// This function mainly returns LCS(str, str)	// with a condition that same characters at	// same index are not considered.	static int findLongestRepeatingSubSeq(char X[], int m, int n) {		if (dp[m][n] != -1) {			return dp[m][n];		}		// return if we have reached the end of either string		if (m == 0 || n == 0) {			return dp[m][n] = 0;		}		// if characters at index m and n matches		// and index is different		if (X[m - 1] == X[n - 1] && m != n) {			return dp[m][n] = findLongestRepeatingSubSeq(X, m - 1, n - 1) + 1;		}		// else if characters at index m and n don't match		return dp[m][n] = Math.max(findLongestRepeatingSubSeq(X, m, n - 1), findLongestRepeatingSubSeq(X, m - 1, n));	}	// Longest Repeated Subsequence Problem	static public void main(String[] args) {		String str = "aabb";		int m = str.length();		for (int[] row : dp) {			Arrays.fill(row, -1);		}		System.out.println("The length of the largest subsequence that" + " repeats itself is : "				+ findLongestRepeatingSubSeq(str.toCharArray(), m, m));	}}

程序运行结果:

The length of the largest subsequence that repeats itself is : 2

 

转载地址:http://intdi.baihongyu.com/

你可能感兴趣的文章
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
测试必会之 Linux 三剑客之 sed
查看>>
Socket请求XML客户端程序
查看>>
Java中数字转大写货币(支持到千亿)
查看>>
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>