-
Logging started for id:1 Module ABC has completed its job Module XYZ has completed its job Logging started for id:10 Module ? has completed its job
样例输出
-
3
Description
In a running system, there are many logs produced within a short period of time, we'd like to know the count of the most frequent logs.
Logs are produced by a few non-empty format strings, the number of logs is N(1<=N<=20000), the maximum length of each log is 256.
Here we consider a log same with another when their edit distance (see note) is <= 5.
Also we have a) logs are all the same with each other produced by a certain format string b) format strings have edit distance 5 of each other.
Your program will be dealing with lots of logs, so please try to keep the time cost close to O(nl), where n is the number of logs, and l is the average log length.
Note edit distance is the minimum number of operations (insertdeletereplace a character) required to transform one string into the other, please refer to
http://en.wikipedia.org/wiki/Edit_distance for more details.
Input
Multiple lines of non-empty strings.
Output
The count of the most frequent logs.
//source here
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main{
static int count = 0,maxcount = 0;
static List<String> list = new ArrayList<String>();
public static void main(String[] args) {
int N = 0;
int i=0,j=0,k=0;
Boolean flag = false;
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);//不会检测输入越界的问题,但是注意整型上限,最好将变量定义为long长整型
do{
String line = in.nextLine();
if( line==null || "".equals(line.toString())){
flag = true;
}else{
list.add(line);
}
}while(!flag && N<20000);
for(i=0;i<list.size()-1;i++){
for(j=i+1;j<list.size();j++){
String strA = list.get(i);
String strB = list.get(j);
int result=edit_distance_Result(strA, strB);
if(result <= 5){
count++;
}
}
if(maxcount<count){
maxcount = count;
}
count = 0;
}
System.out.println(++maxcount);
}
/**
* 相似度比较
* @param strA
* @param strB
* @return
*/
public static int edit_distance_Result(String strA, String strB){
String newStrA = removeSign(strA);
String newStrB = removeSign(strB);
int temp = Math.max(newStrA.length(), newStrB.length());
int temp2 = compare(newStrA, newStrB); //比较两个字符串,返回未匹配的字符串个数
//System.out.println("temp2 == "+temp2);
return (temp2);
}
private static int compare(String str, String target) {//
int d[][]; // 矩阵
int n = str.length();
int m = target.length();
int i; // 遍历str的
int j; // 遍历target的
char ch1; // str的
char ch2; // target的
int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1
if (n == 0) {
return m;
}
if (m == 0) {
return n;
}
d = new int[n + 1][m + 1];
for (i = 0; i <= n; i++) { // 初始化第一列
d[i][0] = i;
}
for (j = 0; j <= m; j++) { // 初始化第一行
d[0][j] = j;
}
for (i = 1; i <= n; i++) {// 遍历str
ch1 = str.charAt(i - 1);
// 去匹配target
for (j = 1; j <= m; j++){
ch2 = target.charAt(j - 1);
if (ch1 == ch2) {
temp = 0;
} else {
temp = 1;
}
// 左边+1,上边+1, 左上角+temp取最小
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + temp);
}
}
return d[n][m];
}
private static int min(int one, int two, int three) {
return (one = one < two ? one : two) < three ? one : three;
}
private static String removeSign(String str) {
StringBuffer sb = new StringBuffer();
for (char item : str.toCharArray())
if (charReg(item)){
//System.out.println("--"+item);
sb.append(item);
}
return sb.toString();
}
private static boolean charReg(char charValue) {
return (charValue >= 0x4E00 && charValue <= 0X9FA5)
|| (charValue >= 'a' && charValue <= 'z')
|| (charValue >= 'A' && charValue <= 'Z')
|| (charValue >= '0' && charValue <= '9');
}
/*
private 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;
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]);
}
}
char[] result = new char[m+n+1];
int currentIndex = result.length - 1;
while (matrix[n].length != 0 && m>1 && n>1) {
if (matrix[n] == matrix[n - 1])
n--;
else if (matrix[n].length == matrix[m - 1][n])
m--;
else {
result[currentIndex] = chars_strA[m - 1];
currentIndex--;
n--;
m--;
}
}
return new String(result);
}*/
}
这是作者提供的一个答案,但是未经过ms hihocoder.平台的测试,因为时间太短,来不及提交!可以输出结果,时间空间的效率还不高,欢迎大家一起讨论!