1. 程式人生 > >輸出所有最長公用子序列的實現(Java)

輸出所有最長公用子序列的實現(Java)

package com.lcs;

import java.util.Scanner;
import java.util.TreeSet;

public class Lcs {
	
	public StringBuffer X;
	public StringBuffer Y;
	public static int m;
	public static int n;
	public int len;
	public static int[][] b;
	public int[][] c;
	public String[] Log;
	public char[] lcs;
	public final int MAX = 100;
	public int CurLen;
	
	public static void main(String[] args) {
		Lcs lcs = new Lcs();
		lcs.search();
		
	}
	public void search() {
		while(true) {
			System.out.println("\n" + "請選擇功能:");
			System.out.println("\n" + "1.尋找最長公共子序列");
			System.out.println("\n" + "2.結束");
			Scanner in = new Scanner(System.in);
			int choose = in.nextInt();
			switch(choose) {
			
			case 1:
				System.out.println("請輸入序列X:");
				X = new StringBuffer(in.next());
				System.out.println("請輸入序列Y:");
				Y = new StringBuffer(in.next());
				lcs_length();
				System.out.println("兩序列的最長公共子序列為:");
				storeLCS(m,n,len);
				PrintLCS();
				X.setLength(0);
				Y.setLength(0);
				break;
			case 2:
				in.close();
				System.exit(0);
				break;
			default:
				in.close();
				System.exit(-1);
			}
			
			
		}
	}
	public void lcs_length() {
		m = X.length();
		n = Y.length();
		c = new int[m+1][n+1];
		b = new int[m+1][n+1];
		for(int i = 1; i <= m; i++) c[i][0] = 0;
		for(int j = 0; j <= n; j++) c[0][j] = 0;
		for(int i = 1; i <= m; i++)
			for(int j = 1; j <= n; j++) {
				if(X.charAt(i-1) == Y.charAt(j-1)){
					c[i][j] = c[i-1][j-1] + 1;
					b[i][j] = 0;
				} else if(c[i-1][j] > c[i][j-1]) {
					c[i][j] = c[i-1][j];
					b[i][j] = 1;
				} else if(c[i-1][j] == c[i][j-1]){
					c[i][j] = c[i-1][j];
					b[i][j] = 2;
				}else {
					c[i][j] = c[i][j-1];
					b[i][j] = 3;
				}
			}
		lcs = new char[m+1];
		len = c[m][n];
		Log = new String[MAX];
		CurLen = 0;
	}
	public void storeLCS(int m,int n,int Len) {
		if(m == 0 || n == 0) {
			Log[CurLen] = new String(lcs);	
			CurLen++;
		} else {
			if(b[m][n] == 0) {
				lcs[Len] = X.charAt(m-1);
				Len--;
				storeLCS(m-1,n-1,Len);
			} else if(b[m][n] == 3) {
				storeLCS(m,n-1,Len);
			} else if(b[m][n] == 1) {
				storeLCS(m-1,n,Len);
			} else {
				storeLCS(m,n-1,Len);
				storeLCS(m-1,n,Len);
			}
		}
		
	}
	public void PrintLCS() {
		TreeSet<String> tr = new TreeSet<String>();
		for(int i = 0; i < CurLen; i++) {
			tr.add(Log[i]);
		}
		String[] s2= new String[tr.size()];
		for(int i = 0; i < s2.length; i++) {
			 s2[i]=tr.pollFirst();//從TreeSet中取出元素重新賦給陣列
			 System.out.println(s2[i]);
		}
	}
}