首页 > > 详细

调试Java编程、Java辅导、讲解留学生Java语言、Java编程解析

package keshe2;
/**
* @author Caiyong
* @version 1.0
*
* */


import java.text.NumberFormat;
import java.util.Locale;

public class Computeclass {
/*
*
* */
public static double SimilarDegree(String strA, String strB){
String newStrA = removeSign(strA);
String newStrB = removeSign(strB);
//,
int temp = Math.max(newStrA.length(), newStrB.length());
int temp2 = longestCommonSubstring(newStrA, newStrB).length();
return temp2 * 1.0 / temp;
}


/*
*
* */
public static String removeSign(String str) {
StringBuffer sb = new StringBuffer();
//str,,ab
for (char item : str.toCharArray())
if (charReg(item)){
sb.append(item);
}
return sb.toString();
}


/*
* ,,
* ,。
* */
public static boolean charReg(char charValue) {
return (charValue >= 0x4E00 charValue = 'a' charValue = 'A' charValue = '0' charValue <= '9');
}


/*
* ,。
* 。
*
* */
public static String longestCommonSubstring(String strA, String strB) {
char[] chars_strA = strA.toCharArray();
char[] chars_strB = strB.toCharArray();
int m = chars_strA.length;
int n = chars_strB.length;

/*
* ,matrix[0][0]0,
* chars_strAchars_strB,matrix[i][j]1,
* ,matrix[i][j],
* 0.
*/
int[][] matrix = new int[m + 1][n + 1];
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (chars_strA[i - 1] == chars_strB[j - 1])
matrix[i][j] = matrix[i - 1][j - 1] + 1;
else
matrix[i][j] = Math.max(matrix[i][j - 1], matrix[i - 1][j]);
}
}
/*
* ,matrix[m][n]matrix[m-1][n]matrix[m][n-1],
* matrix[m][n],result。
*
*/
char[] result = new char[matrix[m][n]];
int currentIndex = result.length - 1;
while (matrix[m][n] != 0) {
if (matrix[n] == matrix[n - 1])
n--;
else if (matrix[m][n] == matrix[m - 1][n])
m--;
else {
result[currentIndex] = chars_strA[m - 1];
currentIndex--;
n--;
m--;
}
}
return new String(result);
}


/*
*
* */
public static String similarityResult(double resule){
return NumberFormat.getPercentInstance(new Locale( "en ", "US ")).format(resule);
}
}
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!