備注學院

          LuLu

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            5 隨筆 :: 50 文章 :: 16 評論 :: 0 Trackbacks
          用算法中的求最大相似子字符串的方法LCS或許可以,它可以找到兩個字符串中最大相似的子字符串

          Java code
          /* * @author talent_marquis<甜菜侯爵> * Email: talent_marquis@163.com * Copyright (C) 2007 talent_marquis<甜菜侯爵> * All rights reserved. */ package ustc.mse.algorithms.dynamicProgramming; /* * LCS, Longest-Common-Subsequence */ public class LCS { public enum DIRECTION{ TOP, TOP_LEFT, LEFT }; private char[] first; private char[] second; private int[][] lcsTable; private DIRECTION[][] lcsAssistTable; private int lcsLength; private String lcs_str, lcsMatrix_str, lcsAssistMatrix_str; private StringBuffer str_buffer; public LCS( String str1, String str2 ) { first = str1.toCharArray(); second = str2.toCharArray(); lcsTable = new int[ first.length + 1 ][ second.length + 1 ]; lcsAssistTable = new DIRECTION[ first.length + 1 ][ second.length + 1]; lcs_str = null; str_buffer = new StringBuffer(); } public static void main(String[] args) { String a = "我抄我抄我抄抄抄:明月幾時有,把酒問青天,不知天上宮闕,今夕是何年"; String b = "蘇軾曾經寫過“明月幾時有,把酒問青天”的千古名句"; LCS lcs = new LCS( a, b ); lcs.getLCSLength(); lcs.runLCS(); println( "最大相似子字符串長度是:" + lcs.getLCSLength() ); println( "最大相似子字符串為:" + lcs.getLCS() ); } public int getLCSLength() { lcsLength = getLCSLength( first, second ); return lcsLength; } private int getLCSLength( char[] one, char[] two ) { lcsTable = new int[ one.length + 1 ][ two.length + 1 ]; lcsAssistTable = new DIRECTION[ one.length + 1 ][ two.length + 1]; for ( int i = 0; i < one.length ; i++ ) { lcsTable[ i ][ 0 ] = 0; } for ( int j = 0; j < two.length - 1; j++ ) { lcsTable[ 0 ][ j ] = 0; } for ( int i = 0; i < one.length; i++ ) { for ( int j = 0; j < two.length; j++ ) { if ( one[ i ] == two[ j ] ) { lcsTable[ i + 1 ][ j + 1 ] = lcsTable[ i ][ j ] + 1; lcsAssistTable[ i + 1 ][ j + 1 ] = DIRECTION.TOP_LEFT; } else if ( lcsTable[ i ][ j + 1 ] >= lcsTable[ i + 1 ][ j ] ) { lcsTable[ i + 1 ][ j + 1 ] = lcsTable[ i ][ j + 1 ]; lcsAssistTable[ i + 1 ][ j + 1 ] = DIRECTION.TOP; } else { lcsTable[ i + 1 ][ j + 1 ] = lcsTable[ i + 1 ][ j ]; lcsAssistTable[ i + 1 ][ j + 1 ] = DIRECTION.LEFT; } } } lcsLength = lcsTable[ one.length ][ two.length ]; return lcsLength; } public void runLCS() { runLCS( lcsAssistTable, first, first.length, second.length ); lcs_str = str_buffer.toString(); } private void runLCS( DIRECTION[][] lcsAssistTable, char[] one, int oneLength, int twoLength ) { if( oneLength == 0 || twoLength == 0 ) { return; } int i = oneLength ; int j = twoLength ; if( lcsAssistTable[ i ][ j ] == DIRECTION.TOP_LEFT ) { runLCS( lcsAssistTable, one, i - 1, j -1 ); str_buffer.append( one[ i - 1 ] ); } else if ( lcsAssistTable[ i ][ j ] == DIRECTION.TOP ) { runLCS( lcsAssistTable, one, i - 1, j ); } else { runLCS( lcsAssistTable, one, i, j -1 ); } } public String getLCSAssistMatrixString() { str_buffer = new StringBuffer(); for( DIRECTION[] row: lcsAssistTable) { for( DIRECTION element : row ) { if( element == DIRECTION.LEFT ) { str_buffer.append( "?? " ); } else if (element == DIRECTION.TOP ) { str_buffer.append( "?? " ); } else if (element == DIRECTION.TOP_LEFT) { str_buffer.append( "?I " ); } else { //str_buffer.append( "\t" ); } } str_buffer.append( "\n" ); } lcsAssistMatrix_str = str_buffer.toString(); return lcsAssistMatrix_str; } public String getLCSMatrixString() { str_buffer = new StringBuffer(); for( int[] row: lcsTable) { for( int element : row ) { str_buffer.append( element + " " ); } str_buffer.append( "\n" ); } lcsMatrix_str = str_buffer.toString(); return lcsMatrix_str; } public static void print( Object o ) { System.out.print( o ); } public static void println( Object o ) { System.out.println( o ); } public String getLCS() { return lcs_str; } /** * @return first */ public char[] getFirstCharArray() { return first; } /** * @return second */ public char[] getSecondCharArray() { return second; } /** * @return lcsAssistTable */ public DIRECTION[][] getLcsAssistTable() { return lcsAssistTable; } /** * @return lcsTable */ public int[][] getLcsTable() { return lcsTable; } }
          posted on 2008-11-04 10:02 smildlzj 閱讀(3615) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 两当县| 黄浦区| 平顶山市| 泰来县| 县级市| 邵武市| 松阳县| 灯塔市| 鸡东县| 富蕴县| 绿春县| 高台县| 叙永县| 阜南县| 岐山县| 勃利县| 施甸县| 乳山市| 白沙| 遂川县| 元朗区| 姜堰市| 西充县| 精河县| 延寿县| 崇阳县| 临西县| 侯马市| 吴江市| 龙门县| 平乐县| 永靖县| 新乐市| 斗六市| 呼图壁县| 金塔县| 永川市| 宁阳县| 扶余县| 应用必备| 昆明市|