使用求最长公共子序列方法求字符串相似度 java 实现



import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;


public class StringSimilarityTest {
    private static final String ALL_LETTERS = "abcdefghijklmnopqrstuvwxyz";

    private static String[][] MAT = null;

    public static void main(String[] args) {
       // String string1 = UUID.randomUUID().toString();
       // String string2 = UUID.randomUUID().toString();

       // String string1 = "赫本风气质长裙小清新女2019春夏新款中长款波点雪纺连衣裙";
       // String string2 = "2019法式复古山本赫本风气质长裙小清新女夏裙子中长款连衣裙子";

       // String string1 ="hello";
       // String string2 ="hellosdsfdddddddddffffffffffffffffff";

       // String string1 = "mother";
       // String string2 = "monster";

       // String string1 = "7dfddi482l9bo9941v2db6e2ev3b1edbc2r091dy92maa0u53dc0c9h0844ec885eb2cf285fb5ca";
       // String string2 = "15i8lfbo397v7f4ec4m91u4bc1bh62784529f8a7d152d7d023331458fa8e7d3084e781f06";

        // String string1 = "deinlnoqvfheiryqyotdufyicldorvkejylotuniultouzvofegysoeuminljofvueqyioauhonvtalrbhgnpkwrrrpxjmvjoztpvldgwtluqeqjomliskikxmduemnopjeb";
        // String string2 = "pizlcobvyegimyloeuocoulzhiloveyouiloveyouiloveyouoreccselnfylvbyvxinarbyeasokwzyqubvqgcaqbzcmqatlmbevssmfoxtbmvgamucbmcspnppygyskqytwyj";

        String string1 = getRandomString( 100 );
        String string2 = getRandomString( 100 );
        String lcs_test = "iloveyouverymuchiloveyouverymuchiloveyouverymuchiloveyouverymuch";
        string1 = insertStr1ToStr2( lcs_test,string1 );
        string2 = insertStr1ToStr2( lcs_test,string2 );

        calculateStringSimilarity( string1,string2 );
    }

    private static String characterList2String( List characters ){
        StringBuilder sb = new StringBuilder("");
        for( Character character:characters ){
            sb.append( character );
        }
        return sb.toString();
    }

    private static List string2CharacterList( String str ){
        List characters = new ArrayList<>();
        int length = str.length();
        for( int i=0;i characters_str2 = string2CharacterList(str2);
        int length1 = str1.length();
        int index=0;
        Random random = new Random();
        for( int i=0;i index_max ){
                index = index_max;
            }
            characters_str2.add( index,str1.charAt( i ) );
            index += random.nextInt( 4 ) + 2;
        }
        return characterList2String( characters_str2 );
    }

    private static final String getRandomString( int len ){
        Random random = new Random();
        StringBuilder sb = new StringBuilder("");
        for( int i=0;i lcs2.length() ){
                                lcs = lcs1;
                            }else {
                                lcs = lcs2;
                            }
                        }
                    }
                }
                MAT[ i ][ j ] = lcs;
            }
        }
        return MAT[ string1.length() - 1 ][ string2.length() - 1 ];
    }

    private static String dropLastChar(String str) {
        return str.substring( 0,str.length() - 1 );
    }


    private static Character getLastChar(String str) {
        if( str == null || str.length() == 0 ){
            return null;
        }
        return  str.charAt(str.length() - 1);
    }

}
字符串1:iclhtbxorklwvaedyroqcurjyvtebbtragygmlmheuscyayehnzinootlqqoknmyvsxglebyntoroyxtxungvvwelrdqwpysksmkdnuwggwcumuzhltiyzlrwkonvxthgegujylzofuverymuchiloveyouverymuchs
字符串2:iplpvtoavcgfsezoityywwhotpitufvilvrtlxeyrnixbytimgbhuuwkgcjdhgninqnzlztomuvajbezcoxywdjorzxsumuvxersrmyxmcyjudrrscjxthdediwlpiovuwvcecsgyouverymuchiloveyouverymuchc
LCS:iltoveyourtebtghuchnintomvxyorxuverymdchliwvegyouverymuchiloveyouverymuch
lcs 和 字符串1 的相似度:45.000%
lcs 和 字符串2 的相似度:45.000%
字符串1 和 字符串2 的相似度:45.000%

你可能感兴趣的:(动态规划,最长公共子序列,字符串相似度,java,lcs,最长公共子序列,字符串相似度,动态规划)