• 最长公共子子串 java


    package maxCommon;
    /**
     * 找到最长公共子串
     * @author root
     */
    public class MaxCommonUnSeries {
    	
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		
    		String s1 = "AGTA";
    		String s2 = "AGGCT";
    		//String s = find(s1.toCharArray(), 0, s2.toCharArray(), 0);
    		String s = find(s1.toCharArray(), s2.toCharArray());
    		System.out.println(s);
    	}
    	/*
    	 * 普通递归方法 O(2^n)  此处可以有两种实现方式,返回值 和  参数 首先通过返回值实现
    	 */
    	public static String find(char []s1, int s1_b, char []s2, int s2_b){
    		String temp1 = "";
    		String temp2 = "";
    		String temp3 = "";
    		String result = "";
    		if(s1_b>=s1.length || s2_b>= s2.length){
    			return "";
    		}
    		if(s1[s1_b]==s2[s2_b]){
    			temp1+= s1[s1_b];
    			//System.out.println("s1[s1_b]:"+s1[s1_b]+" "+"s1[s2_b]:"+s1[s2_b]);
    			String s = find(s1, s1_b+1, s2, s2_b+1);
    			temp1+= s;
    			//return temp;
    		}else{
    			temp2 = find(s1, s1_b+1, s2, s2_b);
    			temp3 = find(s1, s1_b, s2, s2_b+1);
    		}
    		result = temp1.length()>temp2.length()?temp1:temp2;
    		result = result.length()>temp3.length()?result:temp3;
    		//System.out.println("temp1:"+temp1+" "+"temp2:"+temp2+" "+"temp3:"+temp3+" ");
    		return result;
    	}
    <span style="white-space:pre">	</span>//找到最长公共子串  通过参数实现
    <span style="white-space:pre">	</span>public static String find1(String com_str, char []s1, int s1_b, char []s2, int s2_b){
    <span style="white-space:pre">		</span>String temp = "";
    <span style="white-space:pre">		</span>if(s1_b>=s1.length || s2_b>=s2.length){
    <span style="white-space:pre">			</span>return "";
    <span style="white-space:pre">		</span>}
    <span style="white-space:pre">		</span>if(s1[s1_b]==s2[s2_b]){
    <span style="white-space:pre">			</span>temp = com_str+s1[s1_b];
    <span style="white-space:pre">			</span>if(temp.length()>longest.length()){
    <span style="white-space:pre">				</span>longest = temp;
    <span style="white-space:pre">			</span>}
    <span style="white-space:pre">			</span>find1(temp ,s1, s1_b+1, s2, s2_b+1);
    <span style="white-space:pre">		</span>}else{
    <span style="white-space:pre">			</span>find1(com_str, s1, s1_b+1, s2, s2_b);
    <span style="white-space:pre">			</span>find1(com_str, s1, s1_b, s2, s2_b+1);
    <span style="white-space:pre">		</span>}
    <span style="white-space:pre">		</span>return "";
    <span style="white-space:pre">	</span>}
    	/*
    	 * 动态规划 O(n^2)
    	 */
    	public static String find(char []ss1, char []ss2){
    		char []s1 = ("0"+new String(ss1)).toCharArray();
    		char []s2 = ("0"+new String(ss2)).toCharArray();
    		int[][] path = new int[ss1.length+1][ss2.length+1];
    		int[][] b = new int[ss1.length+1][ss2.length+1];
    		for(int i=1; i<path.length; i++){
    			for(int j=1; j<path[i].length; j++){
    				if(s1[i]==s2[j]){
    					path[i][j]=path[i-1][j-1]+1;
    					b[i][j]=0;
    				}else if(path[i-1][j]>path[i][j-1]){
    					path[i][j]=path[i-1][j];
    					b[i][j]=1;
    				}else{
    					path[i][j]=path[i][j-1];
    					b[i][j]=-1;
    				}
    			}
    		}
    		findPath(s1, b, b.length-1, b[0].length-1);
    		return "";
    	}
    	/*
    	 * 动态规划-回溯法找路径
    	 */
    	public static void findPath(char []s1, int[][] b, int i, int j){
    		if(i==0 || j==0){
    			return;
    		}
    		if(b[i][j]==0){
    			findPath(s1, b, i-1, j-1);
    			System.out.print(s1[i]);
    		}else if(b[i][j]==1){
    			findPath(s1, b, i-1, j);
    		}else{
    			findPath(s1, b, i, j-1);
    		}
    	}
    }
    

  • 相关阅读:
    读书笔记-8《构建之法》
    结对编程收获
    结对编程——四则运算器(UI第十组)
    读书笔记-7《构建之法》
    读书笔记-6《构建之法》
    【现代软件工程】个人总结
    【现代软件工程】6月中旬团队项目心得
    【读书笔记】——《代码大全》(六)
    【现代软件工程】五月团队项目心得
    【读书笔记】——《代码大全》(五)
  • 原文地址:https://www.cnblogs.com/yan456jie/p/5369385.html
Copyright © 2020-2023  润新知